Skip to content

Instantly share code, notes, and snippets.

@geomagilles
Created May 16, 2019 15:22
Show Gist options
  • Save geomagilles/f029eadbb28c459402fc2b4a7759117a to your computer and use it in GitHub Desktop.
Save geomagilles/f029eadbb28c459402fc2b4a7759117a to your computer and use it in GitHub Desktop.
const { Workflow, Task, Wait } = require("zenaton");
// start child workflow
let StartChildWorkflow = Task("StartChildWorkflow", {
init(users, id) {
this.users = users;
this.id = id;
},
handle: async function() {
await new ChildWorkflow(this.user, this.id).dispatch()
}
});
// send event to parent workflow
let SendChildWorkflowCompletedEvent = Task("SendChildWorkflowCompletedEvent", async function() {
ParentWorkflow.whereId(this.id).send("ChildWorkflowCompleted", {user: this.user})
});
// whatever task
let DoSomething = Task("DoSomething", async function() {
console.log(this);
});
// Child Workflow
let ChildWorkflow = Workflow("ChildWorkflow", {
init(user, id) {
this.user = user;
this.id = id;
},
handle: async function() {
// wait a few seconds
await new Wait().seconds(7).execute();
// do what you want
// ...
// send event to parent workflow
await new SendChildWorkflowCompletedEvent(this).execute();
}
});
// Parent Workflow
let ParentWorkflow = Workflow("ParentWorkflow", {
init(users, id) {
this.users = users;
this.id = id;
this.completed = 0;
},
handle: async function() {
// dispatch sub-workflows
for(var user of this.users) {
await new StartChildWorkflow(user, this.id).dispatch();
}
// wait for completion for all users
await new Wait("AllChildWorkflowCompleted").execute();
// finished for all users, I continue
await new DoSomething("This is the end").execute();
},
onEvent(eventName, eventData) {
if (eventName === "ChildWorkflowCompleted") {
this.completed++;
if (this.completed == this.users.length) {
ParentWorkflow.whereId(this.id).send("AllChildWorkflowCompleted");
}
}
},
id() {
return this.id;
}
});
module.exports = ParentWorkflow;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment