public void OnPerforming(PerformingContext filterContext)
        {
            var resource = String.Format(
                "{0}.{1}",
                filterContext.Job.Type.FullName,
                filterContext.Job.Method.Name);

            var timeout = TimeSpan.FromSeconds(_timeoutInSeconds);

            var distributedLock = filterContext.Connection.AcquireDistributedLock(resource, timeout);
            filterContext.Items["DistributedLock"] = distributedLock;
        }
Пример #2
0
        private static void PerformJobWithFilters(
            PerformContext context,
            IJobPerformer performer,
            IEnumerable <IServerFilter> filters)
        {
            var preContext = new PerformingContext(context);
            Func <PerformedContext> continuation = () =>
            {
                performer.Perform(context.Activator, context.CancellationToken);
                return(new PerformedContext(context, false, null));
            };

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

            thunk();
        }
Пример #3
0
        private static void PerformJobWithFilters(
            PerformContext context,
            IJobPerformer performer,
            IEnumerable<IServerFilter> filters)
        {
            var preContext = new PerformingContext(context);
            Func<PerformedContext> continuation = () =>
            {
                performer.Perform(context.Activator, context.CancellationToken);
                return new PerformedContext(context, false, null);
            };

            var thunk = filters.Reverse().Aggregate(continuation,
                (next, filter) => () => InvokePerformFilter(filter, preContext, next));
            
            thunk();
        }
Пример #4
0
        public void OnPerforming(PerformingContext filterContext)
        {
            var cultureName = filterContext.GetJobParameter<string>("CurrentCulture");
            var uiCultureName = filterContext.GetJobParameter<string>("CurrentUICulture");

            var thread = Thread.CurrentThread;
            
            if (!String.IsNullOrEmpty(cultureName))
            {
                filterContext.Items["PreviousCulture"] = thread.CurrentCulture;
                thread.CurrentCulture = CultureInfo.GetCultureInfo(cultureName);
            }

            if (!String.IsNullOrEmpty(uiCultureName))
            {
                filterContext.Items["PreviousUICulture"] = thread.CurrentUICulture;
                thread.CurrentUICulture = CultureInfo.GetCultureInfo(uiCultureName);
            }
        }
        public PreserveCultureAttributeFacts()
        {
            _connection = new Mock<IStorageConnection>();
            var job = Job.FromExpression(() => Sample());
            var state = new Mock<IState>();
            var stateMachineFactory = new Mock<IStateMachineFactory>();

            var createContext = new CreateContext(
                _connection.Object, stateMachineFactory.Object, job, state.Object);
            _creatingContext = new CreatingContext(createContext);
            _createdContext = new CreatedContext(createContext, false, null);

            var workerContext = new WorkerContextMock();

            var performContext = new PerformContext(
                workerContext.Object, _connection.Object, JobId, job, DateTime.UtcNow, new Mock<IJobCancellationToken>().Object);
            _performingContext = new PerformingContext(performContext);
            _performedContext = new PerformedContext(performContext, false, null);
        }
Пример #6
0
        private static PerformedContext InvokePerformFilter(
            IServerFilter filter,
            PerformingContext preContext,
            Func <PerformedContext> continuation)
        {
            try
            {
                filter.OnPerforming(preContext);
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception filterException)
            {
                throw new JobPerformanceException(
                          "An exception occurred during execution of one of the filters",
                          filterException);
            }

            if (preContext.Canceled)
            {
                return(new PerformedContext(
                           preContext, true, null));
            }

            var wasError = false;
            PerformedContext postContext;

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

                try
                {
                    filter.OnPerformed(postContext);
                }
                catch (Exception filterException)
                {
                    throw new JobPerformanceException(
                              "An exception occurred during execution of one of the filters",
                              filterException);
                }

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

            if (!wasError)
            {
                try
                {
                    filter.OnPerformed(postContext);
                }
                catch (OperationCanceledException)
                {
                    throw;
                }
                catch (Exception filterException)
                {
                    throw new JobPerformanceException(
                              "An exception occurred during execution of one of the filters",
                              filterException);
                }
            }

            return(postContext);
        }
Пример #7
0
        private static PerformedContext InvokePerformFilter(
            IServerFilter filter, 
            PerformingContext preContext,
            Func<PerformedContext> continuation)
        {
            try
            {
                filter.OnPerforming(preContext);
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception filterException)
            {
                throw new JobPerformanceException(
                    "An exception occurred during execution of one of the filters",
                    filterException);
            }
            
            if (preContext.Canceled)
            {
                return new PerformedContext(
                    preContext, true, null);
            }

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

                try
                {
                    filter.OnPerformed(postContext);
                }
                catch (Exception filterException)
                {
                    throw new JobPerformanceException(
                        "An exception occurred during execution of one of the filters",
                        filterException);
                }

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

            if (!wasError)
            {
                try
                {
                    filter.OnPerformed(postContext);
                }
                catch (OperationCanceledException)
                {
                    throw;
                }
                catch (Exception filterException)
                {
                    throw new JobPerformanceException(
                        "An exception occurred during execution of one of the filters",
                        filterException);
                }
            }

            return postContext;
        }