Friday, February 15, 2013

Schedule job to check process running

In my day job I had to have a scheduled job to check whether an application was running. If it wasn't then it needs to be restarted.

As any developer would do I got into the rabbit hole fast, worrying about the threads/schedules etc. Alas Quartz.Net came to my rescue. You can find it at Quartz.Net.

Quartz.Net is a job scheduler for .Net based on Quartz (Java). It is a very flexible and comprehensive library. Download it and just add the Quartz.dll to your project from the release directory of the zip file.

Below are some sample code that might help you get started

Program.CS


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz.Impl;
using Quartz;
using Quartz.Core;
using Quartz.Job;
using Quartz.Simpl;
using Quartz.Util;

using System.Configuration;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create scheduler factory
            ISchedulerFactory sf = new StdSchedulerFactory();
            IScheduler sched = sf.GetScheduler();

            //Get interval you want to check
            String interval = ConfigurationManager.AppSettings["CheckInterval"];
            int checkInterval;

            //No AppSetting set it to a default value
            if (!int.TryParse(interval, out checkInterval))
                checkInterval = 60;
         
            // define the job and tie it to our Class1 class
            IJobDetail job = JobBuilder.Create().WithIdentity("Checker", "Group1").Build();
         
            // Trigger the job to run on the next round minute
            ITrigger trigger = TriggerBuilder.Create().WithIdentity("Trigger", "Group1").StartNow()
                                                      .WithDailyTimeIntervalSchedule(x => x.WithIntervalInSeconds(checkInterval))
                                                      .Build();

            // Tell quartz to schedule the job using our trigger
            sched.ScheduleJob(job, trigger);

            // Start up the scheduler (nothing can actually run until the
            // scheduler has been started)
            sched.Start();

            String quit = Console.ReadLine();
        }
    }
}

Class1.CS


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz.Impl;
using Quartz;
using System.Configuration;
using System.Diagnostics;

namespace ConsoleApplication1
{
    [PersistJobDataAfterExecution]
    class Class1:IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            //Example to check whether a application is running, use WebClient to check whether website is up.
            //it's up to you.
            String ProcessName = ConfigurationManager.AppSettings["ProcessName"];
            if (!String.IsNullOrEmpty(ProcessName))
            {
                var running = Process.GetProcesses().Where(prc => prc.ProcessName.ToLower().Contains(ProcessName)).FirstOrDefault();
                if (running == null)
                {
                    //Process is not running!! Call Mama!!
                    //Send a email
                    //Try running the process again
                    //Twilio integration for SMS!!
                    Console.Out.WriteLine(ProcessName + " is NOT Running!");
                }
            }
        }
    }
}

App.config

Your app.config should have two keys with "CheckInterval" and "ProcessName".


     
       
   
 
   
   
 



     
       
   
 
   
   
 


     
       
   
 
   
   
 




No comments: