コード例 #1
0
ファイル: MainService.cs プロジェクト: huzhao37/dtsc
        /// <summary>
        /// 监听redis_cmd
        /// </summary>
        /// <param name="nodeId">nodeId</param>
        private static void ListenCmd(int nodeId)
        {
            _redisProvider.DB = 0;
            var jobIds = _redisProvider.Keys("*");

            if (!jobIds?.Any() ?? false)
            {
                return;
            }
            jobIds.ForEach(jobId =>
            {
                if ((TbJob.Find("ID", jobId)?.NodeID ?? 0) != nodeId)
                {
                    return;
                }
                var cmd = _redisProvider.Get <string>(jobId, DataType.String).ToJsonEntity <TbCommand>();
                if (cmd == null)
                {
                    return;
                }
                JobHelper.Excute(cmd);
                cmd.Time = DateTime.Now.TimeSpan();
                if (cmd.Success != 1)
                {
                    return;
                }
                cmd.SaveAsync();
                _redisProvider.Delete(jobId);
            });
        }
コード例 #2
0
        public static string GetJobPath(int jobid)
        {
            var job = TbJob.Find("ID", jobid);

            if (job == null)
            {
                return(null);
            }
            return(Path.GetFullPath(JobPath) + "\\" + job.NodeID + "\\" + jobid);
        }
コード例 #3
0
ファイル: JobBase.cs プロジェクト: huzhao37/dtsc
        /// <summary>
        /// 当某个job超时时,它将被触发,可以发一些通知邮件等
        /// </summary>
        /// <param name="arg"></param>
        private void CancelOperation(object arg)
        {
            CancellationSource.Cancel();
            StdSchedulerFactory.GetDefaultScheduler().Result.Interrupt(new JobKey(JobName));

            new TbError()
            {
                Createtime = DateTime.Now.TimeSpan(),
                JobID      = TbJob.Find("Name", JobName)?.ID ?? 0,
                Msg        = ($"Warn:excute time out,has canceled,wait for next operate...")
            }.SaveAsync();
            Logger.Warn(JobName + " excute time out,has canceled,wait for next operate...");
        }
コード例 #4
0
        public static void DeleteDll(int jobid)
        {
            var job = TbJob.Find("ID", jobid);

            if (job == null)
            {
                return;
            }
            var path = Path.GetFullPath(JobPath) + "\\" + job.NodeID + "\\" + jobid;

            FileEx.DelectDir(path);
            //File.Delete(path);
        }
コード例 #5
0
ファイル: JobBase.cs プロジェクト: huzhao37/dtsc
        public Task Execute(IJobExecutionContext context)
        {
            Timer timer = null;

            try
            {
                timer = new Timer(CancelOperation, null, JobTimeout, Timeout.Infinite);
                Logger.Info("{0} Start Excute", context.JobDetail.Key.Name);
                if (context.JobDetail.JobDataMap != null)
                {
                    foreach (var pa in context.JobDetail.JobDataMap)
                    {
                        Logger.Info($"JobDataMap,key:{pa.Key},value:{pa.Value}");
                    }
                }
                var jobId = context.JobDetail.JobDataMap?["jobid"];
                if (jobId != null)
                {
                    JobId = Convert.ToInt32(jobId);
                }
                var job = TbJob.Find("ID", JobId);

                ExcuteJob(context, CancellationSource);

                if (job != null)
                {
                    job.Runcount++;
                    job.Lastedstart = Convert.ToInt64(context.FireTimeUtc.DateTime.ToLocalTime().TimeSpan());
                    job.Lastedend   = DateTime.Now.TimeSpan();
                    job.Nextstart   = Convert.ToInt64(context.NextFireTimeUtc?.DateTime.ToLocalTime().TimeSpan());
                    job.SaveAsync();
                }
            }
            catch (Exception ex)
            {
                new TbError()
                {
                    Createtime = DateTime.Now.TimeSpan(),
                    JobID      = TbJob.Find("Name", JobName)?.ID ?? 0,
                    Msg        = ($"error:{JobName}-{ex.Message}")
                }.SaveAsync();
            }
            finally
            {
                timer?.Dispose();
            }
            return(Task.CompletedTask);
        }
