Ejemplo n.º 1
0
        public async Task ExecuteJobAsync(ICronJob cronJob)
        {
            _logger.LogInformation($"Executing {cronJob}");
            await cronJob.ExecuteAsync(_token);

            _logger.LogInformation($"Finished {cronJob}");
        }
Ejemplo n.º 2
0
 public JobHandler(Guid jobId, ICronJob cronJob, string cronExpression, IServiceScopeFactory serviceScopeFactory)
 {
     JobId                = jobId;
     _cronJob             = cronJob;
     _serviceScopeFactory = serviceScopeFactory;
     _cronParser          = new CronParser(new CronDateTimeService());
     _cronExpression      = _cronParser.Parse(cronExpression);
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets an <see cref="EventLogAdapter"/> instance, writing to the "Application" event log with a source name of "NCron".
        /// </summary>
        /// <param name="job">The job which the log will be used with. This parameter is ignored.</param>
        /// <returns>An <see cref="EventLogAdapter"/>, writing to the "Application" event log with a source name of "NCron".</returns>
        public ILog GetLogForJob(ICronJob job)
        {
            var eventLog = new EventLog {
                Source = Bootstrap.ApplicationName
            };

            return(new EventLogAdapter(eventLog));
        }
Ejemplo n.º 4
0
 /// <summary>
 /// 移除任务
 /// </summary>
 /// <param name="job">被移除任务</param>
 /// <returns></returns>
 public bool RemoveJob(ICronJob job)
 {
     lock (JobLock)
     {
         job.abort();
         return(_cronJobs.Remove(job));
     }
 }
Ejemplo n.º 5
0
 private static bool GetJob(string className, out ICronJob job)
 {
     try
     {
         Type classType = Type.GetType(className, true);
         job = (ICronJob)Activator.CreateInstance(classType);
         return(true);
     }
     catch
     {
         Console.WriteLine("Invalid job name.");
         PrintUsage();
         job = null;
         return(false);
     }
 }
Ejemplo n.º 6
0
        public void AddCronJob(ICronJob cronJob)
        {
            if (cronJob == null)
            {
                throw new ArgumentNullException("cronJob");
            }

            if (cronJob.Enabled)
            {
                _recurringJobManager.AddOrUpdate(cronJob.Name,
                                                 () => cronJob.Execute(JobCancellationToken.Null),
                                                 cronJob.Schedule);
            }
            else
            {
                _recurringJobManager.RemoveIfExists(cronJob.Name);
            }
        }
Ejemplo n.º 7
0
        private async Task RunCronJob <T>() where T : ICronJob
        {
            using (var serviceScope = _serviceScopeFactory.CreateScope())
            {
                _cronJob = serviceScope.ServiceProvider.GetService <T>();

                while (!_cancellationTokenSource.Token.IsCancellationRequested)
                {
                    var timeToNext = _cronExpression.GetTimeToNext();

                    if (!timeToNext.HasValue)
                    {
                        return;
                    }

                    await Task.Delay((int)timeToNext.Value.TotalMilliseconds, _cancellationTokenSource.Token);

                    await _cronJob.RunJob(_cancellationTokenSource.Token);
                }
            }
        }
Ejemplo n.º 8
0
        private void ExecuteJob(ICronJob job)
        {
            using (var log = _logFactory.GetLogForJob(job))
            {
                var context = new CronContext(job, log);

                log.Info(() => String.Format("Executing job: {0}", job));

                // This inner try-catch serves to report ICronJob failures to the ILog.
                // Such exceptions are expected, and are thus handled seperately.
                try
                {
                    job.Initialize(context);
                    job.Execute();
                }
                catch (Exception exception)
                {
                    log.Error(() => String.Format("The job \"{0}\" threw an unhandled exception.", job),
                              () => exception);
                }
            }
        }
Ejemplo n.º 9
0
 public ILog GetLogForJob(ICronJob job)
 {
     var nlogger = LogManager.GetLogger(job.GetType().FullName);
     return new LogAdapter(nlogger);
 }
Ejemplo n.º 10
0
        public ILog GetLogForJob(ICronJob job)
        {
            var nlogger = LogManager.GetLogger(job.GetType().FullName);

            return(new LogAdapter(nlogger));
        }
Ejemplo n.º 11
0
        private void ExecuteJob(ICronJob job)
        {
            using (var log = _logFactory.GetLogForJob(job))
            {
                var context = new CronContext(job, log);

                log.Info(() => String.Format("Executing job: {0}", job));

                // This inner try-catch serves to report ICronJob failures to the ILog.
                // Such exceptions are expected, and are thus handled seperately.
                try
                {
                    job.Initialize(context);
                    InvokeJobStarted(new JobEventArgs(job));
                    job.Execute();
                    InvokeJobFinished(new JobEventArgs(job));
                }
                catch (Exception exception)
                {
                    log.Error(() => String.Format("The job \"{0}\" threw an unhandled exception.", job),
                              () => exception);
                }
            }
        }
Ejemplo n.º 12
0
 /// <summary>
 /// Gets an <see cref="EventLogAdapter"/> instance, writing to the "Application" event log with a source name of "NCron".
 /// </summary>
 /// <param name="job">The job which the log will be used with. This parameter is ignored.</param>
 /// <returns>An <see cref="EventLogAdapter"/>, writing to the "Application" event log with a source name of "NCron".</returns>
 public ILog GetLogForJob(ICronJob job)
 {
     var eventLog = new EventLog { Source = ApplicationInfo.ApplicationName };
     return new EventLogAdapter(eventLog);
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Creates a new <see cref="CronContext"/> with all properties explicitly set.
 /// </summary>
 /// <param name="job">The job being executed.</param>
 /// <param name="log">Tog to be used while executing the job in this context.</param>
 public CronContext(ICronJob job, ILog log)
 {
     Job = job;
     Log = log;
 }
Ejemplo n.º 14
0
 public void Add(ICronJob job)
 {
     job.JobExecuted  += Job_JobExecuted;
     job.JobExecuting += Job_JobExecuting;
     _cronJobs.Add(job);
 }
Ejemplo n.º 15
0
 public void Remove(ICronJob job)
 {
     _cronJobs.Remove(job);
 }
Ejemplo n.º 16
0
 public void AddJob(ICronJob Job)
 {
     CronJobs.Add(Job);
 }
Ejemplo n.º 17
0
        public NCron.Logging.ILog GetLogForJob(ICronJob job)
        {
            var internalLogger = LogManager.GetLogger(job.GetType());

            return(new LogAdapter(internalLogger));
        }
Ejemplo n.º 18
0
 public void AddJob(ICronJob job)
 {
     cron_jobs.Add (job);
 }
Ejemplo n.º 19
0
 public JobEventArgs(ICronJob job)
 {
     Job = job;
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Creates a new <see cref="CronContext"/> with all properties explicitly set.
 /// </summary>
 /// <param name="job">The job being executed.</param>
 /// <param name="log">Tog to be used while executing the job in this context.</param>
 public CronContext(ICronJob job, ILog log)
 {
     Job = job;
     Log = log;
 }
Ejemplo n.º 21
0
        protected Thread ExecuteTask(ICronJob cronjob, object parameter)
        {
            if (cronjob == null)
            {
                return(null);
            }

            try
            {
                switch (cronjob.CheckStatus((ISchemaInfo)parameter))
                {
                case EComponentStatus.DISABLED:
                {
                    if (LogLevel == ELogLevel.ALL)
                    {
                        MyLogger.LogText(string.Format("{0} DISABLED", cronjob.ID), "ServiceBase::ExecuteTask");
                    }
                    return(null);
                }

                case EComponentStatus.WORKING:
                {
                    if (LogLevel == ELogLevel.ALL || LogLevel == ELogLevel.WARN)
                    {
                        MyLogger.LogText(string.Format("{0} STILL BUSY", cronjob.ID), "ServiceBase::ExecuteTask");
                    }
                    return(null);
                }

                case EComponentStatus.UNAVAILABLE:
                {
                    if (LogLevel == ELogLevel.ALL)
                    {
                        MyLogger.LogText(string.Format("{0} OUT OF DATE ", cronjob.ID), "ServiceBase::ExecuteTask");
                    }
                    return(null);
                }

                case EComponentStatus.READY:
                {
                    if (LogLevel == ELogLevel.ALL || LogLevel == ELogLevel.WARN)
                    {
                        MyLogger.LogText(string.Format("{0} STARTED ", cronjob.ID), "ServiceBase::ExecuteTask");
                    }
#if DEBUG
                    cronjob.Run(parameter);
                    return(null);
#else
                    Thread taskthread = new Thread(new ParameterizedThreadStart(cronjob.Run));
                    taskthread.Start(parameter);
                    return(taskthread);
#endif
                }
                }

                return(null);
            }
            catch (Exception ex)
            {
                MyLogger.LogException(ex, "ServiceBase::ExecuteTask");
                return(null);
            }
        }