Beispiel #1
0
        /// <summary>
        /// Adds the task.
        /// </summary>
        /// <typeparam name="TTransportInit">The type of the transport initialize.</typeparam>
        /// <param name="jobQueueCreation">The job queue creation.</param>
        /// <param name="name">The name.</param>
        /// <param name="queueConnection">Queue and connection information.</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>(
            IJobQueueCreation jobQueueCreation,
            string name,
            QueueConnection queueConnection,
            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()
        {
            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>(jobQueueCreation, queueConnection, producerConfiguration), expressionToRun, _getTime.Create(), route)
                    {
                        Window     = window,
                        IsAttached = true
                    };
                    _tasks.Add(name, job);
                }
                else
                {
                    job = new ScheduledJob(this, name, schedule, _jobQueue.Get <TTransportInit>(jobQueueCreation, queueConnection, 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);
        }
        /// <summary>
        /// Gets the specified queue.
        /// </summary>
        /// <typeparam name="TTransportInit">The type of the transport initialize.</typeparam>
        /// <param name="jobQueueCreation">The job queue creation.</param>
        /// <param name="queueConnection">Queue and connection information.</param>
        /// <param name="producerConfiguration">The producer configuration.</param>
        /// <returns></returns>
        /// <exception cref="DotNetWorkQueueException">Failed to create the queue. The error message is {createResult.ErrorMessage}</exception>
        public IProducerMethodJobQueue Get <TTransportInit>(IJobQueueCreation jobQueueCreation, QueueConnection queueConnection, Action <QueueProducerConfiguration> producerConfiguration = null)
            where TTransportInit : ITransportInit, new()
        {
            var connectionInfo = new BaseConnectionInformation(queueConnection);

            if (_queues.ContainsKey(connectionInfo))
            {
                return(_queues[connectionInfo]);
            }

            var transportName = typeof(TTransportInit).ToString();

            if (!_containers.ContainsKey(transportName))
            {
                var container = new QueueContainer <TTransportInit>(_registrations.QueueRegistrations, _registrations.QueueOptions);
                if (!_containers.TryAdd(transportName, container))
                {
                    container.Dispose();
                }
            }

            if (!_queues.ContainsKey(connectionInfo))
            {
                var createResult = jobQueueCreation.CreateJobSchedulerQueue(_registrations.QueueCreationRegistrations,
                                                                            queueConnection, _registrations.QueueCreationOptions);
                if (createResult.Success)
                {
                    var scope = jobQueueCreation.Scope;
                    var queue = _containers[transportName].CreateMethodJobProducer(queueConnection);
                    producerConfiguration?.Invoke(queue.Configuration);
                    if (!_queues.TryAdd(connectionInfo, queue))
                    {
                        queue.Dispose();
                        scope.Dispose();
                    }
                    else
                    {
                        queue.Start();
                        _creationScopes.TryAdd(connectionInfo, scope);
                    }
                }
                else
                {
                    throw new DotNetWorkQueueException($"Failed to create the queue. The error message is {createResult.ErrorMessage}");
                }
            }

            return(_queues[connectionInfo]);
        }
Beispiel #3
0
 /// <inheritdoc />
 public IScheduledJob AddUpdateJob <TTransportInit>(
     IJobQueueCreation jobQueueCreation,
     string jobname,
     QueueConnection queueConnection,
     string schedule,
     Expression <Action <IReceivedMessage <MessageExpression>, IWorkerNotification> > actionToRun,
     string route = null,
     Action <QueueProducerConfiguration> producerConfiguration = null,
     bool autoRun       = true,
     TimeSpan window    = default,
     bool rawExpression = false)
     where TTransportInit : ITransportInit, new()
 {
     Guard.NotNullOrEmpty(() => schedule, schedule);
     Guard.IsValid(() => jobname, jobname, i => i.Length < 256,
                   "The job name length must be 255 characters or less");
     return(AddTaskImpl <TTransportInit>(jobQueueCreation, jobname, queueConnection, new JobSchedule(schedule, GetCurrentOffset), autoRun, window, actionToRun, null, route, rawExpression, producerConfiguration));
 }
Beispiel #4
0
        /// <summary>
        /// Adds a new job or updates an existing job.
        /// </summary>
        /// <typeparam name="TTransportInit">The type of the transport initialize.</typeparam>
        /// <param name="jobQueueCreation">The job queue creation.</param>
        /// <param name="jobname">The jobname.</param>
        /// <param name="queue">The queue.</param>
        /// <param name="connection">The connection.</param>
        /// <param name="schedule">The schedule.</param>
        /// <param name="actionToRun">The action to run.</param>
        /// <param name="route">The route.</param>
        /// <param name="producerConfiguration">The producer configuration.</param>
        /// <param name="autoRun">if set to <c>true</c> [automatic run].</param>
        /// <param name="window">The window.</param>
        /// <returns></returns>
        public IScheduledJob AddUpdateJob <TTransportInit>(
            IJobQueueCreation jobQueueCreation,
            string jobname,
            string queue,
            string connection,
            string schedule,
            LinqExpressionToRun actionToRun,
            string route = null,
            Action <QueueProducerConfiguration> producerConfiguration = null,
            bool autoRun    = true,
            TimeSpan window = default(TimeSpan))
            where TTransportInit : ITransportInit, new()
        {
            Guard.NotNullOrEmpty(() => schedule, schedule);
            Guard.IsValid(() => jobname, jobname, i => i.Length < 256,
                          "The job name length must be 255 characters or less");

            return(AddTaskImpl <TTransportInit>(jobQueueCreation, jobname, queue, connection, new JobSchedule(schedule, GetCurrentOffset), autoRun, window, null, actionToRun, route, false, producerConfiguration));
        }