Hey!
How would you design a job in Litium 8 to only run once?
In Litium 7 I would have had a task I can execute from backoffice. How would you do in Litium 8?
Happy coding folks!
Litium version: 8.3.1
Hey!
How would you design a job in Litium 8 to only run once?
In Litium 7 I would have had a task I can execute from backoffice. How would you do in Litium 8?
Happy coding folks!
Litium version: 8.3.1
You could have class that runs the job and then schedule that class via SchedulerService, something like this: Execute PriceAgentScheduler from code (not with the "refresh=true" URL)
The DefinitionSetup.cs is like this, by using the _settingService you can block the code from beeing run more then once.
But restart will still execute it if im not wrong?
Yes, a restart would run the job again (if you’re using AutoStart), but by using the service you could set a flag if it’s been run already and then checking that flag when the job executes.
Okey, it dosent really solve my problem, I feel having a flag is way much than needed when I want something to be run only once, i know it’s a special case everytime it’s different things.
Though thinking about it, it would be nice to have a vertical table or something similar that can be used per channel to keep track of things like this, instead of us creating tables or files to hold flags in! ![]()
But thank you for the answers ![]()
I should say that the solution depends on what you actual want to do and I think it may be more a manually trigged job than a job that run once.
If we take a job that is executed only one (@Ericsj11 & @NilsN suggestion) will look like this and is executed during startup of the application. If the job have been executed before it will terminate itself.
using System.Threading;
using System.Threading.Tasks;
using Litium.Common;
using Litium.Runtime;
namespace Litium.Accelerator.Mvc
{
[Autostart]
public class SingleStartupJob : IAsyncAutostart
{
private const string _key = "single-startup-job";
private readonly SettingService _settingService;
public SingleStartupJob(
SettingService settingService)
{
_settingService = settingService;
}
public async ValueTask StartAsync(CancellationToken cancellationToken)
{
if (_settingService.Get<bool?>(_key).GetValueOrDefault())
{
return;
}
try
{
await Execute(cancellationToken);
}
finally
{
_settingService.Set(_key, true);
}
}
private ValueTask Execute(CancellationToken cancellationToken)
{
// Add the code that should be executed only once.
return ValueTask.CompletedTask;
}
}
}
To be able to manage and run a job manually from back office you need to add a panel/settings page that will display the selectable jobs and make it possible to trigger them, the trigger function can be similar to this that @NilsN proposed.
I don’t think I get the content of this, can you explain more of what you trying to do there?
You are a 100% right about trigger job, also this is a one of a kind job. I have gone in another direction, but this would definitely be a good solution to have something only be runned once completly withouth external dependencies!
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.