コード例 #1
0
        public ConcurrentTaskExecution(IDistributedMutex distributedMutex, IKernel kernel, IRuntimeSettings settings)
        {
            _distributedMutex = distributedMutex;
            _kernel           = kernel;

            _defaultConfiguration = DefaultConfiguration(settings, out _preventConcurrentTaskExecutionOnAllTasks);
        }
コード例 #2
0
        public ConcurrentTaskExecutionResult Handle(ITask task, Arguments arguments, TaskLog log)
        {
            if (task == null)
            {
                throw new ArgumentNullException(nameof(task));
            }
            if (arguments == null)
            {
                throw new ArgumentNullException(nameof(arguments));
            }
            if (log == null)
            {
                throw new ArgumentNullException(nameof(log));
            }

            Type taskType = task.GetType();

            var preventConcurrent = taskType.GetCustomAttribute <PreventConcurrentTaskExecutionAttribute>();

            if (preventConcurrent == null)
            {
                var allowConcurrentTaskExecutionAttribute = taskType.GetCustomAttribute <AllowConcurrentTaskExecutionAttribute>();

                if (allowConcurrentTaskExecutionAttribute != null)
                {
                    return(ConcurrentTaskExecutionResult.Continue());
                }

                if (!_preventConcurrentTaskExecutionOnAllTasks)
                {
                    return(ConcurrentTaskExecutionResult.Continue());
                }
            }
            else
            {
                if (IsDisabled(task, arguments, preventConcurrent))
                {
                    return(ConcurrentTaskExecutionResult.Continue());
                }
            }

            string lockName        = GetLockName(task, arguments, preventConcurrent);
            string lockDescription = GetLockDescription(task, log, arguments, preventConcurrent);
            IPreventConcurrentTaskExecutionExceptionHandler exceptionHandler = GetExceptionHandler(task, preventConcurrent);

            try
            {
                DistributedMutexConfiguration configuration = preventConcurrent?.Configuration ?? _defaultConfiguration;

                var context = new DistributedMutexContext(lockName, configuration, log.LogMessage, lockDescription);

                IDisposable lockAcquired = _distributedMutex.Enter(context);

                return(ConcurrentTaskExecutionResult.Continue(lockAcquired));
            }
            catch (Exception ex)
            {
                if (exceptionHandler != null)
                {
                    ex = exceptionHandler.OnException(task, log, arguments, ex);

                    if (ex == null)
                    {
                        return(ConcurrentTaskExecutionResult.Stop());
                    }
                }

                throw new TaskExecutionLockNotAcquiredException($"Unable to acquire lock '{lockName}'.", ex);
            }
        }