Run data
A handler is an ordinary function, so it can use whatever is in scope around it. The data option (data, Data) is for what isn’t: values you decide where the job is defined or where the scheduler is created, and want in the handler when a run starts.
Each run receives it as ctx.data (ctx.Data). It is undefined (null) when you gave none.
Where the data comes from
Section titled “Where the data comes from”| Given in | Applies to | Example |
|---|---|---|
| The scheduler’s options | Every job of the scheduler | new Scheduler({ data: ... }); SchedulerOptions.Data |
| Register defaults | Every job of one decorated object | register(object, { defaults: { data } }); Register(object, new RegisterOptions { Defaults = new() { Data = ... } }) |
| A job’s options | That job | { data: ... }; new() { Data = ... } |
| A decorator (Node.js) | That job | @Every('export', '1h', { data: ... }) |
| A manual run | One manual run | runNow('export', { data: ... }); RunNowAsync("export", data) |
They are layers, from the most general to the most specific, in the order of the table. Two plain objects (in .NET, two IReadOnlyDictionary<string, object?> such as a Dictionary<string, object?>) are merged, and the more specific one’s keys win. Any other value (a string, a number, an array, a class or record instance) replaces what is under it.
Reading it when the run starts
Section titled “Reading it when the run starts”The data can be a function (in .NET, a JobDataProvider). It is called when each run starts, so the handler gets the value as it is at that moment: a token that expires, the permissions a user has now, a feature flag.
The function receives the run: everything in ctx except the data itself, including its cancellation. It may be async (in .NET it returns a ValueTask<object?>).
| If the function | Then |
|---|---|
| Throws or rejects | The run fails with that error, like a handler that throws. The handler isn’t called |
| Is slower than the job’s timeout | The run times out |
Changing it while the scheduler runs
Section titled “Changing it while the scheduler runs”setData() (SetData()) replaces the scheduler’s data. Runs that start afterwards get the new one; runs in progress keep theirs.
For a single run, pass data to the manual run:
Typing it
Section titled “Typing it”ctx.data is unknown unless you say what it is. Name the type in the handler:
ctx.Data is an object?. Read it as the type you gave with ctx.GetData<T>(), which throws an InvalidOperationException that names both types when it is something else (the run then fails). A record of your own reads best; dictionaries are for data that is merged from layers:
In TypeScript the type is your statement about what you configured: anarchon doesn’t check the value against it.
What run data is not
Section titled “What run data is not”- Not shared between instances. Each instance builds the data of the runs it executes, from its own settings. Give every instance the same ones, like the jobs themselves.
- Not stored. It isn’t written to the store or to history, so it can hold secrets and objects such as a database client.
- Not a per-run payload from another instance. A run is defined by its time, not by a message. To hand work from one place to another, use a queue.
Runnable examples: examples/run-data.ts (pnpm example:run-data) and examples/RunData (dotnet run --project examples/RunData).
- Writing handlers: the rest of
ctx - Decorators and attributes: data on declared jobs and objects
- Scheduler API