Difference between revisions of "Automations"
(Created page with "Automations are a mechanism in SocialStack to create a task which runs on some defined schedule. They're also known as "background jobs" or "daemons". The schedule is defined...") |
|||
| Line 1: | Line 1: | ||
Automations are a mechanism in SocialStack to create a task which runs on some defined schedule. They're also known as "background jobs" or "daemons". The schedule is defined using a [https://en.wikipedia.org/wiki/Cron cron expression]. | Automations are a mechanism in SocialStack to create a task which runs on some defined schedule. They're also known as "background jobs" or "daemons". The schedule is defined using a [https://en.wikipedia.org/wiki/Cron cron expression]. | ||
= Usage = | |||
First, install the Automations module: | |||
<syntaxhighlight lang="bash"> | |||
socialstack install Api/Automations | |||
</syntaxhighlight> | |||
That adds the Events.Automation method to your project, which can then be used like so: | |||
<syntaxhighlight lang="csharp"> | |||
// If your automation doesn't need to do anything async: | |||
Events.Automation("once_a_second", "* * * * * ? *") | |||
.AddEventListener((Context context, AutomationRunInfo info) => { | |||
System.Console.WriteLine("- Runs once a second -"); | |||
return new ValueTask<AutomationRunInfo>(info); | |||
}); | |||
// With async: | |||
Events.Automation("once_a_second", "* * * * * ? *") | |||
.AddEventListener(async (Context context, AutomationRunInfo info) => { | |||
System.Console.WriteLine("- Runs once a second -"); | |||
await _someOtherService.DoSomething(context); | |||
return info; | |||
}); | |||
</syntaxhighlight> | |||
Note that Automation has two args - a name and a schedule. The name is such that your automation can be triggered by hand separately, for example if you need a background job to run "now" rather than waiting for the schedule to trigger it. | |||
Because of the name, it is also possible to add additional handlers to a particular job run. For example if you know a job is called "update_certificate" then you can add an additional handler to the event like so: | |||
<syntaxhighlight lang="csharp"> | |||
Events.Automation("update_certificate") | |||
.AddEventListener((Context context, AutomationRunInfo info) => { | |||
System.Console.WriteLine("- This runs immediately after the update_certificate automation. -"); | |||
return new ValueTask<AutomationRunInfo>(info); | |||
}, 11); // Priority 11 ensures we for certain will happen after the main automations listener(s) occur. | |||
</syntaxhighlight> | |||
Revision as of 21:58, 30 July 2022
Automations are a mechanism in SocialStack to create a task which runs on some defined schedule. They're also known as "background jobs" or "daemons". The schedule is defined using a cron expression.
Usage
First, install the Automations module:
socialstack install Api/Automations
That adds the Events.Automation method to your project, which can then be used like so:
// If your automation doesn't need to do anything async:
Events.Automation("once_a_second", "* * * * * ? *")
.AddEventListener((Context context, AutomationRunInfo info) => {
System.Console.WriteLine("- Runs once a second -");
return new ValueTask<AutomationRunInfo>(info);
});
// With async:
Events.Automation("once_a_second", "* * * * * ? *")
.AddEventListener(async (Context context, AutomationRunInfo info) => {
System.Console.WriteLine("- Runs once a second -");
await _someOtherService.DoSomething(context);
return info;
});
Note that Automation has two args - a name and a schedule. The name is such that your automation can be triggered by hand separately, for example if you need a background job to run "now" rather than waiting for the schedule to trigger it.
Because of the name, it is also possible to add additional handlers to a particular job run. For example if you know a job is called "update_certificate" then you can add an additional handler to the event like so:
Events.Automation("update_certificate")
.AddEventListener((Context context, AutomationRunInfo info) => {
System.Console.WriteLine("- This runs immediately after the update_certificate automation. -");
return new ValueTask<AutomationRunInfo>(info);
}, 11); // Priority 11 ensures we for certain will happen after the main automations listener(s) occur.