magic.lambda.scheduler 17.2.0

dotnet add package magic.lambda.scheduler --version 17.2.0
NuGet\Install-Package magic.lambda.scheduler -Version 17.2.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="magic.lambda.scheduler" Version="17.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add magic.lambda.scheduler --version 17.2.0
#r "nuget: magic.lambda.scheduler, 17.2.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install magic.lambda.scheduler as a Cake Addin
#addin nuget:?package=magic.lambda.scheduler&version=17.2.0

// Install magic.lambda.scheduler as a Cake Tool
#tool nuget:?package=magic.lambda.scheduler&version=17.2.0

magic.lambda.scheduler - Creating and maintaining tasks and schedules from Hyperlambda

This project gives you the ability to create persisted and scheduled Hyperlambda tasks for Magic. More specifically it provides the following slots.

  • [tasks.create] - Creates a new task
  • [tasks.get] - Returns an existing task
  • [tasks.list] - Lists all tasks
  • [tasks.update] - Updates an existing task
  • [tasks.count] - Counts tasks in system
  • [tasks.execute] - Executes an existing task
  • [tasks.delete] - Deletes a task
  • [tasks.schedule] - Schedules an existing task
  • [tasks.schedule.delete] - Deletes an existing schedule
  • [tasks.scheduler.start] - Starts the task scheduler

How to use [tasks.create]

To create and persist a task you can use something such as the following.

tasks.create:foo-bar-task-1
   .lambda

      // Your task's lambda object goes here
      log.info:Executing foo-bar-task-1

The name or ID of your task in the above example becomes "foo-bar-task-1", and the task can be referenced later using this name. The name must be unique, otherwise an exception will be thrown. A task can also optionally have a [description] argument, which is a humanly readable description, describing what your task does. Below is an example.

tasks.create:foo-bar-task-2
   description:This task will do a little bit of foo and some bar afterwards.
   .lambda

      log.info:Executing foo-bar-task-2

Notice - Your task's [id] argument, can only contain alpha numeric characters, a-z, 0-9 - In addition to the following special characters; ., - and _. When you create a task, you can also optionally schedule it simultaneously, by providing any amount of [due] dates, and/or [repeats] patterns, which will create and schedule the task at the same time. Below is an example.

tasks.create:foo-bar-task-3
   due:date:"2025-01-01T23:59:27"
   repeats:5.seconds
   repeats:3.hours
   due:date:"2030-01-01T23:59:27"
   .lambda

      log.info:Executing foo-bar-task-2

The above schedules your task for being executed once in the year of 2025, another time in the year of 2030, in addition to once every 3 hours and once every 5 seconds. This document will describe in details how schedules works further down.

You can also update an existing task by using the [tasks.update] slot. This slot allows you to update a task's description and its Hyperlambda, but you cannot associate schedules with your task using this slot. If you've already created your task and you need to (re) schedule it, you'll need to combine the slots [tasks.schedule] and [tasks.schedule.delete] together. Below is an example of first creating a task for then to update it. Any existing schedules you've already associated with your task as you update it will not be changed.

tasks.create:foo-bar-task-4
   .lambda

      log.info:Executing foo-bar-task-1

tasks.update:foo-bar-task-4
   description:This is the foo bar task
   .lambda

      log.info:Another log entry now!

How to use [tasks.execute]

You can explicitly execute a persisted task by invoking [tasks.execute] and pass in the ID of your task. Below is an example, that assumes you have created the above "foo-bar-task-4" task first.

tasks.execute:foo-bar-task-4

Notice, if you try to execute a non-existing task, an exception will be thrown.

How to use [tasks.delete]

Use the [tasks.delete] signal to delete a task. This will also delete all future schedules for your task and automatically dispose any timers associated with the task. An example can be found below.

tasks.delete:task-id

Besides from the task ID, the delete task signal doesn't take any arguments.

How to use [tasks.get]

