SaaS CMS has officially launched! Learn more now.

Calling a Service

Vote:
 

I created a new Service class and its interface -> ScheduledJobService ,IScheduledJobService

-this service contains methods to send mail when faces any exception while running a job

-Thera are many jobs and it is not convinient that calling this method in every job

-Is there any common class to all jobs so that implementation easy

#312928
Nov 22, 2023 7:17
Vote:
 

I suspect what you're looking for is IScheduledJobEvents. This interface allows you to hook in to the scheduled job's "Executed" event and check whether it was successful. I've not tried this on CMS 12 but a CMS 11 implementation would look something like this:

[InitializableModule]
public class ScheduledJobCompletionInitialisation : IInitializableModule
{
    private IScheduledJobEvents _scheduledJobEvents;

    public void Initialize(InitializationEngine context)
    {
        //Get reference to an instance of IScheduledJobEvents
        _scheduledJobEvents = context.Locate.Advanced.GetInstance<IScheduledJobEvents>();

        //Attach to scheduled job executed event
        _scheduledJobEvents.Executed += executedScheduledJob;
    }

    private void executedScheduledJob(object sender, ScheduledJobEventArgs e)
    {
        //Check whether job failed and, if so, send email
        if (e.Job.HasLastExecutionFailed) {
                var jobName = e.Job.Name;
                var errorMessage = e.Job.LastExecutionMessage;
                //send email here
        }
    }

    public void Uninitialize(InitializationEngine context)
    {
        //Clean up event
        _scheduledJobEvents.Executed -= executedScheduledJob;
    }
}
#312934
Nov 22, 2023 9:19
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.