Exemplo n.º 1
0
        /// <summary>
        /// Adds the task
        /// </summary>
        /// <typeparam name="TTransportInit">The type of the transport initialize.</typeparam>
        /// <typeparam name="TQueue">The type of the queue.</typeparam>
        /// <param name="name">The name.</param>
        /// <param name="queue">The queue.</param>
        /// <param name="connection">The connection.</param>
        /// <param name="schedule">The schedule.</param>
        /// <param name="autoRun">if set to <c>true</c> [automatic run].</param>
        /// <param name="window">The window.</param>
        /// <param name="actionToRun">The action to run.</param>
        /// <param name="expressionToRun">The expression to run.</param>
        /// <param name="route">The route.</param>
        /// <param name="rawExpression">if set to <c>true</c> this expression will not be serialized. This will fail unless an in-process queue is being used.</param>
        /// <param name="producerConfiguration">The producer configuration.</param>
        /// <returns></returns>
        /// <exception cref="JobSchedulerException">Cannot add a task after Shutdown has been called.</exception>
        private ScheduledJob AddTaskImpl <TTransportInit, TQueue>(
            string name,
            string queue,
            string connection,
            IJobSchedule schedule,
            bool autoRun,
            TimeSpan window,
            Expression <Action <IReceivedMessage <MessageExpression>, IWorkerNotification> > actionToRun,
            LinqExpressionToRun expressionToRun,
            string route,
            bool rawExpression,
            Action <QueueProducerConfiguration> producerConfiguration = null)
            where TTransportInit : ITransportInit, new()
            where TQueue : class, IJobQueueCreation
        {
            Guard.NotNull(() => schedule, schedule);
            Guard.NotNullOrEmpty(() => name, name);


            ScheduledJob job;

            lock (_lockTasks)
            {
                if (IsShuttingDown)
                {
                    throw new JobSchedulerException("Cannot add a task after Shutdown has been called.");
                }

                if (_tasks.ContainsKey(name))
                {
                    RemoveJob(name);
                }
                if (expressionToRun != null)
                {
                    job = new ScheduledJob(this, name, schedule, _jobQueue.Get <TTransportInit, TQueue>(queue, connection, producerConfiguration), expressionToRun, _getTime.Create(), route)
                    {
                        Window     = window,
                        IsAttached = true
                    };
                    _tasks.Add(name, job);
                }
                else
                {
                    job = new ScheduledJob(this, name, schedule, _jobQueue.Get <TTransportInit, TQueue>(queue, connection, producerConfiguration), actionToRun, _getTime.Create(), route, rawExpression)
                    {
                        Window     = window,
                        IsAttached = true
                    };
                    _tasks.Add(name, job);
                }
            }

            job.OnException += TaskOnOnException;
            job.OnEnQueue   += JobOnOnEnQueue;
            job.OnNonFatalFailureEnQueue += JobOnOnNonFatalFailureEnQueue;
            if (autoRun)
            {
                job.StartSchedule();
            }

            return(job);
        }