To inspect a task you can use the following.

tasks.get:task-id

The above will return the Hyperlambda for your task, in addition to your task's description. If you add a [schedules] argument and set its value to boolean true, this slot will also return all schedules associated with the task.

tasks.get:task-id
   schedules:true

How to use [tasks.list]

To list tasks, you can use the [tasks.list] signal. This slot optionally handles an [offset] and a [limit] argument, allowing you to page, which might be useful if you have a lot of tasks in your system. If no [limit] is specified, this signal will only return the first 10 tasks, including the task's Hyperlambda, but not its repetition pattern(s), or due date(s). Below is an example.

tasks.list
   offset:20
   limit:10

How Hyperlambda tasks are stored

All tasks are persisted into your magic database, either in MySQL, SQLite, PostgreSQL, or Microsoft SQL Server. This implies that even if your server is stopped, all scheduled tasks and persisted tasks will automatically load up again, and be available and re-scheduled as the server is restarted. This might imply that all tasks in the past are immediately executed, which is important for you to understand, since any tasks with a due date in the past, are executed immediately as the server restarts again. Tasks are by default persisted into your tasks table, and schedules are persisted into your task_due table.

How to use Hyperlambda tasks as business process workflows

Hyperlambda tasks allows you to persist a "function invocation" for later to execute it, once some specific condition occurs - Effectively giving you the most important features from Microsoft Workflow Foundation, without the ridiculous XML and WYSIWYG features - In addition to that this also is a .Net Core library, contrary to MWF that only works for the full .Net Framework. The Hyperlambda task scheduler is also probably at least somewhere between 200 and 400 times faster than MWF, due to not using reflection during execution.

How to use [tasks.schedule]

If you want to create a scheduled task, you can choose to have the task executed once in the future, at a specified date and time, by invoking [tasks.schedule], and reference your task after it's been created, passing in a [due] argument being a date and time in the future for when you want to execute your task.

tasks.create:foo-bar-task-3
   .lambda
      log.info:Executing foo-bar-task-3

tasks.schedule:foo-bar-task-3
   due:date:"2025-12-24T17:00"

The above [due] argument is a UTC date and time in the future for when you want your task to be scheduled for execution. After the task has been executed, it will never execute again, unless you manually execute it, or invoke [tasks.schedule] again.

Notice - You cannot create a task with a due date being in the past, and all dates are assumed to be in the UTC timezone. The unique ID of the schedule created is returned when you explicitly schedule a task using the [tasks.schedule] slot. If you add schedules during invocations to [tasks.create] though, no schedule IDs are returned, but you can still retrieve all schedules by invoking [tasks.get] and passing in [schedules] to have the slot return all schedules for a specific task.

How to repeatedly execute a task

There are 3 basic [repeats] patterns for the Magic Lambda Scheduler, in addition to that you can extend it with your own parametrized repeating IRepetitionPattern implementation. The built in repetition patterns, are as follows.

  • x.units - Units can be one of "seconds", "minutes", "hours", "days", "weeks" or "months" - And x can be any integer value.
  • MM.dd.HH.mm.ss - Where the entities are in sequence months, days in months, hours, minutes and seconds.
  • ww.HH.mm.ss - Where the entities are weekdays, hour, minute and second.

Notice, MM, dd, and ww can have double asterix (**) as their values, implying "whatever value". MM, dd and ww can also have multiple values, separated by the pipe character (|), to provide multiple values for these types. See examples of this further below in this documentation.

Evaluating a task periodically

Evaluating your task every second/minute/hour/etc can be done by using something such as the following.

tasks.create:task-id-qwqw
   .lambda

      log.info:Executing repeating task

tasks.schedule:task-id-qwqw
   repeats:50.seconds

The above will evaluate your task every 50 second. The above "seconds" can be exchanged with "minutes", "hours", "days", "weeks" or "months". Notice, this allows you to have very large values, to have tasks that are repeating very rarely, such as the following illustrates.

