Exemplo n.º 1
0
        private static void RunBackgroundTask(BackgroundTask backgroundTask)
        {
            UserInfo user = PackageController.GetPackageOwner(backgroundTask.PackageId);

            SecurityContext.SetThreadPrincipal(user.UserId);

            var schedule = SchedulerController.GetScheduleComplete(backgroundTask.ScheduleId);

            backgroundTask.Guid   = TaskManager.Guid;
            backgroundTask.Status = BackgroundTaskStatus.Run;


            TaskController.UpdateTask(backgroundTask);

            try
            {
                var objTask = (SchedulerTask)Activator.CreateInstance(Type.GetType(schedule.Task.TaskType));

                objTask.DoWork();
            }
            catch (Exception ex)
            {
                TaskManager.WriteError(ex, "Error executing scheduled task");
            }
            finally
            {
                try
                {
                    TaskManager.CompleteTask();
                }
                catch (Exception)
                {
                }
            }
        }
Exemplo n.º 2
0
        public ScheduleTaskViewConfiguration GetScheduleTaskViewConfiguration(string taskId, string environment)
        {
            List <ScheduleTaskViewConfiguration> configurations = SchedulerController.GetScheduleTaskViewConfigurations(taskId);

            return(configurations.Find(delegate(ScheduleTaskViewConfiguration configuration)
            {
                return configuration.Environment == environment;
            }));
        }
Exemplo n.º 3
0
        public static void ScheduleTasks()
        {
            RunManualTasks();

            nextSchedule = SchedulerController.GetNextSchedule();

            if (nextSchedule != null)
            {
                if (nextSchedule.ScheduleInfo.NextRun <= DateTime.Now)
                {
                    RunNextSchedule(null);
                }
            }
        }
Exemplo n.º 4
0
        static void RunSchedule(SchedulerJob schedule, bool changeNextRun)
        {
            try
            {
                // update next run (if required)
                if (changeNextRun)
                {
                    SchedulerController.CalculateNextStartTime(schedule.ScheduleInfo);
                }

                // disable run once task
                if (schedule.ScheduleInfo.ScheduleType == ScheduleType.OneTime)
                {
                    schedule.ScheduleInfo.Enabled = false;
                }

                Dictionary <int, BackgroundTask> scheduledTasks = TaskManager.GetScheduledTasks();
                if (!scheduledTasks.ContainsKey(schedule.ScheduleInfo.ScheduleId))
                {
                    // this task should be run, so
                    // update its last run
                    schedule.ScheduleInfo.LastRun = DateTime.Now;
                }

                // update schedule
                int MAX_RETRY_COUNT = 10;
                int counter         = 0;
                while (counter < MAX_RETRY_COUNT)
                {
                    try
                    {
                        SchedulerController.UpdateSchedule(schedule.ScheduleInfo);
                        break;
                    }
                    catch (SqlException)
                    {
                        System.Threading.Thread.Sleep(1000);
                    }

                    counter++;
                }
                if (counter == MAX_RETRY_COUNT)
                {
                    return;
                }

                // skip execution if the current task is still running
                scheduledTasks = TaskManager.GetScheduledTasks();
                if (!scheduledTasks.ContainsKey(schedule.ScheduleInfo.ScheduleId))
                {
                    // run the schedule in the separate thread
                    schedule.Run();
                }
            }
            catch (Exception Ex)
            {
                try
                {
                    TaskManager.WriteError(string.Format("RunSchedule Error : {0}", Ex.Message));
                }
                catch (Exception)
                {
                }
            }
        }
Exemplo n.º 5
0
 public List <ScheduleTaskViewConfiguration> GetScheduleTaskViewConfigurations(string taskId)
 {
     return(SchedulerController.GetScheduleTaskViewConfigurations(taskId));
 }
Exemplo n.º 6
0
 public List <ScheduleTaskParameterInfo> GetScheduleParameters(string taskId, int scheduleId)
 {
     return(SchedulerController.GetScheduleParameters(taskId, scheduleId));
 }
Exemplo n.º 7
0
 public ScheduleInfo GetSchedule(int scheduleId)
 {
     return(SchedulerController.GetSchedule(scheduleId));
 }
Exemplo n.º 8
0
 public DataSet GetSchedulesPaged(int packageId, bool recursive,
                                  string filterColumn, string filterValue, string sortColumn, int startRow, int maximumRows)
 {
     return(SchedulerController.GetSchedulesPaged(packageId,
                                                  recursive, filterColumn, filterValue, sortColumn, startRow, maximumRows));
 }
Exemplo n.º 9
0
 public DataSet GetSchedules(int userId)
 {
     return(SchedulerController.GetSchedules(userId));
 }
Exemplo n.º 10
0
 public List <ScheduleTaskInfo> GetScheduleTasks()
 {
     return(SchedulerController.GetScheduleTasks());
 }
Exemplo n.º 11
0
 public DateTime GetSchedulerTime()
 {
     return(SchedulerController.GetSchedulerTime());
 }
Exemplo n.º 12
0
 public int DeleteSchedule(int scheduleId)
 {
     return(SchedulerController.DeleteSchedule(scheduleId));
 }
Exemplo n.º 13
0
 public int UpdateSchedule(ScheduleInfo schedule)
 {
     return(SchedulerController.UpdateSchedule(schedule));
 }
Exemplo n.º 14
0
 public int AddSchedule(ScheduleInfo schedule)
 {
     return(SchedulerController.AddSchedule(schedule));
 }
Exemplo n.º 15
0
 public int StopSchedule(int scheduleId)
 {
     return(SchedulerController.StopSchedule(scheduleId));
 }
