Exemple #1
0
        private JobData GetJobData(BackgroundWorkerJob job)
        {
            Type jobType = Type.GetType(job.Type);
            var  jobData = new JobData
            {
                Id               = job.Id,
                UniqueId         = job.UniqueId,
                Instance         = job.Instance,
                AbsoluteTimeout  = job.AbsoluteTimeout,
                CreatedDate      = job.CreatedDate,
                Data             = job.Data,
                MetaData         = job.MetaData,
                QueueId          = job.QueueId,
                Status           = (JobStatus)job.StatusId,
                LastEndTime      = job.LastExecutionEndDateTime,
                LastStartTime    = job.LastExecutionStartDateTime,
                NextStartTime    = job.NextExecutionStartDateTime,
                LastErrorMessage = job.LastErrorMessage,
                JobType          = jobType,
                Application      = job.Application,
                Group            = job.Group,
                Name             = job.Name,
                Description      = job.Description,
                SuppressHistory  = job.SuppressHistory,
                DeleteWhenDone   = job.DeleteWhenDone,
            };

            if (!string.IsNullOrEmpty(job.ScheduleType))
            {
                jobData.Schedule = (ISchedule)Utils.DeserializeObject(job.Schedule, Type.GetType(job.ScheduleType));
            }
            return(jobData);
        }
Exemple #2
0
        /// <summary>
        /// Schedules (create with schedule) the job in the job store.
        /// </summary>
        /// <param name="jobType">Type of the job.</param>
        /// <param name="data">The data.</param>
        /// <param name="metaData">The meta data.</param>
        /// <param name="queueId">The queue id.</param>
        /// <param name="schedule">The schedule.</param>
        /// <param name="name">The name.</param>
        /// <param name="description">The description.</param>
        /// <param name="application">The application.</param>
        /// <param name="group">The group.</param>
        /// <param name="absoluteTimeout">The absolute timeout.</param>
        /// <param name="jobStatus">The job status.</param>
        /// <param name="createdDate">The created date. Jobs are ordered by creation date for execution so it's possible to prioritize jobs for queueing.</param>
        /// <param name="suppressHistory">if set to <c>true</c> no execution history records are created.</param>
        /// <param name="deleteWhenDone">if set to <c>true</c> the job is deleted after a successfull (status Done) execution.</param>
        /// <returns></returns>
        public JobData ScheduleJob(Type jobType, string data, string metaData, byte queueId, ISchedule schedule, string name = null, string description = null, string application = null, string group = null, TimeSpan?absoluteTimeout = null, JobStatus?jobStatus = JobStatus.Ready, DateTime?createdDate = null, bool suppressHistory = false, bool deleteWhenDone = false)
        {
            if (jobType == null)
            {
                throw new ArgumentException("jobType cannot be null.");
            }

            BackgroundWorkerJob newJob = new BackgroundWorkerJob
            {
                AbsoluteTimeout = absoluteTimeout,
                CreatedDate     = DateTime.Now,
                Data            = data,
                MetaData        = metaData,
                QueueId         = queueId,
                StatusId        = (int?)jobStatus ?? (int)JobStatus.Ready,
                Type            = jobType.AssemblyQualifiedName,
                ScheduleType    = schedule.GetType().AssemblyQualifiedName,
                Schedule        = Utils.SerializeObject(schedule, schedule.GetType()),
                Instance        = null,          //This is just a default to show that no instance has picked this up
                Application     = application,
                Group           = group,
                Name            = name,
                Description     = description,
                SuppressHistory = suppressHistory,
                DeleteWhenDone  = deleteWhenDone,
            };

            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions {
                IsolationLevel = IsolationLevel.Serializable
            }))
            {
                using (Linq2SqlJobStoreDalDataContext context = new Linq2SqlJobStoreDalDataContext(connectionString))
                {
                    //In order to get a table lock, we have to resort to SQL
                    context.ExecuteQuery <BackgroundWorkerJob>("SELECT TOP 1 * FROM BackgroundWorkerJobs").FirstOrDefault();
                    context.BackgroundWorkerJobs.InsertOnSubmit(newJob);
                    try
                    {
                        context.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);
                        JobData job = GetJobData(newJob);
                        jobsRequiringAction.Add(job);
                        return(job);
                    }
                    catch (Exception ex)
                    {
                        logger.LogException(string.Format("Failed to create '{0}' with data '{1}'", jobType.AssemblyQualifiedName, data), ex);
                    }
                    finally
                    {
                        ts.Complete();
                    }
                }
            }
            return(null);
        }
Exemple #3
0
        public JobData CreateJob(Type jobType, string data, string metaData, byte queueId, ISchedule schedule = null, Guid?uniqueId = null, string name = null, string description = null, string application = null, string group = null, TimeSpan?absoluteTimeout = null, JobStatus?jobStatus = null, DateTime?createdDate = null, bool suppressHistory = false, bool deleteWhenDone = false)
        {
            if (jobType == null)
            {
                throw new ArgumentException("jobType cannot be null.");
            }

            BackgroundWorkerJob newJob = new BackgroundWorkerJob
            {
                UniqueId                   = uniqueId ?? Guid.NewGuid(),
                AbsoluteTimeout            = absoluteTimeout,
                CreatedDate                = DateTime.Now,
                Data                       = data,
                MetaData                   = metaData,
                QueueId                    = queueId,
                StatusId                   = (int?)jobStatus ?? (schedule == null ? (int)JobStatus.Ready : (int)JobStatus.Scheduled),
                Type                       = jobType.AssemblyQualifiedName,
                ScheduleType               = schedule != null?schedule.GetType().AssemblyQualifiedName            : null,
                Schedule                   = schedule != null?Utils.SerializeObject(schedule, schedule.GetType()) : null,
                Instance                   = null, //This is just a default to show that no instance has picked this up
                Application                = application,
                Group                      = group,
                Name                       = name,
                Description                = description,
                SuppressHistory            = suppressHistory,
                DeleteWhenDone             = deleteWhenDone,
                NextExecutionStartDateTime = (schedule != null ? schedule.GetNextOccurrence() : null),
            };

            using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions {
                IsolationLevel = IsolationLevel.Serializable, Timeout = transactionTimeout
            }))
            {
                using (Linq2SqlJobStoreDalDataContext context = new Linq2SqlJobStoreDalDataContext(connectionString))
                {
                    context.BackgroundWorkerJobs.InsertOnSubmit(newJob);
                    try
                    {
                        context.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);
                        ts.Complete();
                        JobData job = GetJobData(newJob);
                        return(job);
                    }
                    catch (Exception ex)
                    {
                        logger.Error(string.Format("Failed to create '{0}' with data '{1}'", jobType.AssemblyQualifiedName, data), ex);
                    }
                }
            }
            return(null);
        }
 partial void DeleteBackgroundWorkerJob(BackgroundWorkerJob instance);
 partial void UpdateBackgroundWorkerJob(BackgroundWorkerJob instance);
 partial void InsertBackgroundWorkerJob(BackgroundWorkerJob instance);