tasks.create:task-id-qwerty123
   .lambda

      log.info:Executing seldomly repeating task once every 10 year

tasks.schedule:task-id-qwerty123
   repeats:3650.days

The above task will only be evaluated every 3650 days, which becomes once every 10 years. Below is a list of all valid units types.

  • seconds
  • minutes
  • hours
  • days
  • weeks
  • months

Periodically scheduled tasks

To create a task that is executed on the first day of every month, at 5PM, you can use the following repetition pattern.

tasks.create:task-id-xyx
   .lambda

      log.info:It is the 1st of the month, any month, and the time is 5AM at night.

tasks.schedule:task-id-xyx
   repeats:**.01.05.00.00

Hours must be supplied as "military hours", implying from 00:00 to 23:59, where for instance 22 equals 10PM UTC time. Also notice how we provided a double asterix (**) for the month parts, implying "all months". We could also have provided multiple days, and/or months, such as the following illustrates. The Hyperlambda below will create a task that is executed in January and February, but only on the 5th and 15th of these months.

tasks.create:task-id
   .lambda

      log.info:It is the 5th or the 15th of January or February, and the time is 5AM at night.

tasks.schedule:task-id
   repeats:01|02.5|15.05.00.00

By using the double asterix for month and day of month, you can create a task that is executed every day, at some specific time of the day (UTC time). Below is an example.

tasks.create:task-id-555
   .lambda

      log.info:It is the 10PM now.

tasks.schedule:task-id-555
   repeats:**.**.22.00.00

Weekdays pattern and Hyperlambda tasks

If you use the weekdays pattern when you create your tasks, you can create any combinations of weekdays, allowing you to supply multiple weekdays in a single repetition pattern. Below is an exhaustive list of all possible weekdays.

  • Monday
  • Tuesday
  • Wednesday
  • Thursday
  • Friday
  • Saturday
  • Sunday

To evaluate a task every Saturday and Sunday for instance, you can use saturday|sunday as your weekday. Below is an example. Notice, weekdays are case insensitive.

tasks.create:task-id-567
   .lambda

      log.info:It is Saturday or Sunday, and the time is 22PM.

tasks.schedule:task-id-567
   repeats:saturday|SUNDAY.22.00.00

You can also provide a double asterix (**) for the weekdays pattern, implying "all days of the week". Notice how weekdays are case-insensitive as illustrated above.

Creating your own task repetition pattern

In addition to the internally provided repetition patternsfor tasks, you can also create your own repetition pattern type, by implementing the IRepetitionPattern interface on one of your own types, and registering your type create function by using the PatternFactory.AddExtensionPattern method. If you do, you'll have to reference your repetition pattern type using "ext:", combined with its resolver key. Implying if you register your IRepetitionPattern type such that it resolves using for instance "my-pattern" as its key, you'll have to use _"ext:my-pattern:args" to reference it later, as you wish to create an instance of your custom pattern type. The "args" part are any arguments supplied to your pattern during creation. Below is an example that creates a custom repetition pattern.

private class ExtPattern : IRepetitionPattern
{
    readonly string _args;
    public string Value => "ext:custom-pattern:" + _args;

    public ExtPattern(string args)
    {
        _args = args;
    }

    public DateTime Next()
    {
        return new DateTime(2030, 11, 11, 11, 11, 57);
    }
}

The above IRepetitionPattern will statically resolve to the 11th of November 2030, at 23:11:57. But the idea is that the args supplied during creation, can be used to parametrize your pattern, and calculate the next due date for your schedule. After you have declared your custom IRepetitionPattern type, you'll need to inform the PatternFactory class that you want to use the above class, and resolve it using some specific key from your schedules. This is accomplished using something resembling the following code.

PatternFactory.AddExtensionPattern(
    "custom-type",
    str =>
    {
        return new ExtPattern(str);
    });

