internal static string BuildJobName(CronshopScript script, Type type) { return Path.GetFileNameWithoutExtension(script.FullPath) + @"." + type.Name; }
public JobInfo(CronshopScript script, IJobDetail jobDetail, ICollection<ITrigger> triggers) { Script = script; JobDetail = jobDetail; Triggers = triggers; }
private void ScheduleScript(CronshopScript script) { Assembly assembly = LoadAssembly(script); if (assembly == null) { return; } // find implementations of CronshopJob IEnumerable<Type> types = assembly.GetTypes() .Where(t => typeof (CronshopJob).IsAssignableFrom(t) && !t.IsAbstract); foreach (Type type in types) { JobConfigurator configurator; // create the instance to get the schedule using (var instance = (CronshopJob) Activator.CreateInstance(type)) { configurator = new JobConfigurator(); instance.Configure(configurator); } string name = JobInfo.BuildJobName(script, type); var detail = new JobDetailImpl(name, CronshopDefaultGroup, type) {Durable = true}; Scheduler.AddJob(detail, false); Console.WriteLine("ScheduleJob: " + detail.Key); var triggers = new List<ITrigger>(); foreach (string cron in configurator.Crons) { var trigger = new CronTriggerImpl(name + @"." + Guid.NewGuid(), CronshopDefaultGroup, cron) { JobKey = detail.Key, MisfireInstruction = MisfireInstruction.CronTrigger.DoNothing, }; triggers.Add(trigger); Scheduler.ScheduleJob(trigger); } _jobs.Add(detail.Key, new JobInfo(script, detail, triggers)); } }
private void UnscheduleScript(CronshopScript script) { KeyValuePair<JobKey, JobInfo>[] keys = _jobs .Where(x => x.Value.Script.FullPath == script.FullPath) .ToArray(); foreach (KeyValuePair<JobKey, JobInfo> tuple in keys) { bool success = Scheduler.DeleteJob(tuple.Value.JobDetail.Key); Console.WriteLine("DeleteJob: " + tuple.Value.JobDetail.Key + " " + (success ? "success" : "FAILED")); _jobs.Remove(tuple.Key); } }
private static Assembly LoadAssembly(CronshopScript script) { try { return CSScript.Load(script.FullPath); } catch (Exception e) { Console.WriteLine(e); } return null; }