Ask or search…
K
Links

Extending - Threads

Let's take a look at how you might extend Perigee to fit your companies very specific needs. Although we ship with many different connectors, watchers, agents, and synchronization features you may want to add something to the Perigee core system. It is very easy to implement custom threads. This is one the reasons Perigee is so powerful as we don't lock you out of adding and customizing the application.
There are two types of custom threads you can add where both have different execution rules as they allow you create any additional functionality needed for your specific requirements.

Managed Thread

The first type is a managed thread. The ManagedThread calls the callback method when it is started, and doesn't expect that method to return, throw an exception, or exit until the cancellation token is canceled.
When the callback does exit before token cancelation, it will restart the thread a second later expecting the process to start again. If an exception is thrown and is uncaught within the callback, it will be caught in the ManagedThread, and the ExceptionRestartTime property of the manager will be awaited before restarting.
Below is a fully working extension that adds a new method to call from TaskConfig.
public static ThreadRegistry AddCustomXYZFunction(this ThreadRegistry tr)
{
//Create a regular managed thread (not an expression, CRON)
var TM = new ManagedThread("CustomXYZFunction", (ct, l) => {
//The callback method is here...
do
{
//An example of repeating something every second, and exiting the thread when the token is cancelled.
//Doing a long process that can be stopped safely? Pay attention to the cancellation token, and kindly exit when a graceful shutdown is requested.
if (ct.IsCancellationRequested)
{
//Save or finish or persist information.
return;
}
}
while (PerigeeApplication.delayOrCancel(1000, ct));
}, tr.CTS, tr.GetLogger<Program>(), started: true);
//Set additional options
TM.ExceptionRestartTime = TimeSpan.FromSeconds(30);
//Add the thread to the management system
tr.AddManagedThread(TM);
return tr;
}
Then to use this custom method on startup.
PerigeeApplication.ApplicationNoInit("DemoApp", (c) => {
c.AddCustomXYZFunction();
});

Expression Thread

This is nearly identical to the managed thread, however it's execution is built on a CRON expression. This changes the way the callback functions slightly.
The Callback Method is ONLY called the expression is at execution time. This means the process is expected to kick off, and end by returning out of the method.
If an exception is thrown, it will be caught in ManagedThread, but no additional delay is added as the next execution period of the CRON expression will simply call the callback again.
The takeaway about the two types:
Expression Threads: Get called, expect to exit.
Managed Threads: Get called, don't expect to exit, and will trigger the restart functionality on uncaught exceptions.