コード例 #1
0
ファイル: JobCreationProcess.cs プロジェクト: hahmed/HangFire
        private static void CreateWithFilters(
            CreateContext context,
            IEnumerable<IClientFilter> filters)
        {
            var preContext = new CreatingContext(context);
            Func<CreatedContext> continuation = () =>
            {
                context.CreateJob();
                return new CreatedContext(context, false, null);
            };

            var thunk = filters.Reverse().Aggregate(continuation,
                (next, filter) => () => InvokeClientFilter(filter, preContext, next));

            thunk();
        }
コード例 #2
0
        public PreserveCultureAttributeFacts()
        {
            _connection = new Mock <IStorageConnection>();
            var job   = Job.FromExpression(() => Sample());
            var state = new Mock <IState>();

            var createContext = new CreateContext(
                _connection.Object, job, state.Object);

            _creatingContext = new CreatingContext(createContext);

            var workerContext = new WorkerContextMock();

            var performContext = new PerformContext(
                workerContext.Object, _connection.Object, JobId, job, DateTime.UtcNow, new Mock <IJobExecutionContext>().Object);

            _performingContext = new PerformingContext(performContext);
            _performedContext  = new PerformedContext(performContext, null, false, null);
        }
        public void OnCreating(CreatingContext filterContext)
        {
            var now  = DateTimeOffset.Now;
            var data = filterContext.Job.Args.OfType <HangfireRecurringScheduledMessageData>().Single();

            if (data.StartTime > now)
            {
                filterContext.Canceled = true;
            }
            else if (data.EndTime.HasValue && data.EndTime.Value < now)
            {
                filterContext.Canceled = true;
                if (!filterContext.Parameters.TryGetValue("RecurringJobId", out var recurringJobId))
                {
                    return;
                }
                RecurringJob.RemoveIfExists((string)recurringJobId);
            }
        }
        public void OnCreating(CreatingContext filterContext)
        {
            if (filterContext == null)
            {
                throw new ArgumentNullException(nameof(filterContext));
            }
            var recurringJobId = filterContext.GetJobParameter <string>("RecurringJobId");

            if (!string.IsNullOrWhiteSpace(recurringJobId))
            {
                var recurringJob = filterContext.Connection.GetAllEntriesFromHash("recurring-job:" + recurringJobId);
                var isPauseState = recurringJob.ContainsKey("PauseState") ? SerializationHelper.Deserialize <bool>(recurringJob["PauseState"]) : false;

                if (isPauseState)
                {
                    filterContext.SetJobParameter("PauseState", isPauseState);
                    filterContext.Canceled = true;
                }
            }
        }
コード例 #5
0
        private static void CancelJobIfAlreadyInQueue(CreatingContext context, string methodName, bool varyByArgs)
        {
            if (context.InitialState == null)
            {
                return;
            }

            var queueName      = ((EnqueuedState)context.InitialState).Queue;
            var api            = context.Storage.GetMonitoringApi();
            var count          = api.EnqueuedCount(queueName);
            var jobs           = api.EnqueuedJobs(queueName, 0, (int)count);
            var currentJobHash = GetArgssHash(context.Job);

            if (jobs.Any(j =>
            {
                var jv = j.Value; return(jv.Job.Method.Name == methodName && jv.InEnqueuedState && (!varyByArgs || GetArgssHash(jv.Job) == currentJobHash));
            }))
            {
                context.Canceled = true;
            }
        }
コード例 #6
0
        public PreserveCultureAttributeFacts()
        {
            _connection = new Mock <IStorageConnection>();

            var storage       = new Mock <JobStorage>();
            var backgroundJob = new BackgroundJobMock {
                Id = JobId
            };
            var state = new Mock <IState>();

            var createContext = new CreateContext(
                storage.Object, _connection.Object, backgroundJob.Job, state.Object);

            _creatingContext = new CreatingContext(createContext);

            var performContext = new PerformContext(
                _connection.Object, backgroundJob.Object, new Mock <IJobCancellationToken>().Object);

            _performingContext = new PerformingContext(performContext);
            _performedContext  = new PerformedContext(performContext, null, false, null);
        }
