/// <summary>
 /// Allows to cancel a running job by its job id.
 /// </summary>
 public virtual void CancelJob(string jobId)
 {
     if (JobCancellationServiceBus.CancellationTopicExists(_serviceBusConnectionString, _cancellationServiceBusTopicName))
     {
         SendCancellationNotification(jobId);
     }
 }
 /// <summary>
 /// Sends the cancellation notification through service bus to workers to stop a job in progress.
 /// </summary>
 private void SendCancellationNotification(string jobId)
 {
     // add a new notification to the cancel topic
     JobCancellationServiceBus.SendCancellationMessage
     (
         jobId,
         _serviceBusConnectionString,
         _cancellationServiceBusTopicName
     );
 }
        /// <summary>
        /// Cancel any further processing of jobs for this batch
        /// Send a cancel notification to halt the processing of jobs
        /// </summary>
        public void CancelBatch(string batchId)
        {
            // cannot close the shared queue
            if (IsSharedQueue(batchId))
            {
                throw new InvalidOperationException(GlobalConstants.SHARED_QUEUE_CANNOT_BE_CANCELLED);
            }

            var batchQueue = _queueClient.GetQueueReference(batchId);

            // change the status of the queue in table storage to stop the addition of new jobs
            // the new status will be closed.
            MarkBatchAsClosed(batchId);

            // delete the queue
            batchQueue.DeleteIfExists();

            // Cancel all jobs belonging to the batch which are running, already
            var    jobIds      = new List <string>();
            string pagingToken = null;

            using (var repo = RepositoryFactory.CreateJobsRepository(_storageConnectionString))
            {
                do
                {
                    var jobsInRepo = repo.GetJobs(batchId, 1000, ref pagingToken);
                    jobIds.AddRange
                    (
                        jobsInRepo.Where(j => j.Status == JobStatus.Started || j.Status == JobStatus.InProgress)
                        .Select(j => j.JobId)
                        .ToList()
                    );
                } while (!string.IsNullOrEmpty(pagingToken));
            }

            // Send a cancel notification to the Cancel Topic, but only if the topic exists (means cancellation is enabled)
            if (JobCancellationServiceBus.CancellationTopicExists(_serviceBusConnectionString, _cancellationServiceBusTopicName))
            {
                jobIds.ForEach(j => SendCancellationNotification(j));
            }
        }
Exemple #4
0
        public void Initialize(ITenantManager tenantManager)
        {
            // Set the Tenant Manager
            _tenantManager = tenantManager;

            // Create the factory for built-in jobs
            _builtInJobsFactory = new Geres.Engine.JobFactories.JobWorkerFactory();

            // Read configuration settings
            _storageConnectionString = CloudConfigurationManager.GetSetting(GlobalConstants.STORAGE_CONNECTIONSTRING_CONFIGNAME);
            if (string.IsNullOrEmpty(_storageConnectionString.Trim()))
            {
                throw new Exception("Missing configuration setting " + GlobalConstants.STORAGE_CONNECTIONSTRING_CONFIGNAME);
            }
            _serviceBusConnectionString = CloudConfigurationManager.GetSetting(GlobalConstants.SERVICEBUS_INTERNAL_CONNECTIONSTRING_CONFIGNAME);
            if (string.IsNullOrEmpty(_serviceBusConnectionString.Trim()))
            {
                throw new Exception("Missing configuration setting " + GlobalConstants.SERVICEBUS_INTERNAL_CONNECTIONSTRING_CONFIGNAME);
            }

            // Parse further configuration settings
            _messageLockForProcessingInMinutes = int.Parse(CloudConfigurationManager.GetSetting(GlobalConstants.MAX_MESSAGE_LOCK_TIME_IN_MIN));
            _messageMaxRetryAttempts           = int.Parse(CloudConfigurationManager.GetSetting(GlobalConstants.MAX_MESSAGE_RETRY_ATTEMPTS));

            // Read the setting for the single-job-cancellations
            _singleJobCancellationEnabled           = bool.Parse(CloudConfigurationManager.GetSetting(GlobalConstants.GERES_CONFIG_JOB_SINGLECANCELLATION_ENABLED));
            _singleJobCancellationTimeWindow        = TimeSpan.FromSeconds(int.Parse(CloudConfigurationManager.GetSetting(GlobalConstants.GERES_CONFIG_JOB_SINGLECANCELLATION_TIMEWINDOW_IN_SECONDS)));
            _singleJobCancellationMessageTimeToLive = TimeSpan.FromSeconds(int.Parse(CloudConfigurationManager.GetSetting(GlobalConstants.GERES_CONFIG_JOB_SINGLECANCELLATION_MESSAGETIMETOLIVE)));

            // Retrieve storage account from connection string.
            _storageAccount = CloudStorageAccount.Parse(_storageConnectionString);
            _queueClient    = _storageAccount.CreateCloudQueueClient();

            // Create the default batch queue if it does not exist
            var defaultBatchQueue = _queueClient.GetQueueReference(GlobalConstants.DEFAULT_BATCH_ID);

            defaultBatchQueue.CreateIfNotExists();

            // create a retry policy
            _requestOptions = new QueueRequestOptions
            {
                // create a retry policy, retry every 30 seconds for a maximum of 10 times
                RetryPolicy = new LinearRetry(
                    TimeSpan.FromMilliseconds(GlobalConstants.STORAGE_RETRY_MILLISECONDS_BETWEEN_RETRY),
                    GlobalConstants.STORAGE_RETRY_MAX_ATTEMPTS),
            };

            // Create the service bus connection
            // Initialize the connection to Service Bus Queue
            _notificationServiceBusClient = new JobNotificationServiceBus(
                _serviceBusConnectionString,
                string.Format("{0}_{1}", GlobalConstants.SERVICEBUS_INTERNAL_TOPICS_TOPICPREFIX, GlobalConstants.SERVICEBUS_INTERNAL_TOPICS_JOBSTATUS));

            // Create the service bus connection
            // Initialize the connection to Service Bus Queue
            _cancellationServiceBusClient = new JobCancellationServiceBus(
                _serviceBusConnectionString,
                string.Format("{0}_{1}", GlobalConstants.SERVICEBUS_INTERNAL_TOPICS_TOPICPREFIX, GlobalConstants.SERVICEBUS_INTERNAL_TOPICS_CANCELJOBS),
                _singleJobCancellationTimeWindow,
                _singleJobCancellationMessageTimeToLive);

            // Create all required repositories
            _jobsRepository  = RepositoryFactory.CreateJobsRepository(_storageConnectionString);
            _batchRepository = RepositoryFactory.CreateBatchRepository(_storageConnectionString);
        }