# Scheduler

The SDK provides a flexible scheduler to do actions in a specific interval. You write your job in C# and the user of the job can configure it in the extension config (plugin.conf).

The scheduler can be configured with the plugin.conf file:

```hocon
scheduler {
   enabled = true
   jobs {
      // write here your job configurations
   }
}
```

### Writing an job

We now writing a job that logs a message.

```csharp
public class HelloJob : Job {
    private static readonly LoggerInstance Logger = Log.GetLogger<HelloJob>(); // Configure a logger specified for the HelloJob

    public override void Run() {
        Log.Info("Hello World");
    }

}
```

Now the job is implemented but it has to be registered in the scheduler with a name in your `MossExtension` class. Jobs have to be registered before your extension will be initialized.

```csharp
[UnmanagedCallersOnly(EntryPoint = "moss_extension_register")]
public static ulong Register()
{
    TaskScheduler.Activator.Register<HelloWorldJob>("hello");

    Init<MyMossExtension>();

    return 0;
}
```

You can now configure the scheduler to run your job:
\
In the plugin.conf set this properties:

```hocon
scheduler {
    jobs {
       helloworld { # the name of the job instance
          class = hello
          enabled = true
          interval = 5s
       }
    }
}
```

now when your extension is started the scheduler will run your job every 5 seconds

### Parameterize your job

the user can specifiy job specific options in the job configuration. In our sample we will introduce a message option:

```hocon
scheduler {
    jobs {
       helloworld { # the name of the job instance
          class = hello
          enabled = true
          interval = 5s
          options {
             message = "I am a configured message"
          }
       }
    }
}
```

Now you can read those option with the `Options`-property of the job:

```csharp
public class HelloJob : Job {
    private static readonly LoggerInstance Logger = Log.GetLogger<HelloJob>(); // Configure a logger specified for the HelloJob

    string message;
    public override void Init() {
       message = Options.message; // you can directly access your option as member
    }

    public override void Run() {
        Log.Info(message);
    }
}
```

You can see different examples of custom jobs in the [Totletheyn](https://github.com/RedTTGMoss/Moss.NET.SDK/tree/main/src/Plugins/Totletheyn/Jobs) plugin.