The above code will ensure that every time you use "custom-type" as a repetition pattern type, the create function above will be invoked, allowing you to create and decorate an instance of your custom IRepetitionPattern type. The str argument to your above create function, will be everything after the ext:custom-type: parts, when creating an instance of your pattern. To use the above pattern in your own code, you can use something such as the following.

tasks.create:custom-repetition-pattern
   .lambda

      log.info:Executing custom-repetition-pattern

tasks.schedule:custom-repetition-pattern
   repeats:"ext:custom-pattern:some-arguments-here"

In the above [repeat] argument, the ext parts informs the scheduler that you want to use a custom repetition pattern, the custom-pattern parts resolves to your IRepetitionPattern create function, and the "some-arguments-here" parts will be passed into your above ExtPattern constructor allowing you to parametrize your pattern any ways you see fit.

How to use [tasks.scheduler.start]

Notice, this slot is not intended for being directly invoked by your code, but internally used by Magic after the system has been setup.

Task scheduler internals

One System.Threading.Timer object will be created for each due date/repetition pattern you have, and kept in memory of your Magic server, which doesn't lend itself to thousands of schedules for obvious reasons - But due to the mechanics of how these are implemented at the system level, this is still highly scalable for most solutions.

When a repeating task has finished executed, the next due date for the task's execution will be calculated using its interval pattern - Implying that if you use a 5 second pattern, the schedule for its next execution, will be calculated 5 seconds from the time the task finished executing, which might not necessarily imply that your tasks are executed exactly every 5 seconds, depending upon how much time your task requires to execute. The interval pattern declares how many units to count to before executing the task again, from when the task finished executing.

Magic's GitHub project page

Magic is 100% Open Source and you can find the primary project GitHub page here.

Project website for magic.lambda.scheduler

The source code for this repository can be found at github.com/polterguy/magic.lambda.scheduler, and you can provide feedback, provide bug reports, etc at the same place.

  • Build status
  • Quality Gate Status
  • Bugs
  • Code Smells
  • Coverage
  • Duplicated Lines (%)
  • Lines of Code
  • Maintainability Rating
  • Reliability Rating
  • Security Rating
  • Technical Debt
  • Vulnerabilities

The projects is copyright Thomas Hansen 2023 - 2024, and professionally maintained by AINIRO.IO.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on magic.lambda.scheduler:

Package Downloads
magic.library

