Article Jobs

Jobs are used to perform an action based on either a dynamic job that may schedule when an event fires, or based on a recurring schedule which executes based on a specified schedule.  When jobs are scheduled, the job server will pick up the jobs and execute them.

When Would I Create Dynamic Jobs

When you have a process that needs to occur based on another action occurring and you need the process to run in the background so it does not slow down the current process.  

When Would I Create Scheduled Jobs

When you have a process that needs to occur on a recurring basis.  

Creating dynamic jobs

To create a dynamic job you will need to implement the IEvolutionJob plugin interface (which requires a reference to Telligent.Evolution.Components.dll). IEvolutionJob extends the IPlugin to add support for executing jobs.   

In this sample we create a dynamic job called SampleJob which will parse the jobData data dictionary to extract the Id we will pass in and log an entry into the Event Log using the Id that was passed in.

using System;
using Telligent.Evolution.Extensibility.Api.Version1;
using Telligent.Evolution.Extensibility.Jobs.Version1;

namespace Samples
{
    public class SampleJob : IEvolutionJob
    {
        public void Execute(JobData jobData)
        {
            var eventLog = Telligent.Common.Services.Get<IEventLog>();
            Guid id = Guid.Empty;

            Guid.TryParse(jobData.Data["Id"], out id);

            eventLog.Write(
                string.Format("SampleJob has successfully run.  The Id {0} was passed into the jobData.", id), 
                new EventLogEntryWriteOptions 
                { 
                    Category = "Jobs", 
                    EventType = "Information" 
                });
        }
    }
}

Scheduling your custom dynamic job

Dynamic jobs are not scheduled to reoccur, however they do have to be scheduled in order to be picked up by the job server and execute.  To schedule the SampleJob you will need to call the JobService In-Process API (which requires a reference to Telligent.Evolution.Api.dll). 

The Execute method accepts the jobData parameter which is of the JobData type class.  The JobData class has Data property which is of the type Dictionary<string, string>.  You can populate the Data dictionary with data your job may need to run.  

In this sample, we are scheduling the SampleJob and passing in an Id for the SampleJob to use when it executes.

PublicApi.JobService.Schedule<SampleJob>(
    new Dictionary<string, string>() 
    { 
        { "Id", Guid.NewGuid().ToString() } 
    });

There is also a method overload of the Schedule method to allow for scheduling for a specified time to execute.  In the following sample we are scheduling the SampleJob to run in 15 minutes.

PublicApi.JobService.Schedule<SampleJob>(
    DateTime.UtcNow.AddMinutes(15), 
    new Dictionary<string, string>() 
    { 
        { "Id", Guid.NewGuid().ToString() }
    });

Creating recurring scheduled jobs

To recreate a custom scheduled job you will need to implement the IRecurringEvolutionJobPlugin plugin interface (which requires a reference to Telligent.Evolution.Components.dll).  The IRecurringEvolutionJobPlugin interface extends the IEvolutionJob interface to add support for scheduling jobs assigning a unique JobTypeId and specifying the context in which the job should run.

Jobs can be run in a background thread in the website or in the job service background process.  A job defines where it should be run by setting the SupportedContext property.  The value of the SupportedContext is a JobContext.  

The options for JobContext are:

  • Service: The job will be run from the background job service.
  • InProcess: The job will be run on each web node.

Here we set the SupportedContext property as JobContext.Service

public JobContext SupportedContext
{
    get { return JobContext.Service; }
}

A recurring job defines its default schedule using the DefaultSchedule property.  The schedule can be in intervals of seconds, minutes, hours, or on certain days at a specific time.  The JobSchedule class exposes various static methods that should be used to create a valid job schedule.

Here we set the DefaultSchedule to run every three minutes.

public JobSchedule DefaultSchedule
{
    get { return JobSchedule.EveryMinutes(3); }
}

In this complete sample of a recurring job the job will log an entry into the Event Log every time it runs.

using System;
using Telligent.Evolution.Extensibility.Api.Version1;
using Telligent.Evolution.Extensibility.Jobs.Version1;

namespace Samples
{
    public class SamplemRecurringJob : IRecurringEvolutionJobPlugin
    {
        public JobSchedule DefaultSchedule
        {
            get { return JobSchedule.EveryMinutes(3); }
        }

        public Guid JobTypeId
        {
            get { return new Guid("A5F40BA8-7D11-4429-AFE9-8238CE450B73"); }
        }

        public JobContext SupportedContext
        {
            get { return JobContext.Service; }
        }

        public void Execute(JobData jobData)
        {
            var eventLog = Telligent.Common.Services.Get<IEventLog>();

            eventLog.Write(
                "SampleRecurringJob has successfully run", 
                new EventLogEntryWriteOptions 
                { 
                    Category = "Jobs", 
                    EventType = "Information" 
                });
        }

        public string Description
        {
            get { return "This is a sample recurring job to demonstratoe how the IRecurringEvolutionJobPlugin works"; }
        }

        public void Initialize()
        {
        }

        public string Name
        {
            get { return "Sample Recurring Job"; }
        }
    }
}

Once this sample is deployed to Verint Community, the plugin will show in Administration > Jobs > Jobs.  On the Schedule tab, you have the option to change the default schedule.