Exemple #1
0
        private void ExecuteScripts()
        {
            NCC.ClearView.Application.Core.Scheduler oScheduler = new NCC.ClearView.Application.Core.Scheduler(0, dsn);
            DataSet dsScripts = oScheduler.Gets(1);

            foreach (DataRow drScript in dsScripts.Tables[0].Rows)
            {
                // Check to see if it's ready to run
                string name   = drScript["name"].ToString();
                int    status = Int32.Parse(drScript["status"].ToString());
                bool   run    = false;

                if (status != SchedulerStatus.Running)
                {
                    if (status == SchedulerStatus.RunOnce)
                    {
                        if (intLogging >= 1)
                        {
                            oEventLog.WriteEntry(String.Format("Job \"" + name + "\" - Run Once has been selected."), EventLogEntryType.Information);
                        }
                        run = true; // run once is set
                    }
                    else
                    {
                        DateTime now = DateTime.Now;

                        // Check that schedule can run today
                        string[] days  = drScript["days"].ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        bool     dayOK = false;
                        foreach (string day in days)
                        {
                            string DAY = day.Trim().ToUpper();
                            if (intLogging >= 3)
                            {
                                oEventLog.WriteEntry(String.Format("Checking DAY = " + DAY), EventLogEntryType.FailureAudit);
                            }

                            int date = 0;
                            if (Int32.TryParse(DAY, out date))
                            {
                                // day is a number - check against date of month
                                if (date == now.Day)
                                {
                                    dayOK = true;
                                }
                            }
                            else
                            {
                                // day is NOT a number - check list of known values
                                if (DAY == "SU" && now.DayOfWeek == DayOfWeek.Sunday)
                                {
                                    dayOK = true;
                                }
                                else if (DAY == "MO" && now.DayOfWeek == DayOfWeek.Monday)
                                {
                                    dayOK = true;
                                }
                                else if (DAY == "TU" && now.DayOfWeek == DayOfWeek.Tuesday)
                                {
                                    dayOK = true;
                                }
                                else if (DAY == "WE" && now.DayOfWeek == DayOfWeek.Wednesday)
                                {
                                    dayOK = true;
                                }
                                else if (DAY == "TH" && now.DayOfWeek == DayOfWeek.Thursday)
                                {
                                    dayOK = true;
                                }
                                else if (DAY == "FR" && now.DayOfWeek == DayOfWeek.Friday)
                                {
                                    dayOK = true;
                                }
                                else if (DAY == "SA" && now.DayOfWeek == DayOfWeek.Saturday)
                                {
                                    dayOK = true;
                                }
                                else
                                {
                                    // last day of month
                                    DateTime endOfMonth = new DateTime(now.Year, now.Month, 1).AddMonths(1).AddDays(-1);
                                    if (DAY == "L" && now.Day == endOfMonth.Day)
                                    {
                                        dayOK = true;
                                    }
                                }
                            }
                            if (dayOK)
                            {
                                if (intLogging >= 2)
                                {
                                    oEventLog.WriteEntry(String.Format("DAY = " + DAY + " is Valid!"), EventLogEntryType.Information);
                                }
                                break;
                            }
                            else if (intLogging >= 3)
                            {
                                oEventLog.WriteEntry(String.Format("DAY = " + DAY + " is not valid"), EventLogEntryType.Warning);
                            }
                        }

                        // Finished with Days
                        if (dayOK)
                        {
                            // Check the time
                            string[] times  = drScript["times"].ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                            bool     timeOK = false;
                            foreach (string time in times)
                            {
                                if (String.IsNullOrEmpty(time) == false)
                                {
                                    if (intLogging >= 3)
                                    {
                                        oEventLog.WriteEntry(String.Format("Checking Time = " + time), EventLogEntryType.Information);
                                    }
                                    DateTime _tick = DateTime.Parse(time.Trim());
                                    if (_tick.ToShortTimeString() == now.ToShortTimeString())
                                    {
                                        timeOK = true;
                                        if (intLogging >= 2)
                                        {
                                            oEventLog.WriteEntry(String.Format("Time = " + time + " is Valid!"), EventLogEntryType.Information);
                                        }
                                        break;
                                    }
                                    else if (intLogging >= 3)
                                    {
                                        oEventLog.WriteEntry(String.Format("Time = " + time + " is not valid (" + now.ToShortTimeString() + ")"), EventLogEntryType.Warning);
                                    }
                                }
                            }

                            // Finished with Times
                            if (timeOK)
                            {
                                // all conditions met
                                run = true;
                            }
                        }
                    }
                }

                // Check to see if the job can be run
                if (run)
                {
                    if (intLogging >= 1)
                    {
                        oEventLog.WriteEntry(String.Format("Executing Job \"" + name + "\""), EventLogEntryType.Information);
                    }
                    ExecuteScript oExecuteScript       = new ExecuteScript(Int32.Parse(drScript["id"].ToString()), drScript["name"].ToString(), drScript["server"].ToString(), Int32.Parse(drScript["credentials"].ToString()), drScript["parameters"].ToString(), Int32.Parse(drScript["timeout"].ToString()), (drScript["privledges"].ToString() == "1"), (drScript["interactive"].ToString() == "1"), strScripts, dsn, intEnvironment, intLogging, oEventLog);
                    ThreadStart   oExecuteScriptStart  = new ThreadStart(oExecuteScript.Begin);
                    Thread        oExecuteScriptThread = new Thread(oExecuteScriptStart);
                    oExecuteScriptThread.Start();
                }
            }
        }
