Beispiel #1
0
        private void Execute()
        {
            // get a manager for the scheduler
            YetaWFManager.MakeInitialThreadInstance(null);

            // TODO: Scheduler logging should not start during startup processing. This timer postpones it (but is not a good solution)

            // Because initialization is called during application startup, we'll wait before we
            // check for any scheduler items that may be due (just so app start isn't all too slow).
            try {
                Thread.Sleep(defaultStartupTimeSpan);
            } catch (ThreadInterruptedException) {
                // thread was interrupted because there is work to be done
            }

            SchedulerLog = new SchedulerLogging();
            SchedulerLog.Init();
            SchedulerLog.LimitTo(YetaWFManager.Manager);

            YetaWFManager.Syncify(async() => {  // there is no point in running the scheduler async
                await Logging.RegisterLoggingAsync(SchedulerLog);
            });

            Logging.AddTraceLog("Scheduler task started");

            // mark all scheduled items that are supposed to be run at application startup
            try {
                YetaWFManager.Syncify(async() => {  // there is no point in running the scheduler async
                    await RunStartupItemsAsync();
                });
            } catch (Exception exc) {
                Logging.AddErrorLog("An error occurred running startup items", exc);
            }

            for (;;)
            {
                TimeSpan delayTime = defaultTimeSpanNoTask;
                if (SchedulerSupport.Enabled)
                {
                    try {
                        YetaWFManager.Syncify(async() => {  // there is no point in running the scheduler async
                            delayTime = await RunItemsAsync();
                        });
                    } catch (Exception exc) {
                        delayTime = defaultTimeSpanError;
                        Logging.AddErrorLog("An error occurred in the scheduling loop.", exc);
                    }
                    if (delayTime < new TimeSpan(0, 0, 5))// at a few seconds
                    {
                        delayTime = new TimeSpan(0, 0, 5);
                    }
                    else if (delayTime > new TimeSpan(1, 0, 0, 0)) // max. 1 day
                    {
                        delayTime = new TimeSpan(1, 0, 0, 0);
                    }
                }
                try {
                    schedulingThreadRunning = false;
                    Logging.AddLog($"Waiting {delayTime}");
                    Thread.Sleep(delayTime);
                } catch (ThreadInterruptedException) {
                    // thread was interrupted because there is work to be done
                } catch (ThreadAbortException) { }
                finally {
                    schedulingThreadRunning = true;
                }
            }

            // This never really ends so we don't need to unregister logging
            //log.Shutdown();
            //Logging.UnregisterLogging(log);
        }