Persisting and restoring workflows
You can save a reference to any workflow to restore its WorkflowHandler
and retrieve the result later. This is especially useful for long-running automations that might be interrupted by an application restart.
The process involves these steps:
- When you initiate a workflow, save its
workflowId
. You must also save the function name constant you called; this is required to restore the handler with the correct typed response later. - After a restart, retrieve the saved
workflowId
and the function name constant. - Use the
client.restoreWorkflow()
method, passing both theworkflowId
and the constant to restore the handler. - Call the
result()
method on the restored handler to get the result.
// --- Step 1 & 2: Initiate a workflow and save its references ---
async function startAndSaveWorkflow(taskId) {
try {
const functionName = FUNCTION_NAME.checkConnectionStatus;
const handler = await client.checkConnectionStatus({ personUrl: "..." });
const workflowId = handler.workflow.id;
// Save both the ID and the function name constant to your database
await db.saveTask({
taskId: taskId,
workflowId: workflowId,
functionName: functionName
});
console.log(`Workflow ${workflowId} started and saved for task ${taskId}.`);
} catch (e) {
console.error('Failed to start workflow:', e.message);
}
}
// --- Step 3 & 4: Restore the workflow and get the result ---
async function restoreAndGetResult(taskId) {
try {
// Retrieve the saved workflow details from your database
const task = await db.getTask(taskId);
if (!task || !task.workflowId || !task.functionName) {
console.log(`No pending workflow found for task ${taskId}.`);
return;
}
// Restore the handler using the saved ID and function name
const restoredHandler = client.restoreWorkflow(task.workflowId, task.functionName);
console.log(`Handler for workflow ${task.workflowId} restored.`);
// Get the final result
const { data, errors } = await restoredHandler.result();
if (data) {
console.log(`Task ${taskId} complete. Result:`, data);
await db.updateTaskStatus(taskId, 'complete');
}
} catch (e) {
console.error(`Failed to get result for task ${taskId}:`, e.message);
await db.updateTaskStatus(taskId, 'failed');
}
}
# Python SDK is coming soon!
#
# You can always use Linked API through HTTP
# https://linkedapi.io/docs/
// Go SDK is coming soon!
//
// You can always use Linked API through HTTP
// https://linkedapi.io/docs/