GetConnection() public abstract method

public abstract GetConnection ( ) : IStorageConnection
return IStorageConnection
コード例 #1
0
        /// <inheritdoc />
        public string Create(Job job, IState state)
        {
            if (job == null)
            {
                throw new ArgumentNullException("job");
            }
            if (state == null)
            {
                throw new ArgumentNullException("state");
            }

            try
            {
                using (var connection = _storage.GetConnection())
                {
                    var context      = new CreateContext(connection, job, state);
                    var stateMachine = _stateMachineFactory.Create(connection);

                    return(_process.Run(context, stateMachine));
                }
            }
            catch (Exception ex)
            {
                throw new CreateJobFailedException("Job creation process has failed. See inner exception for details", ex);
            }
        }
コード例 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="job"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public string Create(Job job, IState state)
        {
            if (job == null)
            {
                throw new ArgumentNullException(nameof(job));
            }
            if (state == null)
            {
                throw new ArgumentNullException(nameof(state));
            }

            try
            {
                using (var connection = _storage.GetConnection())
                {
                    var context      = new CreateContext(_storage, connection, job, state);
                    var backroundJob = _factory.Create(context);

                    return(backroundJob?.Id);
                }
            }
            catch (Exception ex)
            {
                throw new BackgroundJobClientException("Background job creation failed. See inner exception for details.", ex);
            }
        }
コード例 #3
0
        public void AddOrUpdate(
            [NotNull] string recurringJobId,
            [NotNull] Job job,
            [NotNull] string cronExpression,
            [NotNull] TimeZoneInfo timeZone,
            [NotNull] string queue)
        {
            if (recurringJobId == null)
            {
                throw new ArgumentNullException("recurringJobId");
            }
            if (job == null)
            {
                throw new ArgumentNullException("job");
            }
            if (cronExpression == null)
            {
                throw new ArgumentNullException("cronExpression");
            }
            if (timeZone == null)
            {
                throw new ArgumentNullException("timeZone");
            }

            EnqueuedState.ValidateQueueName("queue", queue);
            ValidateCronExpression(cronExpression);

            using (var connection = _storage.GetConnection())
            {
                var recurringJob   = new Dictionary <string, string>();
                var invocationData = InvocationData.Serialize(job);

                recurringJob["Job"]        = JobHelper.ToJson(invocationData);
                recurringJob["Cron"]       = cronExpression;
                recurringJob["TimeZoneId"] = timeZone.Id;
                recurringJob["Queue"]      = queue;

                var existingJob = connection.GetAllEntriesFromHash($"recurring-job:{recurringJobId}");
                if (existingJob == null)
                {
                    recurringJob["CreatedAt"] = JobHelper.SerializeDateTime(DateTime.UtcNow);
                }

                using (var transaction = connection.CreateWriteTransaction())
                {
                    transaction.SetRangeInHash(
                        String.Format("recurring-job:{0}", recurringJobId),
                        recurringJob);

                    transaction.AddToSet("recurring-jobs", recurringJobId);

                    transaction.Commit();
                }
            }
        }