Helper project for Magic to wire up everything easily by simply adding one package, and invoking two simple methods. When using Magic, this is (probably) the only package you should actually add, since this package pulls in everything else you'll need automatically, and wires up everything sanely by default. To use package go to https://polterguy.github.io

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
17.2.0 263 1/22/2024
17.1.7 155 1/12/2024
17.1.6 120 1/11/2024
17.1.5 143 1/5/2024
17.0.1 174 1/1/2024
17.0.0 316 12/14/2023
16.11.5 295 11/12/2023
16.9.0 278 10/9/2023
16.7.0 512 7/11/2023
16.4.1 327 7/2/2023
16.4.0 358 6/22/2023
16.3.1 327 6/7/2023
16.3.0 303 5/28/2023
16.1.9 581 4/30/2023
16.1.1 316 4/20/2023
15.10.11 353 4/13/2023
15.9.1 561 3/27/2023
15.9.0 438 3/24/2023
15.8.2 457 3/20/2023
15.7.0 361 3/6/2023
15.5.0 1,516 1/28/2023
15.2.0 640 1/18/2023
15.1.0 1,104 12/28/2022
14.5.7 675 12/13/2022
14.5.5 789 12/6/2022
14.5.1 637 11/23/2022
14.5.0 576 11/18/2022
14.4.5 658 10/22/2022
14.4.1 749 10/22/2022
14.4.0 634 10/17/2022
14.3.1 1,214 9/12/2022
14.3.0 632 9/10/2022
14.1.3 917 8/7/2022
14.1.2 644 8/7/2022
14.1.1 605 8/7/2022
14.0.14 677 7/26/2022
14.0.12 654 7/24/2022
14.0.11 633 7/23/2022
14.0.10 616 7/23/2022
14.0.9 634 7/23/2022
14.0.8 687 7/17/2022
14.0.5 752 7/11/2022
14.0.4 762 7/6/2022
14.0.3 696 7/2/2022
14.0.2 638 7/2/2022
14.0.0 640 6/25/2022
13.4.19 1,478 6/8/2022
13.4.0 1,167 5/31/2022
13.3.4 1,397 5/9/2022
13.3.1 718 5/1/2022
13.3.0 657 5/1/2022
13.2.0 1,117 4/21/2022
13.1.0 985 4/7/2022
13.0.0 715 4/5/2022
11.0.5 1,374 3/2/2022
11.0.4 715 2/22/2022
11.0.3 757 2/9/2022
11.0.2 771 2/6/2022
11.0.1 741 2/5/2022
10.0.21 724 1/28/2022
10.0.20 741 1/27/2022
10.0.19 727 1/23/2022
10.0.18 727 1/17/2022
10.0.16 721 1/2/2022
10.0.15 472 12/31/2021
10.0.14 508 12/28/2021
10.0.7 1,378 12/22/2021
10.0.5 710 12/18/2021
9.9.9 1,605 11/29/2021
9.9.3 863 11/9/2021
9.9.2 582 11/4/2021
9.9.0 690 10/30/2021
9.8.9 668 10/29/2021
9.8.7 633 10/27/2021
9.8.6 597 10/27/2021
9.8.5 660 10/26/2021
9.8.0 1,303 10/20/2021
9.7.9 599 10/19/2021
9.7.5 1,427 10/14/2021
9.7.0 817 10/9/2021
9.6.6 1,175 8/14/2021
9.2.0 6,112 5/26/2021
9.1.4 1,227 4/21/2021
9.1.0 1,006 4/14/2021
9.0.0 805 4/5/2021
8.9.9 987 3/30/2021
8.9.3 1,488 3/19/2021
8.9.2 976 1/29/2021
8.9.1 993 1/24/2021
8.9.0 1,042 1/22/2021
8.6.9 2,925 11/8/2020
8.6.6 1,967 11/2/2020
8.6.0 3,974 10/28/2020
8.5.0 1,874 10/23/2020
8.4.0 5,542 10/13/2020
8.3.2 1,937 10/8/2020
8.3.1 1,225 10/5/2020
8.3.0 1,181 10/3/2020
8.2.2 1,989 9/26/2020
8.2.1 1,322 9/25/2020
8.2.0 1,345 9/25/2020
8.1.17 6,635 9/13/2020
8.1.16 573 9/13/2020
8.1.15 1,866 9/12/2020
8.1.11 2,467 9/11/2020
8.1.10 1,266 9/6/2020
8.1.9 1,288 9/3/2020
8.1.8 1,239 9/2/2020
8.1.7 1,161 8/28/2020
8.1.4 1,188 8/25/2020
8.1.3 1,253 8/18/2020
8.1.2 1,160 8/16/2020
8.1.1 1,245 8/15/2020
8.0.2 470 8/11/2020
8.0.1 2,655 8/7/2020
8.0.0 1,190 8/7/2020
7.0.1 1,355 6/28/2020
7.0.0 1,198 6/28/2020
5.0.0 7,386 2/25/2020
4.0.4 7,865 1/27/2020
4.0.3 1,253 1/27/2020
4.0.2 1,368 1/16/2020
4.0.1 1,341 1/11/2020
4.0.0 1,320 1/5/2020
3.1.3 4,153 12/4/2019
3.1.2 1,366 11/30/2019
3.1.1 1,453 11/26/2019
3.1.0 581 11/19/2019