예제 #1
0
        public static void EnqueueBackgroundJob(
            [NotNull] this IStateMachine stateMachine,
            [NotNull] JobStorage storage,
            [NotNull] IStorageConnection connection,
            [NotNull] IWriteOnlyTransaction transaction,
            [NotNull] RecurringJobEntity recurringJob,
            [NotNull] BackgroundJob backgroundJob,
            [CanBeNull] string reason,
            [NotNull] IProfiler profiler)
        {
            if (stateMachine == null)
            {
                throw new ArgumentNullException(nameof(stateMachine));
            }
            if (storage == null)
            {
                throw new ArgumentNullException(nameof(storage));
            }
            if (connection == null)
            {
                throw new ArgumentNullException(nameof(connection));
            }
            if (transaction == null)
            {
                throw new ArgumentNullException(nameof(transaction));
            }
            if (recurringJob == null)
            {
                throw new ArgumentNullException(nameof(recurringJob));
            }
            if (backgroundJob == null)
            {
                throw new ArgumentNullException(nameof(backgroundJob));
            }
            if (profiler == null)
            {
                throw new ArgumentNullException(nameof(profiler));
            }

            var state = new EnqueuedState {
                Reason = reason
            };

            if (recurringJob.Queue != null)
            {
                state.Queue = recurringJob.Queue;
            }

            stateMachine.ApplyState(new ApplyStateContext(
                                        storage,
                                        connection,
                                        transaction,
                                        backgroundJob,
                                        state,
                                        null,
                                        profiler));
        }
예제 #2
0
 private static void ValidateCronExpression(string cronExpression)
 {
     try
     {
         RecurringJobEntity.ParseCronExpression(cronExpression);
     }
     catch (Exception ex)
     {
         throw new ArgumentException(
                   "CRON expression is invalid. Please see the inner exception for details.",
                   nameof(cronExpression),
                   ex);
     }
 }
예제 #3
0
        public static BackgroundJob TriggerRecurringJob(
            [NotNull] this IBackgroundJobFactory factory,
            [NotNull] JobStorage storage,
            [NotNull] IStorageConnection connection,
            [NotNull] IProfiler profiler,
            [NotNull] RecurringJobEntity recurringJob,
            DateTime now)
        {
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }
            if (storage == null)
            {
                throw new ArgumentNullException(nameof(storage));
            }
            if (connection == null)
            {
                throw new ArgumentNullException(nameof(connection));
            }
            if (profiler == null)
            {
                throw new ArgumentNullException(nameof(profiler));
            }
            if (recurringJob == null)
            {
                throw new ArgumentNullException(nameof(recurringJob));
            }

            var context = new CreateContext(storage, connection, recurringJob.Job, null, profiler);

            context.Parameters["RecurringJobId"] = recurringJob.RecurringJobId;
            context.Parameters["Time"]           = JobHelper.ToTimestamp(now);

            var backgroundJob = factory.Create(context);

            recurringJob.LastExecution = now;
            recurringJob.LastJobId     = backgroundJob?.Id;

            return(backgroundJob);
        }
예제 #4
0
        public static void UpdateRecurringJob(
            [NotNull] this IWriteOnlyTransaction transaction,
            [NotNull] RecurringJobEntity recurringJob,
            [NotNull] IReadOnlyDictionary <string, string> changedFields,
            [CanBeNull] DateTime?nextExecution,
            [CanBeNull] ILog logger)
        {
            if (transaction == null)
            {
                throw new ArgumentNullException(nameof(transaction));
            }
            if (recurringJob == null)
            {
                throw new ArgumentNullException(nameof(recurringJob));
            }
            if (changedFields == null)
            {
                throw new ArgumentNullException(nameof(changedFields));
            }

            if (changedFields.Count > 0)
            {
                transaction.SetRangeInHash($"recurring-job:{recurringJob.RecurringJobId}", changedFields);
            }

            var score = nextExecution.HasValue ? JobHelper.ToTimestamp(nextExecution.Value) : -1.0D;

            if (logger != null && logger.IsTraceEnabled())
            {
                logger.Trace($"Recurring job '{recurringJob.RecurringJobId}' is being updated. RecurringJob: ({recurringJob}), Changes: ({String.Join(";", changedFields.Select(x => $"{x.Key}:{x.Value}"))}), NextExecution: ({nextExecution})");
            }

            transaction.AddToSet(
                "recurring-jobs",
                recurringJob.RecurringJobId,
                score);
        }