Ask or search…
K
Links

Report Scheduler

Let's say your web app allows users to schedule reports to email off. These items are configurable and the user can add or remove them at any time.
  • The front-end web app saves a database record back with all the information needed to run the specific report and it's parameters.
  • Then the Scheduler reads the table for those database records every few minutes and updates, removes, reschedules any changed items.
  • You get a simple callback when it's time to execute the event task

Demo Scheduler

The demo below hooks up everything but actually generating reports, and emailing them off.
  • The RunType argument could easily contain what reports to generate.
  • The RunArgs could easily contain additional user information like who it's being sent to, a database ID of the job to lookup, etc.
1
PerigeeApplication.ApplicationNoInit("Demo Scheduler", (c) =>
2
{
3
4
//Declare a new memory source, remember to use a single instance of memory/file based sources or locking can occur
5
using var MemSource = new MemoryScheduledSource("memSource.json", c.CTS.Token);
6
7
//Add scheduled items.
8
//If this was something like a DatabaseScheduledSource, we obviously would control these records from the database, not here.
9
MemSource.AddIfNotExists(GenericScheduledItem<ushort>.MemoryItem(0, "A scheduler, 15sec", "A", "a;b;c", "*/15 * * * * *", TimeZoneInfo.Local));
10
MemSource.AddIfNotExists(GenericScheduledItem<ushort>.MemoryItem(1, "B scheduler, 45sec", "B", "b;c;d", "45 * * * * *", TimeZoneInfo.Local));
11
12
//Add a scheduler with the MemorySource, a single callback is given for anything required to run (multi-threaded)
13
c.AddScheduler("Main", MemSource, (ct, l, item) => {
14
if (item.GetRunType() == "A")
15
{
16
l.LogInformation("Running A with {args}", item.GetRunArgs());
17
}
18
else if (item.GetRunType() == "B")
19
{
20
l.LogInformation("Running B with {args}", item.GetRunArgs());
21
}
22
23
});
24
25
});
The demo code uses the Memory Scheduler as it does not require a remote source. Typically speaking you would tie the event task descriptions back to a database record (like the MSSQL Scheduler Source).
  • Line 5 - Declare a new event source. This demo uses the memory scheduler.
  • Line 9-10 - Add the two scheduled items, A, and B, to schedule and execute
  • Line 13 - Add a scheduler using the source we defined, and declare the callback.
    • You're given a CancellationToken for respecting graceful shutdown event.
    • An ILogger for logging to the system and defined sink sources.
    • And the GenericScheduledItem<ushort> Which is the interfaced item that allows you to access it's definition values.
  • Line 14 - You can execute or perform any tasks you need. Like generating a report with parameters.
    • You can see the call to GetRunType(). There's also GetRunArgs(), GetLastRunDate(), GetName(), etc.