コード例 #4
0
        public void AddOrUpdate(string recurringJobId, Job job, string cronExpression, RecurringJobOptions options)
        {
            if (recurringJobId == null)
            {
                throw new ArgumentNullException(nameof(recurringJobId));
            }
            if (job == null)
            {
                throw new ArgumentNullException(nameof(job));
            }
            if (cronExpression == null)
            {
                throw new ArgumentNullException(nameof(cronExpression));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            ValidateCronExpression(cronExpression);

            using (var connection = _storage.GetConnection())
            {
                var recurringJob   = new Dictionary <string, string>();
                var invocationData = InvocationData.Serialize(job);

                recurringJob["Job"]        = JobHelper.ToJson(invocationData);
                recurringJob["Cron"]       = cronExpression;
                recurringJob["TimeZoneId"] = options.TimeZone.Id;
                recurringJob["Queue"]      = options.QueueName;
                recurringJob["Name"]       = options.ReportName;

                var existingJob = connection.GetAllEntriesFromHash($"recurring-job:{recurringJobId}");
                if (existingJob == null)
                {
                    recurringJob["CreatedAt"] = JobHelper.SerializeDateTime(DateTime.UtcNow);
                }

                using (var transaction = connection.CreateWriteTransaction())
                {
                    transaction.SetRangeInHash(
                        $"recurring-job:{recurringJobId}",
                        recurringJob);

                    transaction.AddToSet("recurring-jobs", recurringJobId);

                    transaction.Commit();
                }
            }
        }
コード例 #5
0
        public void AddOrUpdate(
            [NotNull] string recurringJobId,
            [NotNull] Job job,
            [NotNull] string cronExpression,
            [NotNull] TimeZoneInfo timeZone)
        {
            if (recurringJobId == null)
            {
                throw new ArgumentNullException("recurringJobId");
            }
            if (job == null)
            {
                throw new ArgumentNullException("job");
            }
            if (cronExpression == null)
            {
                throw new ArgumentNullException("cronExpression");
            }
            if (timeZone == null)
            {
                throw new ArgumentNullException("timeZone");
            }

            ValidateCronExpression(cronExpression);

            using (var connection = _storage.GetConnection())
            {
                var recurringJob   = new Dictionary <string, string>();
                var invocationData = InvocationData.Serialize(job);

                recurringJob["Job"]        = JobHelper.ToJson(invocationData);
                recurringJob["Cron"]       = cronExpression;
                recurringJob["TimeZoneId"] = timeZone.Id;

                using (var transaction = connection.CreateWriteTransaction())
                {
                    transaction.SetRangeInHash(
                        String.Format("recurring-job:{0}", recurringJobId),
                        recurringJob);

                    transaction.AddToSet("recurring-jobs", recurringJobId);

                    transaction.Commit();
                }
            }
        }
コード例 #6
0
        public void AddOrUpdate(string recurringJobId, Job job, ReportOnEventJobOptions options)
        {
            if (recurringJobId == null)
            {
                throw new ArgumentNullException(nameof(recurringJobId));
            }
            if (job == null)
            {
                throw new ArgumentNullException(nameof(job));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            using (var connection = _storage.GetConnection())
            {
                var recurringJob   = new Dictionary <string, string>();
                var invocationData = InvocationData.Serialize(job);

                recurringJob["Job"] = JobHelper.ToJson(invocationData);

                recurringJob["SignalId"]    = options.Signal;
                recurringJob["TriggerExpr"] = JsonConvert.SerializeObject(options.Predicate, jsonSettings.Value);
                recurringJob["Queue"]       = options.QueueName;
                recurringJob["LastValue"]   = null;
                recurringJob["Name"]        = options.ReportName;

                var existingJob = connection.GetAllEntriesFromHash($"on-event-job:{recurringJobId}");
                if (existingJob == null)
                {
                    recurringJob["CreatedAt"] = JobHelper.SerializeDateTime(DateTime.UtcNow);
                }

                using (var transaction = connection.CreateWriteTransaction())
                {
                    transaction.SetRangeInHash(
                        $"on-event-job:{recurringJobId}",
                        recurringJob);

                    transaction.AddToSet("on-event-jobs", recurringJobId);
                    transaction.Commit();
                }
            }
        }
コード例 #7
0
        public void AddOrUpdate(string recurringJobId, Job job, string cronExpression, RecurringJobOptions options,
                                bool status = true)
        {
            if (recurringJobId == null)
            {
                throw new ArgumentNullException(nameof(recurringJobId));
            }
            if (job == null)
            {
                throw new ArgumentNullException(nameof(job));
            }
            if (cronExpression == null)
            {
                throw new ArgumentNullException(nameof(cronExpression));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            ValidateCronExpression(cronExpression);

            using (var connection = _storage.GetConnection())
                using (connection.AcquireDistributedRecurringJobLock(recurringJobId, DefaultTimeout))
                {
                    var recurringJob = connection.GetOrCreateRecurringJob(recurringJobId, _timeZoneResolver, _nowFactory());

                    recurringJob.Job      = job;
                    recurringJob.Cron     = cronExpression;
                    recurringJob.TimeZone = options.TimeZone;
                    recurringJob.Queue    = options.QueueName;
                    recurringJob.IsActive = status;
                    if (recurringJob.IsChanged(out var changedFields, out var nextExecution))
                    {
                        using (var transaction = connection.CreateWriteTransaction())
                        {
                            transaction.UpdateRecurringJob(recurringJobId, changedFields, nextExecution);
                            transaction.Commit();
                        }
                    }
                }
        }