I would like to have my custom Select Service Package do something repeatedly on a schedule. Based on the SDK documentation it seems like SoftPro.Select.Service.Scheduler and/or SoftPro.Select.Service.Tasks will provide this type of behavior. Which, if either, should I be looking at and if so how do I register my "job" with the service?
Thanks!
-Matt
Running Scheduled Tasks in Service Package
-
- Posts: 411
- Joined: Thu Sep 11, 2008 11:35 am
- Location: Raleigh, NC, USA
- Contact:
Re: Running Scheduled Tasks in Service Package
The Task classes you're seeing are NOT part of our scheduler. So you can ignore those. They provide scratch storage space for worker threads.
In order to create a scheduled item, you need to first define the trigger handler. This is done using the ITriggerHandler interface. Once you have a handler defined, your package needs to register the handler with the system using the ITriggerHandlerManager service during its OnInitialize() routine. The trigger handler is responsible for starting your custom unit of work.
To set the schedule for your trigger handler, you now create a scheduled trigger using the ITriggerManager::NewScheduledTrigger() method. This method takes a reference to your trigger handler. It returns a new, unsaved scheduled trigger. Set the properties on the IScheduledTrigger object accordingly and then apply the changes to save it.
Now, when the scheduled trigger fires, it will find your handler and call its Execute method.
In order to create a scheduled item, you need to first define the trigger handler. This is done using the ITriggerHandler interface. Once you have a handler defined, your package needs to register the handler with the system using the ITriggerHandlerManager service during its OnInitialize() routine. The trigger handler is responsible for starting your custom unit of work.
To set the schedule for your trigger handler, you now create a scheduled trigger using the ITriggerManager::NewScheduledTrigger() method. This method takes a reference to your trigger handler. It returns a new, unsaved scheduled trigger. Set the properties on the IScheduledTrigger object accordingly and then apply the changes to save it.
Now, when the scheduled trigger fires, it will find your handler and call its Execute method.
Code: Select all
public class Package : SoftPro.Select.Service.Package
{
protected override OnIntialize()
{
//create your handler
ITriggerHandler myhandler = new MyTriggerHandler(this);
//register your trigger handler
ITriggerHandlerManager thm = GetService<ITriggerHandlerManager>();
thm.Register(myhandler);
//SAMPLE - create a schedule to run every 10 mins
//We only want to really do this ONCE. The trigger is persisted in the db, so we don't want to do this
//on every server restart.
if (!DoneBefore())
{
ITriggerManager tm = GetService<ITriggerManager>();
IScheduledTrigger trigger = tm.NewScheduledTrigger(myhandler);
trigger.Recurrence = ScheduledTriggerRecurrence.Once;
trigger.RepeatEnabled = true;
trigger.RepeatPeriod = TimeSpan.FromMinutes(10);
trigger.Scope = ScheduledTriggerScope.Global //this ensures that the handler is only running one instance at a time in a pooled environment
tm.ApplyChanges(trigger);
}
}
private bool DoneBefore()
{
//this is the real tricky part. you have to come up with a way to know that you have previously created a trigger already and then
//avoid creating ANOTHER trigger. That would create two items getting executed and so on.
//you could use your own db table for this purpose.
//for demo purposes, I am always return false - BE CAREFUL!! Don't do that in production!!
return false;
}
}
private class MyTriggerHandler : SoftPro.Select.Servicer.Runtime.TriggerHandler
{
public MyTriggerHandler(IServiceProvider site) : base(site)
{
}
protected override void OnExecute(ITrigger trigger, TriggerEventArgs args)
{
//do you custom work here
}
}
John Morris
Sr. Software Architect
SoftPro
Sr. Software Architect
SoftPro
Re: Running Scheduled Tasks in Service Package
Is it possible to set a specific time of the day that the scheduled task will run or by using 'time' am I setting it to one specific instance in time. Currently I have:
However, not sure how this sets the first occurrence of the scheduled task.
Code: Select all
trigger.Recurrence = ScheduledTriggerRecurrence.Daily;
trigger.RepeatEnabled = true;
trigger.RepeatPeriod = TimeSpan.FromHours(12);
-
- Posts: 23
- Joined: Wed Jul 12, 2023 11:29 am
Re: Running Scheduled Tasks in Service Package
The IScheduledTrigger object has a property. Have you tried using that?