@@ -14,12 +14,9 @@ type Env = {
14
14
// this.env.WORKFLOW is WorkflowBinding
15
15
export class WorkflowBinding extends WorkerEntrypoint < Env > implements Workflow {
16
16
public async create ( {
17
- id,
18
- params,
19
- } : WorkflowInstanceCreateOptions ) : Promise < WorkflowInstance > {
20
- if ( ! id ) {
21
- id = crypto . randomUUID ( ) ;
22
- }
17
+ id = crypto . randomUUID ( ) ,
18
+ params = { } ,
19
+ } : WorkflowInstanceCreateOptions = { } ) : Promise < WorkflowInstance > {
23
20
const stubId = this . env . ENGINE . idFromName ( id ) ;
24
21
const stub = this . env . ENGINE . get ( stubId ) ;
25
22
@@ -34,13 +31,37 @@ export class WorkflowBinding extends WorkerEntrypoint<Env> implements Workflow {
34
31
}
35
32
) ;
36
33
37
- return new WorkflowHandle ( id , stub ) ;
34
+ const handle = new WorkflowHandle ( id , stub ) ;
35
+ return {
36
+ id : id ,
37
+ pause : handle . pause . bind ( handle ) ,
38
+ resume : handle . resume . bind ( handle ) ,
39
+ terminate : handle . terminate . bind ( handle ) ,
40
+ restart : handle . restart . bind ( handle ) ,
41
+ status : handle . status . bind ( handle ) ,
42
+ } ;
38
43
}
39
44
40
45
public async get ( id : string ) : Promise < WorkflowInstance > {
41
- const stubId = this . env . ENGINE . idFromName ( id ) ;
42
- const stub = this . env . ENGINE . get ( stubId ) ;
43
- return new WorkflowHandle ( id , stub ) ;
46
+ const engineStubId = this . env . ENGINE . idFromName ( id ) ;
47
+ const engineStub = this . env . ENGINE . get ( engineStubId ) ;
48
+
49
+ const handle = new WorkflowHandle ( id , engineStub ) ;
50
+
51
+ try {
52
+ await handle . status ( ) ;
53
+ } catch ( e ) {
54
+ throw new Error ( "instance.not_found" ) ;
55
+ }
56
+
57
+ return {
58
+ id : id ,
59
+ pause : handle . pause . bind ( handle ) ,
60
+ resume : handle . resume . bind ( handle ) ,
61
+ terminate : handle . terminate . bind ( handle ) ,
62
+ restart : handle . restart . bind ( handle ) ,
63
+ status : handle . status . bind ( handle ) ,
64
+ } ;
44
65
}
45
66
}
46
67
0 commit comments