Exemple #2
0
        private void Script()
        {
            try
            {
                Variables oVariable = new Variables(environment);
                Functions oFunction = new Functions(0, dsn, environment);
                oScheduler = new NCC.ClearView.Application.Core.Scheduler(0, dsn);
                oScheduler.Status(id, SchedulerStatus.Running, DateTime.Now.ToString(), -1);

                if (String.IsNullOrEmpty(server) == false)
                {
                    server = "\\\\" + server;
                }
                Variables oCredentials = new Variables(credentials);
                string    user         = oCredentials.Domain() + "\\" + oCredentials.ADUser();
                string    pass         = oCredentials.ADPassword();
                int       intTimeout   = (timeout * 60 * 1000); // convert to milliseconds
                //int intReturn = 0;
                if (interactive)
                {
                    parameters = "-i " + parameters;
                }
                if (privledges)
                {
                    parameters = "-h " + parameters;
                }

                bool             boolTimeout = false;
                ProcessStartInfo infoPsExec  = new ProcessStartInfo(location + "psexec");
                infoPsExec.WorkingDirectory = location;
                string command = server + " -u " + user + " -p {0} " + parameters;
                infoPsExec.Arguments = string.Format(command, pass);
                LogIt("Job \"" + name + "\" Starting PSEXEC: " + location + "psexec " + string.Format(command, "***"), false);
                Process procPsExec = Process.Start(infoPsExec);
                procPsExec.WaitForExit(intTimeout);
                if (procPsExec.HasExited == false)
                {
                    LogIt("Job \"" + name + "\" Timed Out after " + timeout.ToString() + " minute(s)...", true);
                    oScheduler.Status(id, SchedulerStatus.Waiting, DateTime.Now.ToString(), 1);
                    procPsExec.Kill();
                    boolTimeout = true;
                }
                else
                {
                    if (procPsExec.ExitCode == 0)   // 0 = Success
                    {
                        LogIt("Job \"" + name + "\" Exited (" + procPsExec.ExitCode.ToString() + ")", false);
                        oScheduler.Status(id, SchedulerStatus.Waiting, DateTime.Now.ToString(), 0);
                    }
                    else
                    {
                        LogIt("Job \"" + name + "\" Exited (" + procPsExec.ExitCode.ToString() + ")", true);
                        oScheduler.Status(id, SchedulerStatus.Waiting, DateTime.Now.ToString(), 1);
                    }
                }
                if (boolTimeout)
                {
                    string strEMails = oFunction.GetGetEmailAlertsEmailIds("EMAILGRP_DEVELOPER_ERROR");
                    oFunction.SendEmail("Timeout", strEMails, "", "", "SSIS Job \"" + name + "\" Timeout", "This message is to inform you that job \"" + name + "\" timed out after " + intTimeout.ToString() + "minute(s)", false, true);
                }
                //if (boolTimeout == false)
                //    intReturn = procPsExec.ExitCode;
                procPsExec.Close();
            }
            catch (Exception ex)
            {
                try
                {
                    string strError = "Scheduler Service (" + name + "): " + "(Error Message: " + ex.Message + ") ~ (Source: " + ex.Source + ") (Stack Trace: " + ex.StackTrace + ") [" + System.Environment.UserName + "]";
                    LogIt(strError, true);
                }
                catch
                {
                }
            }
        }