예제 #1
0
파일: JobInfo.cs 프로젝트: Jabe/cronshop
 internal static string BuildJobName(CronshopScript script, Type type)
 {
     return Path.GetFileNameWithoutExtension(script.FullPath) + @"." + type.Name;
 }
예제 #2
0
파일: JobInfo.cs 프로젝트: Jabe/cronshop
 public JobInfo(CronshopScript script, IJobDetail jobDetail, ICollection<ITrigger> triggers)
 {
     Script = script;
     JobDetail = jobDetail;
     Triggers = triggers;
 }
예제 #3
0
        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));
            }
        }
예제 #4
0
        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);
            }
        }
예제 #5
0
        private static Assembly LoadAssembly(CronshopScript script)
        {
            try
            {
                return CSScript.Load(script.FullPath);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            return null;
        }