sam-goodwin / eventual

Build scalable and durable micro-services with APIs, Messaging and Workflows
https://docs.eventual.ai
MIT License
174 stars 4 forks source link

Heartbeat Checkpoint #422

Open thantos opened 1 year ago

thantos commented 1 year ago
workflow(() => {
    const act = act1();

    act.onHeartbeat(async ({ i: 100 }) => {
        await reportProgress(i);
    });
});

const reportProceess = activity(...);

const act1 = activity<{ result: string }, { i: 100 } | undefined>({
   heartbeat: { seconds: 20 },
   timeout: { seconds: 160 }
}, (context: Context): { result: string } | AsyncToken => {
   ...doSomeWork...

   await sendToQueue({ token: context.activity.token, start: context.checkpoint });

    // should this be on the context to be typed?
   await sendHeartbeat();

   return makeAsync();
})

// lambda function consuming the queue
const workflowClient = new WorkflowClient();
export const handler = async (event) => {
    Promise.all(event.Records.map(async (record) => {
        const payload = JSON.parse(record.body);

        const items = [...];

        const start = event.start ?? 0;

        for(const i of items.slice()) {
           // some long process
           await workflowClient.heartbeatActivity<typeof act1>(
               payload.token,
               { i }
           );
        }

        // complete the activity with a payload
        await workflowClient.completeActivity<typeof act1>(
            payload.token,
            { result: "done"}
        );
    }));
}
thantos commented 1 year ago

https://github.com/functionless/eventual/issues/60