コード例 #6
0
        public static Type GetJobType(int jobid)
        {
            var job = TbJob.Find("ID", jobid);

            if (job == null)
            {
                return(null);
            }
            var path = Path.GetFullPath(JobPath) + "\\" + job.NodeID + "\\" + jobid + "\\" + job.Name + ".dll";

            if (!File.Exists(path))
            {
                Logger.Error($"请检查{job.Name}是否为实际的任务名称");
            }
            try
            {
                using (var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
                {
                    var asm = Assembly.Load(fs.ReadBytes());

                    foreach (var type in asm.GetTypes().Where(i => i.BaseType == typeof(JobBase)))
                    {
                        return(type);
                    }
                }
            }
            catch (Exception e)
            {
                new TbError()
                {
                    Createtime = DateTime.Now.TimeSpan(),
                    JobID      = jobid,
                    Msg        = $"Warn:{e.Message}"
                }.SaveAsync();

                Logger.Warn(e.Message);
                using (var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
                {
                    var asm = Assembly.Load(fs.ReadBytes());

                    foreach (var type in asm.GetTypes().Where(i => i.BaseType == typeof(JobBase)))
                    {
                        return(type);
                    }
                }
            }
            return(null);
        }
コード例 #7
0
        /// <summary>
        /// 监听Redis执行命令
        /// </summary>
        /// <param name="command"></param>
        public static void Excute(TbCommand command)
        {
            var success = 0;
            var job     = TbJob.Find("ID", command.Jobid);

            if (job == null)
            {
                return;
            }

            Type jobtype;
            var  type = (CommandType)command.Commandtype;

            switch (type)
            {
            case CommandType.Pause:
                jobtype = GetJobType(command.Jobid);
                if (jobtype == null)
                {
                    return;
                }
                PauseToQuartz(jobtype, job);
                job.State = 0;
                //job.Lastedend = DateTime.Now.TimeSpan();
                success = job.SaveAsync() ? 1 : 0;
                break;

            case CommandType.Delete:
                jobtype = GetJobType(command.Jobid);
                if (jobtype == null)
                {
                    return;
                }
                if (job.State == 0)
                {
                    DelteToQuartz(jobtype, job);
                }
                DeleteDll(job.ID);
                if (job.Delete() > 0)
                {
                    var zip = TbZip.Find("JobID", command.Jobid);
                    zip?.Delete();
                    success = 1;
                }
                break;

            case CommandType.Stop:
                jobtype = GetJobType(command.Jobid);
                if (jobtype == null)
                {
                    return;
                }
                DelteToQuartz(jobtype, job);
                job.State = 0;
                //job.Lastedend = DateTime.Now.TimeSpan();
                job.SaveAsync();
                success = 1;
                break;

            case CommandType.Start:
                if (job.State == 1)
                {
                    jobtype = GetJobType(command.Jobid);
                    PauseToQuartz(jobtype, job);
                    File.Delete(GetJobPath(job.ID) + "zip");
                }
                if (Dictionary.ContainsKey(job.Name))
                {
                    jobtype = GetJobType(command.Jobid);
                    ResumeToQuartz(jobtype, job);
                    job.State = 1;
                    //job.Runcount++;
                    //job.Lastedstart = DateTime.Now.TimeSpan();
                    success = job.SaveAsync() ? 1 : 0;
                    break;
                }
                var path = CreateFile(job.ID, job.NodeID);
                DownLoadZip(job.ID, path);
                var json = new Dictionary <string, object>();
                if (!job.Datamap.IsNullOrWhiteSpace())
                {
                    json = job.Datamap.ToJsonEntity <Dictionary <string, object> >();
                }
                if (!json.ContainsKey("jobid"))
                {
                    json.Add("jobid", job.ID);
                }
                JoinToQuartz(GetJobType(command.Jobid), job, json);
                job.State = 1;
                //job.Runcount++;
                //job.Lastedstart = DateTime.Now.TimeSpan();
                success = job.SaveAsync() ? 1 : 0;
                break;

            case CommandType.ReStart:
                //var jsons = new Dictionary<string, object>();
                //if (!job.Datamap.IsNullOrWhiteSpace())
                //    jsons = job.Datamap.ToJsonEntity<Dictionary<string, object>>();
                //if (!jsons.ContainsKey("jobid"))
                //    jsons.Add("jobid", job.ID);
                //jobtype = GetJobType(command.Jobid);
                //ResumeToQuartz(jobtype);
                //job.State = 1;
                //job.Runcount++;
                //job.Lastedstart = DateTime.Now.TimeSpan();
                //success = job.SaveAsync() ? 1 : 0;
                break;

            default:
                break;
            }
            command.Success = success;
        }