コード例 #7
0
        public void OnCreating(CreatingContext context)
        {
            if (!(context.InitialState is EnqueuedState))
            {
                return;
            }

            try
            {
                var key = GetFingerprintWithPrefix(context.Job, LockKeyPrefix);
                Logger.TraceFormat("Acquring a lock for {key}", key);
                using (context.Connection.AcquireDistributedLock(key, s_lockTimeout))
                {
                    var timestamp = GetTimestamp(context.Connection, context.Job);

                    if (TimestampInWindow(timestamp, _seconds))
                    {
                        Logger.TraceFormat("cancel the job.");
                        context.Canceled = true;
                    }
                    else
                    {
                        // Set the timestamp - this will add the lock key, or update
                        // and extend the lock.
                        context.Connection.SetRangeInHash(GetFingerprintWithPrefix(context.Job, FingerprintPrefix), new Dictionary <string, string>
                        {
                            { TimestampKey, DateTimeOffset.UtcNow.ToString("o") }
                        });
                    }
                }
            }
            catch
            {
                // continue from the exception
            }
        }
コード例 #8
0
 /// <summary>Evento antes de registrar uma ação(client)</summary>
 /// <param name="context">Contexto do evento</param>
 public void OnCreating(CreatingContext context)
 {
     Logger.LogInformation($"Creating a job based on method `{context.Job.Method.Name}`...");
 }
 internal static CreateContext SetBatchParameter(this CreatingContext ctx, string name, object value)
 {
     SetBatchParameterImpl(name, value, ctx);
     return(ctx);
 }
コード例 #10
0
 public void OnCreating(CreatingContext context)
 {
     Console.WriteLine("Creating a job based on method `{0}`...", context.Job.Method.Name);
 }
コード例 #11
0
 public void OnCreating(CreatingContext context)
 {
     Logger.InfoFormat("Creating a job based on method `{0}`...", context.Job.Method.Name);
 }
コード例 #12
0
 public void OnCreating(CreatingContext filterContext)
 {
     logger.Info($"[OnCreating] Job.Method.Name:{filterContext.Job.Method.Name}");
 }
コード例 #13
0
 public void OnCreating(CreatingContext context)
 {
     _hangfireLogger.InfoFormat("Creating a job based on method {0}...", context.Job.Method.Name);
 }
コード例 #14
0
 public void OnCreating(CreatingContext filterContext)
 {
     logger.DebugFormat("[OnCreated] Job.Method.Name: `{0}`", filterContext.Job.Method.Name);
 }
コード例 #15
0
        public void OnCreating(CreatingContext filterContext)
        {
            logger.Info($"[OnCreating] Job.Method.Name:{filterContext.Job.Method.Name}");

            filterContext.SetJobParameter("expireIn", TimeSpan.FromMinutes(5));
        }
コード例 #16
0
 public void OnCreating(CreatingContext context)
 {
     Logger.InfoFormat($"Creating a job based on method `{context.Job?.Method?.Name}`...");
 }
コード例 #17
0
 public void OnCreating(CreatingContext filterContext)
 {
     Logger.InfoFormat($"Creating a job based on method `{filterContext.Job.Method.Name}`...");
 }
コード例 #18
0
 public void OnCreating(CreatingContext context)
 {
     // log to secondary database..
 }
コード例 #19
0
 public void OnCreating(CreatingContext filterContext)
 {
     filterContext.SetJobParameter(JobParameterKey, this.Queue);
 }
コード例 #20
0
ファイル: JobCreationProcess.cs プロジェクト: hahmed/HangFire
        private static CreatedContext InvokeClientFilter(
            IClientFilter filter,
            CreatingContext preContext,
            Func<CreatedContext> continuation)
        {
            filter.OnCreating(preContext);
            if (preContext.Canceled)
            {
                return new CreatedContext(
                    preContext, true, null);
            }

            var wasError = false;
            CreatedContext postContext;
            try
            {
                postContext = continuation();
            }
            catch (Exception ex)
            {
                wasError = true;
                postContext = new CreatedContext(
                    preContext, false, ex);

                filter.OnCreated(postContext);

                if (!postContext.ExceptionHandled)
                {
                    throw;
                }
            }

            if (!wasError)
            {
                filter.OnCreated(postContext);
            }

            return postContext;
        }
コード例 #21
0
 public void OnCreating(CreatingContext filterContext)
 {
     _pusher.NotifyQueueItemsChanged();
 }
コード例 #22
0
 public void OnCreating(CreatingContext filterContext)
 {
 }
コード例 #23
0
 public void OnCreating(CreatingContext filterContext)
 {
     filterContext.SetJobParameter("contextSnapshot", _context.Peek());
 }