public override JobResult Run(JobContext context) {
            Log.Info().Message("Remove stale accounts job starting").Write();

            int skip = 0;
            var organizations = _organizationRepository.Collection.FindAs<Organization>(
                Query.And(
                    Query.LTE(OrganizationRepository.FieldNames.TotalErrorCount, new BsonInt64(0)), 
                    Query.EQ(OrganizationRepository.FieldNames.PlanId, BillingManager.FreePlan.Id)))
                .SetFields(OrganizationRepository.FieldNames.Id, OrganizationRepository.FieldNames.Name, OrganizationRepository.FieldNames.StripeCustomerId, OrganizationRepository.FieldNames.LastErrorDate)
                .SetLimit(20).SetSkip(skip).ToList();

            while (organizations.Count > 0) {
                foreach (var organization in organizations)
                    TryDeleteOrganization(organization);

                skip += 20;
                organizations = _organizationRepository.Collection.FindAs<Organization>(
                    Query.And(
                        Query.LTE(OrganizationRepository.FieldNames.TotalErrorCount, new BsonInt64(0)), 
                        Query.EQ(OrganizationRepository.FieldNames.PlanId, BillingManager.FreePlan.Id)))
                    .SetFields(OrganizationRepository.FieldNames.Id, OrganizationRepository.FieldNames.Name, OrganizationRepository.FieldNames.StripeCustomerId, OrganizationRepository.FieldNames.LastErrorDate)
                    .SetLimit(20).SetSkip(skip).ToList();
            }

            return new JobResult {
                Result = "Successfully removed all stale accounts."
            };
        }
예제 #2
0
        public override JobResult Run(JobContext context) {
            if (!String.Equals(_messageService.GetStatus(), "Disposed"))
                _messageService.Start();

            return new JobResult {
                Result = "Successfully started the message service."
            };
        }
        public override JobResult Run(JobContext context) {
            Log.Info().Message("Daily Notification job starting").Write();

            if (!Settings.Current.EnableSummaryNotifications) {
                return new JobResult {
                    Result = "Summary Notifications are disabled.",
                    Cancelled = true
                };
            }

            const int BATCH_SIZE = 25;

            // Send an email at 9:00am in the projects local time.
            IMongoQuery query = Query.LT(ProjectRepository.FieldNames.NextSummaryEndOfDayTicks, new BsonInt64(DateTime.UtcNow.Ticks - (TimeSpan.TicksPerHour * 9)));
            UpdateBuilder update = Update.Inc(ProjectRepository.FieldNames.NextSummaryEndOfDayTicks, TimeSpan.TicksPerDay);

            var projects = _projectRepository.Collection.FindAs<Project>(query)
                .SetFields(ProjectRepository.FieldNames.Id, ProjectRepository.FieldNames.NextSummaryEndOfDayTicks)
                .SetLimit(BATCH_SIZE).ToList();

            while (projects.Count > 0) {
                IMongoQuery queryWithProjectIds = Query.And(Query.In(ProjectRepository.FieldNames.Id, projects.Select(p => new BsonObjectId(new ObjectId(p.Id)))), query);
                var result = _projectRepository.Collection.Update(queryWithProjectIds, update, UpdateFlags.Multi);
                Log.Info().Message("Daily Notification job processing {0} projects. Successfully updated {1} projects. ", projects.Count, result.DocumentsAffected);

                Debug.Assert(projects.Count == result.DocumentsAffected);

                foreach (var project in projects) {
                    var utcStartTime = new DateTime(project.NextSummaryEndOfDayTicks - TimeSpan.TicksPerDay);
                    if (utcStartTime < DateTime.UtcNow.Date.SubtractDays(2)) {
                        Log.Info().Message("Skipping Summary Notification older than two days for Project: {0} with a start time of {1}.", project.Id, utcStartTime);
                        continue;
                    }

                    if (_messageFactory != null) {
                        using (IMessageProducer messageProducer = _messageFactory.CreateMessageProducer()) {
                            var notification = new SummaryNotification {
                                Id = project.Id,
                                UtcStartTime = utcStartTime,
                                UtcEndTime = new DateTime(project.NextSummaryEndOfDayTicks - TimeSpan.TicksPerSecond)
                            };

                            Log.Info().Message("Publishing Summary Notification for Project: {0}, with a start time of {1} and an end time of {2}", notification.Id, notification.UtcStartTime, notification.UtcEndTime);
                            messageProducer.Publish(notification);
                        }
                    } else
                        Log.Error().Message("Message Factory is null").Write();
                }

                projects = _projectRepository.Collection.FindAs<Project>(query)
                    .SetFields(ProjectRepository.FieldNames.Id, ProjectRepository.FieldNames.NextSummaryEndOfDayTicks)
                    .SetLimit(BATCH_SIZE).ToList();
            }

            return new JobResult {
                Result = "Successfully enforced all retention limits."
            };
        }
        public override JobResult Run(JobContext context) {
            Log.Info().Message("Enforce retention limits job starting").Write();

            int skip = 0;
            var organizations = _organizationRepository.Collection.FindAs<Organization>(Query.Null)
                .SetFields(OrganizationRepository.FieldNames.Id, OrganizationRepository.FieldNames.Name, OrganizationRepository.FieldNames.RetentionDays)
                .SetLimit(100).SetSkip(skip).ToList();

            while (organizations.Count > 0) {
                // TODO: Need to add overage days to the org when they went over their limit for the day.
                foreach (var organization in organizations)
                    EnforceErrorCountLimits(organization);

                skip += 100;
                organizations = _organizationRepository.Collection.FindAs<Organization>(Query.Null)
                    .SetFields(OrganizationRepository.FieldNames.Id, OrganizationRepository.FieldNames.Name, OrganizationRepository.FieldNames.RetentionDays)
                    .SetLimit(100).SetSkip(skip).ToList();
            }

            return new JobResult {
                Result = "Successfully enforced all retention limits."
            };
        }