Exemplo n.º 16
0
        public override void DoWork()
        {
            BackgroundTask topTask = TaskManager.TopTask;

            // get input parameters
            string mailTo           = (string)topTask.GetParamValue("MAIL_TO");
            int    auditLogSeverity = Utils.ParseInt((string)topTask.GetParamValue("AUDIT_LOG_SEVERITY"), -1);
            string auditLogSource   = (string)topTask.GetParamValue("AUDIT_LOG_SOURCE");
            string auditLogTask     = (string)topTask.GetParamValue("AUDIT_LOG_TASK");
            string auditLogDate     = (string)topTask.GetParamValue("AUDIT_LOG_DATE");
            int    showExecutionLog = Utils.ParseInt((string)topTask.GetParamValue("SHOW_EXECUTION_LOG"), 0);

            // check input parameters
            if (String.IsNullOrEmpty(mailTo))
            {
                TaskManager.WriteWarning("Specify 'Mail To' task parameter");
                return;
            }

            string         mailFrom = null;
            SystemSettings settings = SystemController.GetSystemSettingsInternal(SystemSettings.SMTP_SETTINGS, false);

            if (settings != null)
            {
                mailFrom = settings["SmtpUsername"];
            }
            if (String.IsNullOrEmpty(mailFrom))
            {
                TaskManager.WriteWarning("You need to configure SMTP settings first");
                return;
            }

            DateTime logStart, logEnd;

            switch (auditLogDate)
            {
            case "today":
                logStart = DateTime.Now;
                logEnd   = DateTime.Now;
                break;

            case "yesterday":
                logStart = DateTime.Now.AddDays(-1);
                logEnd   = DateTime.Now.AddDays(-1);
                break;

            case "schedule":
            default:
                logEnd = DateTime.Now;
                ScheduleInfo schedule = SchedulerController.GetSchedule(topTask.ScheduleId);
                switch (schedule.ScheduleTypeId)
                {
                case "Daily":
                    logStart = DateTime.Now.AddDays(-1);
                    break;

                case "Weekly":
                    logStart = DateTime.Now.AddDays(-7);
                    break;

                case "Monthly":
                    logStart = DateTime.Now.AddMonths(-1);
                    break;

                case "Interval":
                    logStart = DateTime.Now.AddSeconds(-schedule.Interval);
                    break;

                case "OneTime":
                default:
                    logStart = DateTime.Now;
                    break;
                }
                break;
            }

            string mailSubject = "Audit Log Report (" + logStart.ToString("MMM dd, yyyy") + " - " + logEnd.ToString("MMM dd, yyyy") + ")";

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<html><head><style>");
            sb.AppendLine("table, th, td { border: 1px solid black; border-collapse: collapse; }");
            sb.AppendLine("th, td { padding: 5px; }");
            sb.AppendLine("th { text-align: left; }");
            sb.AppendLine("</style></head><body>");
            sb.AppendLine("<h2>" + mailSubject + "</h2>");
            sb.AppendFormat("<h3>Source: {0}, Task: {1}, Severity: {2}</h3>", String.IsNullOrEmpty(auditLogSource) ? "All" : auditLogSource,
                            String.IsNullOrEmpty(auditLogTask) ? "All" : auditLogTask, GetAuditLogRecordSeverityName(auditLogSeverity));

            DataTable logs = AuditLog.GetAuditLogRecordsPaged(topTask.EffectiveUserId, 0, 0, null, logStart, logEnd,
                                                              auditLogSeverity, auditLogSource, auditLogTask, "", 0, Int32.MaxValue).Tables[1];

            sb.AppendLine("<p>");
            if (logs.Rows.Count == 0)
            {
                sb.AppendLine("Audit Log is empty.");
            }
            else
            {
                sb.AppendLine("<table>");
                sb.Append("<tr><th>Started</th><th>Finished</th><th>Severity</th><th>Username</th><th>Source</th><th>Task</th><th>Item-Name</th>");
                if (showExecutionLog == 1)
                {
                    sb.AppendLine("<th>Execution-Log</th></tr>");
                }
                else
                {
                    sb.AppendLine("</tr>");
                }
                foreach (DataRow log in logs.Rows)
                {
                    sb.AppendLine("<tr>");
                    // Started
                    sb.AppendFormat("<td>{0}</td>", log["StartDate"].ToString());
                    // Finished
                    sb.AppendFormat("<td>{0}</td>", log["FinishDate"].ToString());
                    // Severity
                    sb.AppendFormat("<td>{0}</td>", GetAuditLogRecordSeverityName((int)log["SeverityID"]));
                    // Username
                    sb.AppendFormat("<td>{0}</td>", log["Username"]);
                    // Source
                    sb.AppendFormat("<td>{0}</td>", log["SourceName"]);
                    // Task
                    sb.AppendFormat("<td>{0}</td>", log["TaskName"]);
                    // Item-Name
                    sb.AppendFormat("<td>{0}</td>", log["ItemName"]);
                    // Execution-Log
                    if (showExecutionLog == 1)
                    {
                        string executionLog = FormatPlainTextExecutionLog(log["ExecutionLog"].ToString());
                        sb.AppendFormat("<td>{0}</td>", executionLog);
                    }
                    sb.AppendLine("</tr>");
                }
                sb.AppendLine("</table>");
            }
            sb.AppendLine("</p></body></html>");

            // send mail message
            int res = MailHelper.SendMessage(mailFrom, mailTo, mailSubject, sb.ToString(), true);

            if (res != 0)
            {
                TaskManager.WriteError("SMTP Error. Code: " + res.ToString());
            }
        }