public ConsoleResultModel Run()
        {
            var taskToUpdate     = SchedulingController.GetSchedule(Convert.ToInt32(TaskId));
            List <TaskModel> lst = new List <TaskModel>();

            if (taskToUpdate != null)
            {
                taskToUpdate.Enabled = (bool)Enabled;
                var _with1 = taskToUpdate;

                SchedulingController.UpdateSchedule(_with1.ScheduleID, _with1.TypeFullName, _with1.TimeLapse, _with1.TimeLapseMeasurement, _with1.RetryTimeLapse, _with1.RetryTimeLapseMeasurement, _with1.RetainHistoryNum, _with1.AttachToEvent, _with1.CatchUpEnabled, _with1.Enabled,
                                                    _with1.ObjectDependencies, _with1.Servers, _with1.FriendlyName);
                var updatedTask = SchedulingController.GetSchedule(Convert.ToInt32(TaskId));
                if (updatedTask != null)
                {
                    lst.Add(new TaskModel(updatedTask));
                }
            }

            return(new ConsoleResultModel(string.Format("{0} task{1} updated", lst.Count, (lst.Count != 1 ? "s" : "")))
            {
                data = lst
            });
        }
        public override void DoWork()
        {
            try
            {
                //TODO: do some clean-up for very old import/export jobs/logs

                var job = EntitiesController.Instance.GetFirstActiveJob();
                if (job == null)
                {
                    ScheduleHistoryItem.Succeeded = true;
                    ScheduleHistoryItem.AddLogNote("<br/>No Site Export/Import jobs queued for processing.");
                }
                else if (job.IsCancelled)
                {
                    job.JobStatus = JobStatus.Cancelled;
                    EntitiesController.Instance.UpdateJobStatus(job);
                    ScheduleHistoryItem.Succeeded = true;
                    ScheduleHistoryItem.AddLogNote("<br/>Site Export/Import jobs was previously cancelled.");
                }
                else
                {
                    job.JobStatus = JobStatus.InProgress;
                    EntitiesController.Instance.UpdateJobStatus(job);
                    var result = new ExportImportResult
                    {
                        JobId = job.JobId,
                    };
                    var engine    = new ExportImportEngine();
                    var succeeded = true;

                    switch (job.JobType)
                    {
                    case JobType.Export:
                        try
                        {
                            engine.Export(job, result, ScheduleHistoryItem);
                        }
                        catch (Exception ex)
                        {
                            result.AddLogEntry("EXCEPTION exporting job #" + job.JobId, ex.Message, ReportLevel.Error);
                            engine.AddLogsToDatabase(job.JobId, result.CompleteLog);
                            throw;
                        }
                        EntitiesController.Instance.UpdateJobStatus(job);
                        break;

                    case JobType.Import:
                        try
                        {
                            engine.Import(job, result, ScheduleHistoryItem);
                        }
                        catch (ThreadAbortException)
                        {
                            ScheduleHistoryItem.TimeLapse                 = EmergencyScheduleFrequency;
                            ScheduleHistoryItem.TimeLapseMeasurement      = EmergencyScheduleFrequencyUnit;
                            ScheduleHistoryItem.RetryTimeLapse            = EmergencyScheduleRetry;
                            ScheduleHistoryItem.RetryTimeLapseMeasurement = EmergencyScheduleRetryUnit;
                            ScheduleHistoryItem.RetainHistoryNum          = EmergencyHistoryNumber;

                            SchedulingController.UpdateSchedule(ScheduleHistoryItem);

                            SchedulingController.PurgeScheduleHistory();

                            Logger.Error("The Schduler item stopped because main thread stopped, set schedule into emergency mode so it will start after app restart.");
                            succeeded = false;
                        }
                        catch (Exception ex)
                        {
                            result.AddLogEntry("EXCEPTION importing job #" + job.JobId, ex.Message, ReportLevel.Error);
                            engine.AddLogsToDatabase(job.JobId, result.CompleteLog);
                            throw;
                        }
                        EntitiesController.Instance.UpdateJobStatus(job);
                        if (job.JobStatus == JobStatus.Successful || job.JobStatus == JobStatus.Cancelled)
                        {
                            // clear everything to be sure imported items take effect
                            DataCache.ClearCache();
                        }
                        break;

                    default:
                        throw new Exception("Unknown job type: " + job.JobType);
                    }

                    ScheduleHistoryItem.Succeeded = true;

                    //restore schedule item running timelapse to default.
                    if (succeeded &&
                        ScheduleHistoryItem.TimeLapse == EmergencyScheduleFrequency &&
                        ScheduleHistoryItem.TimeLapseMeasurement == EmergencyScheduleFrequencyUnit)
                    {
                        ScheduleHistoryItem.TimeLapse                 = DefaultScheduleFrequency;
                        ScheduleHistoryItem.TimeLapseMeasurement      = DefaultScheduleFrequencyUnit;
                        ScheduleHistoryItem.RetryTimeLapse            = DefaultScheduleRetry;
                        ScheduleHistoryItem.RetryTimeLapseMeasurement = DefaultScheduleRetryUnit;
                        ScheduleHistoryItem.RetainHistoryNum          = DefaultHistoryNumber;

                        SchedulingController.UpdateSchedule(ScheduleHistoryItem);
                    }

                    var sb        = new StringBuilder();
                    var jobType   = Localization.GetString("JobType_" + job.JobType, Constants.SharedResources);
                    var jobStatus = Localization.GetString("JobStatus_" + job.JobStatus, Constants.SharedResources);
                    sb.AppendFormat("<br/><b>{0} {1}</b>", jobType, jobStatus);
                    var summary = result.Summary;
                    if (summary.Count > 0)
                    {
                        sb.Append("<br/><b>Summary:</b><ul>");
                        foreach (var entry in summary)
                        {
                            sb.Append($"<li>{entry.Name}: {entry.Value}</li>");
                        }
                        sb.Append("</ul>");
                    }

                    ScheduleHistoryItem.AddLogNote(sb.ToString());
                    engine.AddLogsToDatabase(job.JobId, result.CompleteLog);

                    Logger.Trace("Site Export/Import: Job Finished");
                }
                //SetLastSuccessfulIndexingDateTime(ScheduleHistoryItem.ScheduleID, ScheduleHistoryItem.StartDate);
            }
            catch (Exception ex)
            {
                ScheduleHistoryItem.Succeeded = false;
                ScheduleHistoryItem.AddLogNote("<br/>Export/Import EXCEPTION: " + ex.Message);
                Errored(ref ex);
                // this duplicates the logging
                //if (ScheduleHistoryItem.ScheduleSource != ScheduleSource.STARTED_FROM_BEGIN_REQUEST)
                //{
                //    Exceptions.LogException(ex);
                //}
            }
        }