# Litium 8 Run a Job once?

**URL:** <https://forum.litium.com/t/litium-8-run-a-job-once/2599>\
**Category:** Questions\
**Tags:** litium-8\
**Created:** [May 18, 2022, 9:16am UTC](https://forum.litium.com/t/litium-8-run-a-job-once/2599 "2022-05-18T09:16:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mynameisandreas](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/mynameisandreas/32/282_2.png) [@mynameisandreas](https://forum.litium.com/u/mynameisandreas)\
**Post date:** [May 18, 2022, 9:16am UTC](https://forum.litium.com/t/litium-8-run-a-job-once/2599/1 "2022-05-18T09:16:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![NilsN](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/nilsn/32/302_2.png) [@NilsN](https://forum.litium.com/u/NilsN)\
**Post date:** [May 18, 2022, 9:35am UTC](https://forum.litium.com/t/litium-8-run-a-job-once/2599/2 "2022-05-18T09:35:35Z")

</div>

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)](https://forum.litium.com/t/execute-priceagentscheduler-from-code-not-with-the-refresh-true-url/1196)

---

<div class="post-metadata">

**Author:** ![Ericsj11](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/ericsj11/32/899_2.png) [@Ericsj11](https://forum.litium.com/u/Ericsj11)\
**Post date:** [May 18, 2022, 9:42am UTC](https://forum.litium.com/t/litium-8-run-a-job-once/2599/3 "2022-05-18T09:42:12Z")

</div>

The DefinitionSetup.cs is like this, by using the \_settingService you can block the code from beeing run more then once.

---

<div class="post-metadata">

**Author:** ![mynameisandreas](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/mynameisandreas/32/282_2.png) [@mynameisandreas](https://forum.litium.com/u/mynameisandreas)\
**Post date:** [May 18, 2022, 9:51am UTC](https://forum.litium.com/t/litium-8-run-a-job-once/2599/4 "2022-05-18T09:51:53Z")

</div>

But restart will still execute it if im not wrong?

---

<div class="post-metadata">

**Author:** ![NilsN](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/nilsn/32/302_2.png) [@NilsN](https://forum.litium.com/u/NilsN)\
**Post date:** [May 18, 2022, 10:02am UTC](https://forum.litium.com/t/litium-8-run-a-job-once/2599/5 "2022-05-18T10:02:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![mynameisandreas](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/mynameisandreas/32/282_2.png) [@mynameisandreas](https://forum.litium.com/u/mynameisandreas)\
**Post date:** [May 18, 2022, 10:48am UTC](https://forum.litium.com/t/litium-8-run-a-job-once/2599/6 "2022-05-18T10:48:22Z")

</div>

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 👍

---

<div class="post-metadata">

**Author:** ![patric.forsgard](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/patric.forsgard/32/10_2.png) [@patric.forsgard](https://forum.litium.com/u/patric.forsgard)\
**Post date:** [May 18, 2022, 4:16pm UTC](https://forum.litium.com/t/litium-8-run-a-job-once/2599/7 "2022-05-18T16:16:12Z")

</div>

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.

```cs
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](https://forum.litium.com/t/execute-priceagentscheduler-from-code-not-with-the-refresh-true-url/1196) that @NilsN proposed.

> [@mynameisandreas](#):
>
> 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

I don’t think I get the content of this, can you explain more of what you trying to do there?

---

<div class="post-metadata">

**Author:** ![mynameisandreas](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.litium.com/mynameisandreas/32/282_2.png) [@mynameisandreas](https://forum.litium.com/u/mynameisandreas)\
**Post date:** [May 18, 2022, 7:43pm UTC](https://forum.litium.com/t/litium-8-run-a-job-once/2599/8 "2022-05-18T19:43:10Z")

</div>

> [@patric.forsgard](#):
>
> `_settingService`

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!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/litium/original/1X/12eddac3b8b661de1db86af10ac8151b571e6fb9.png) [@system](https://forum.litium.com/u/system)\
**Post date:** [June 15, 2022, 7:43pm UTC](https://forum.litium.com/t/litium-8-run-a-job-once/2599/9 "2022-06-15T19:43:27Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
