Exemple #1
0
        private async Task Start(JobTaskView view, TaskLoadContext lc)
        {
            //throw

            //在应用程序域中创建实例返回并保存在job中,这是最终调用任务执行的实例
            TaskBase instance = AssemblyHelper.CreateTaskInstance(lc, view.JobTask.Id, view.JobTask.AssemblyName, view.JobTask.ClassName);

            if (instance == null)
            {
                throw new InvalidCastException($"任务实例创建失败,请确认目标任务是否派生自TaskBase类型。程序集:{view.JobTask.AssemblyName},类型:{view.JobTask.ClassName}");
            }
            // instance.logger = new LogWriter(); ;
            JobDataMap map = new JobDataMap
            {
                new KeyValuePair <string, object> ("domain", lc),
                new KeyValuePair <string, object> ("instance", instance),
                new KeyValuePair <string, object> ("name", view.JobTask.Title),
                new KeyValuePair <string, object> ("params", ConvertParamsJson(view.JobTask.CustomParamsJson)),
                new KeyValuePair <string, object> ("keepers", view.Keepers),
                new KeyValuePair <string, object> ("children", view.Children)
            };

            try
            {
                IJobDetail job = JobBuilder.Create <RootJob>()
                                 .WithIdentity(view.JobTask.Id.ToString())
                                 .UsingJobData(map)
                                 .Build();

                //添加触发器
                var listener = new JobRunListener(view.JobTask.Id.ToString());
                listener.OnSuccess += StartedEventAsync;
                _scheduler.ListenerManager.AddJobListener(listener, KeyMatcher <JobKey> .KeyEquals(new JobKey(view.JobTask.Id.ToString())));

                if (!CronExpression.IsValidExpression(view.JobTask.CronExpression))
                {
                    throw new Exception("cron表达式验证失败");
                }
                CronTriggerImpl trigger = new CronTriggerImpl
                {
                    CronExpressionString = view.JobTask.CronExpression,
                    Name        = view.JobTask.Title,
                    Key         = new TriggerKey(view.JobTask.Id.ToString()),
                    Description = view.JobTask.Remark
                };

                await _scheduler.ScheduleJob(job, trigger);
            }
            catch (Exception ex)
            {
                throw new SchedulerException(ex);
            }
            _logger.LogInformation($"任务[{view.JobTask.Title}]启动成功!", view.JobTask.Id);
        }
        public void LoadTest()
        {
            Guid sid = Guid.Parse("3c915575-7171-4d95-b08e-9f082b3f58b0");

            var loadContext = AssemblyHelper.LoadAssemblyContext(sid, "Hos.ScheduleMaster.Demo");

            Assert.True(loadContext != null);

            var instance = AssemblyHelper.CreateTaskInstance(loadContext, sid, "Hos.ScheduleMaster.Demo", "Hos.ScheduleMaster.Demo.Simple");

            Assert.True(instance != null);
        }
 public void CreateRunnableInstance(ScheduleContext context)
 {
     loadContext      = AssemblyHelper.LoadAssemblyContext(context.Schedule.Id, context.Schedule.AssemblyName);
     RunnableInstance = AssemblyHelper.CreateTaskInstance(
         loadContext,
         context.Schedule.Id,
         context.Schedule.AssemblyName,
         context.Schedule.ClassName
         );
     if (RunnableInstance == null)
     {
         throw new InvalidCastException($"任务实例创建失败,请确认目标任务是否派生自TaskBase类型。程序集:{context.Schedule.AssemblyName},类型:{context.Schedule.ClassName}");
     }
 }