예제 #5
0
        private void RunInternal()
        {
            if (IsBusy)
                return;

            IsBusy = true;

            // get lock
            using (var jobLock = _jobLockProvider.Acquire(Name))
            {
                // give up if no lock
                if (!jobLock.LockAcquired) {
                    LastResult = "Could not acquire a job lock.";
                    LastStatus = JobStatus.Canceled;
                    Status = JobStatus.Waiting;
                    IsBusy = false;

                    return;
                }

                DateTime started = DateTime.Now;
                LastRunStartTime = started;
                JobManager.Current.OnJobRunning(new JobEventArgs(Name, JobAction.Running, _id));

                Status = JobStatus.Running;
                Interlocked.Increment(ref JobManager.JobsRunning);

                try
                {
                    CreateInstance();

                    var context = new JobContext(Name, Description, LastRunStartTime, LastStatus, Arguments, UpdateStatus, _dependencyResolver);
                    JobResult r = _instance.Run(context);

                    if (r == null)
                    {
                        if (String.IsNullOrEmpty(LastResult))
                            LastResult = "Completed";
                        LastStatus = JobStatus.Completed;
                    }
                    else if (r.Error != null)
                    {
                        LastResult = r.Error.Message;
                        LastStatus = JobStatus.Error;

                        Trace.TraceError(r.Error.ToString());
                    }
                    else
                    {
                        if (r.Result != null)
                            LastResult = r.Result.ToString();
                        LastStatus = JobStatus.Completed;
                    }
                }
                catch (Exception ex)
                {
                    LastResult = ex.Message;
                    LastStatus = JobStatus.Error;

                    Trace.TraceError(ex.ToString());
                }
                finally
                {
                    Interlocked.Decrement(ref JobManager.JobsRunning);
                    LastRunFinishTime = DateTime.Now;

                    if (!_keepAlive)
                        _instance = null;

                    try
                    {
                        if (_jobHistoryProvider != null)
                            _jobHistoryProvider.SaveHistory(this);
                    }
                    catch (Exception ex)
                    {
                        Trace.TraceError("Error saving job history: " + ex.Message);
                    }

                    JobManager.Current.OnJobCompleted(
                        new JobCompletedEventArgs(
                            Name,
                            JobAction.Completed,
                            _id,
                            started,
                            LastRunFinishTime,
                            LastResult,
                            LastStatus));

                    Status = JobStatus.Waiting;
                    IsBusy = false;
                }
            } // release job lock
        }
예제 #6
0
 /// <summary>
 /// Runs this job.
 /// </summary>
 /// <param name="context">The job context.</param>
 /// <returns>
 /// A <see cref="JobResult"/> instance indicating the results of the job.
 /// </returns>
 public abstract JobResult Run(JobContext context);
예제 #7
0
        private void RunInternal()
        {
            if (IsBusy)
            {
                return;
            }

            IsBusy = true;

            // get lock
            using (var jobLock = _jobLockProvider.Acquire(Name))
            {
                // give up if no lock
                if (!jobLock.LockAcquired)
                {
                    LastResult = "Could not acquire a job lock.";
                    LastStatus = JobStatus.Canceled;
                    Status     = JobStatus.Waiting;
                    IsBusy     = false;

                    return;
                }

                DateTime started = DateTime.Now;
                LastRunStartTime = started;
                JobManager.Current.OnJobRunning(new JobEventArgs(Name, JobAction.Running, _id));

                Status = JobStatus.Running;
                Interlocked.Increment(ref JobManager.JobsRunning);

                try
                {
                    CreateInstance();

                    var       context = new JobContext(Name, Description, LastRunStartTime, LastStatus, Arguments, UpdateStatus, _dependencyResolver);
                    JobResult r       = _instance.Run(context);

                    if (r == null)
                    {
                        if (String.IsNullOrEmpty(LastResult))
                        {
                            LastResult = "Completed";
                        }
                        LastStatus = JobStatus.Completed;
                    }
                    else if (r.Error != null)
                    {
                        LastResult = r.Error.Message;
                        LastStatus = JobStatus.Error;

                        Trace.TraceError(r.Error.ToString());
                    }
                    else
                    {
                        if (r.Result != null)
                        {
                            LastResult = r.Result.ToString();
                        }
                        LastStatus = JobStatus.Completed;
                    }
                }
                catch (Exception ex)
                {
                    LastResult = ex.Message;
                    LastStatus = JobStatus.Error;

                    Trace.TraceError(ex.ToString());
                }
                finally
                {
                    Interlocked.Decrement(ref JobManager.JobsRunning);
                    LastRunFinishTime = DateTime.Now;

                    if (!_keepAlive)
                    {
                        _instance = null;
                    }

                    try
                    {
                        if (_jobHistoryProvider != null)
                        {
                            _jobHistoryProvider.SaveHistory(this);
                        }
                    }
                    catch (Exception ex)
                    {
                        Trace.TraceError("Error saving job history: " + ex.Message);
                    }

                    JobManager.Current.OnJobCompleted(
                        new JobCompletedEventArgs(
                            Name,
                            JobAction.Completed,
                            _id,
                            started,
                            LastRunFinishTime,
                            LastResult,
                            LastStatus));

                    Status = JobStatus.Waiting;
                    IsBusy = false;
                }
            } // release job lock
        }
예제 #8
0
 /// <summary>
 /// Runs this job.
 /// </summary>
 /// <param name="context">The job context.</param>
 /// <returns>
 /// A <see cref="JobResult"/> instance indicating the results of the job.
 /// </returns>
 public abstract JobResult Run(JobContext context);