예제 #1
0
        private void ShedulerThread(SchedulerParams schdlrParams)
        {
            try
            {
                _isRunning = true;
                _state     = ESchedulerState.Init;

                while (_isRunning)
                {
                    _state = ESchedulerState.RunningJobs;

                    RunJobs(schdlrParams);

                    _state = ESchedulerState.Delay;

                    Thread.Sleep(schdlrParams.SleepDelay * 1000);
                }
            }
            catch (Exception ex)
            {
                _logger.Log(EErrorType.Error, "Failed to start scheduler");
                _logger.Log(ex);
            }

            _state = ESchedulerState.Idle;
        }
예제 #2
0
        private void RunJobs(SchedulerParams schdlrParams)
        {
            // running jobs based on EST time
            DateTime easternNow = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, "Eastern Standard Time");

            foreach (var job in _jobs)
            {
                if (_isRunning)
                {
                    if (job.IsActive && easternNow - job.LastRun >= job.Interval && (job.Hour == null || job.Hour == easternNow.Hour) && (job.Minute == null || job.Minute == easternNow.Minute))
                    {
                        try
                        {
                            job.LastRun = easternNow;
                            foreach (var url in job.JobUrls)
                            {
                                _logger.Log(EErrorType.Info, string.Format("Calling job '{0}': URL - {1}", job.Name, url));
                                SendRequest(schdlrParams.ServicesHost, url, job.Method, job.RequestPayload);
                            }
                        }
                        catch (Exception ex)
                        {
                            _logger.Log(EErrorType.Error, string.Format("Error in job '{0}:'", job.Name));
                            _logger.Log(ex);
                        }
                    }
                }
            }
        }
예제 #3
0
        public object Any(StartScheduler request)
        {
            _logger.Log(EErrorType.Info, " ****** Call start: StartScheduler");

            StartSchedulerResponse response = new StartSchedulerResponse();

            try
            {
                TransferHeader(request, response);

                if (IsValidSessionToken(request))
                {
                    if (Global.Scheduler.CurrentState == Scheduler.ESchedulerState.Idle)
                    {
                        SchedulerParams schdParams = new SchedulerParams();
                        schdParams.SleepDelay   = Int32.Parse(ConfigurationManager.AppSettings["SchedulerInterval"]);
                        schdParams.ServicesHost = ConfigurationManager.AppSettings["ServicesHost"];

                        Global.Scheduler.Start(schdParams);

                        response.Success = true;
                    }
                    else
                    {
                        response.Success = false;
                        response.Errors.Add(new Interfaces.Error()
                        {
                            Code = Interfaces.EErrorCodes.SchedulerBusy, Type = Interfaces.EErrorType.Error, Message = string.Format("Scheduler is running, current state - {0}", Global.Scheduler.CurrentState)
                        });
                    }
                }
                else
                {
                    response.Success = false;
                    response.Errors.Add(new Error()
                    {
                        Code = EErrorCodes.InvalidSession, Type = EErrorType.Error, Message = "Invalid session token"
                    });
                }
            }
            catch (Exception ex)
            {
                _logger.Log(ex);
                response.Success = false;
                response.Errors.Add(new Error()
                {
                    Code    = EErrorCodes.GeneralError,
                    Type    = EErrorType.Error,
                    Message = string.Format("Unpexcted error: {0}", ex.Message)
                });
            }


            _logger.Log(EErrorType.Info, " ****** Call end: StartScheduler");
            return(response);
        }
예제 #4
0
        public bool Start(SchedulerParams schdlrParams)
        {
            bool result = false;

            try
            {
                _logger.Log(EErrorType.Info, "Starting scheduler");

                _errors.Clear();

                IResult prepResult = PrepareJobsList();
                if (prepResult != null && !prepResult.Success)
                {
                    _errors.AddRange(prepResult.Errors);
                    return(false);
                }

                _schedulerThread = new Task(() => ShedulerThread(schdlrParams));
                _schedulerThread.Start();

                result = true;
            }
            catch (Exception ex)
            {
                result = false;
                _logger.Log(EErrorType.Error, "Failed to start scheduler");
                _logger.Log(ex);

                _errors.Add(new Error()
                {
                    Code = EErrorCodes.GeneralError, Type = EErrorType.Error, Message = ex.Message
                });
            }

            return(result);
        }