Retries with Tasks

Katrin Geske
Katrin Geske
  • Updated

When executing tasks, it might be useful to add retry mechanisms to the template. Retries are usually sensible if the task failed with any kind of error. Tasks are marked as error some. Therefore, babelforce allows to react on such a status.

Below we provide an extension to your script that allows you to define how often a task is retried (attempts) and what the time between each attempt (delay) should be. Of course, if a retry was successful, no further attempt will be scheduled.

{
"required_fields": [
{yourRequiredFields}
],
"task": {
"type": "google.pub.sub",
"body": {
"retry": {
"backoff": [
"now",
"in 1h",
"in 8h",
"in 24h",
"in 7d"
],
"attempt": 0
},
"data": {}
},
"actions": {
"on_scheduled": [
{yourActions}
],
"on_completed": [
{
"js": {
"code": "() => { task.body.retry.attempt = task.body.retry.attempt + 1; const has_error = !(context?.publishedMessageId?.messageIds?.length > 0) && task.body.retry.attempt < task.body.retry.backoff.length; return { 'retry':has_error,'attempts': task.body.retry.attempt};}",
"var": "check"
}
},
{
"codeblock": {
"error": {
"handling": "continue",
"visibility": "hide"
},
"condition": "() => context.check.retry",
"actions": [
{
"js": {
"code": "() => { return task.body.retry.backoff[task.body.retry.attempt];}",
"var": "scheduled_at"
}
},
{
"js": {
"code": "() => { const retry_body = task.body; retry_body['parent_task_id']=task.id; retry_body.retry.attempt = context.check.attempts; return retry_body;}",
"var": "retry_body"
}
},
{
"http": {
"url": "{{env.babelforce.url}}/api/v3/tasks",
"method": "POST",
"header": {
"Authorization": "Bearer {{task.token.access_token}}",
"Content-Type": "application/json"
},
"body": {
"type": "{{{task.type}}}.retry.{{{context.check.attempts}}}",
"body": "{{{context.retry_body}}}",
"actions": "{{{task.actions}}}",
"task_completion": "{{{task.task_completion}}}",
"selection_settings": "{{{task.selection_settings}}}",
"scheduled_at": "{{{context.scheduled_at}}}"
}
}
}
]
}
}
]
},
"task_completion": {
"autoComplete": true
},
"selection_settings": {
"enable": false
},
"scheduled_at": "now"
}
}

 

 

Related to

Was this article helpful?

/

Comments

0 comments

Please sign in to leave a comment.