private readonly long _tickDuration; // a timespan expressed as ticks /// <summary> /// TBD /// </summary> /// <param name="scheduler">TBD</param> /// <param name="log">TBD</param> /// <exception cref="ArgumentOutOfRangeException">TBD</exception> public HashedWheelTimerScheduler(Config scheduler, ILoggingAdapter log) : base(scheduler, log) { var ticksPerWheel = SchedulerConfig.GetInt("akka.scheduler.ticks-per-wheel"); var tickDuration = SchedulerConfig.GetTimeSpan("akka.scheduler.tick-duration"); if (tickDuration.TotalMilliseconds < 10.0d) { throw new ArgumentOutOfRangeException("minimum supported akka.scheduler.tick-duration on Windows is 10ms"); } // convert tick-duration to ticks _tickDuration = tickDuration.Ticks; // Normalize ticks per wheel to power of two and create the wheel _wheel = CreateWheel(ticksPerWheel, log); _mask = _wheel.Length - 1; // prevent overflow if (_tickDuration >= long.MaxValue / _wheel.Length) { throw new ArgumentOutOfRangeException("akka.scheduler.tick-duration", _tickDuration, $"akka.scheduler.tick-duration: {_tickDuration} (expected: 0 < tick-duration in ticks < {long.MaxValue / _wheel.Length}"); } _shutdownTimeout = SchedulerConfig.GetTimeSpan("akka.scheduler.shutdown-timeout"); }
/// <summary> /// Ondemand thread. /// </summary> private void DoWork() { StartSignal = DateTime.MinValue; LastEndTime = DateTime.MinValue; while (true) { if (scheduler.GetCurrentlyExecutingJobs().Count == 0 && !paused) { scheduler.PauseAll(); if (DateTime.Compare(StartSignal, LastEndTime) > 0) { running = true; StartSignal = DateTime.Now; LastEndTime = StartSignal; SchedulerConfig schedulerConfig = new SchedulerConfig(runConfigurationFile); if (schedulerConfig != null) { schedulerConfig.RunOnDemand(); } else { logger.Error("Scheduler configuration not found."); throw new JobExecutionException("Scheduler configuration not found."); } running = false; } scheduler.ResumeAll(); } // 5 second delay Thread.Sleep(5000); } }
/// <summary> /// TBD /// </summary> /// <param name="config">TBD</param> /// <param name="log">TBD</param> public DedicatedThreadScheduler(Config config, ILoggingAdapter log) : base(config, log) { var precision = SchedulerConfig.GetTimeSpan("akka.scheduler.tick-duration"); _shutdownTimeout = SchedulerConfig.GetTimeSpan("akka.scheduler.shutdown-timeout"); var thread = new Thread(_ => { var allWork = new List <ScheduledWork>(); while (_stopped.Value == null) { Thread.Sleep(precision); var now = HighResMonotonicClock.Ticks; ScheduledWork work; while (_workQueue.TryDequeue(out work)) { //has work already expired? if (work.TickExpires < now) { work.Action(); } else { //buffer it for later allWork.Add(work); } } //this is completely stupid, but does work.. if (allWork.Count > 0) { var tmp = allWork; allWork = new List <ScheduledWork>(); foreach (var bufferedWork in tmp) { if (bufferedWork.TickExpires < now) { bufferedWork.Action(); } else { allWork.Add(bufferedWork); } } } } // shutdown has been signaled FireStopSignal(); }) { IsBackground = true }; thread.Start(); }
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); DependencyResolver.SetResolver(new tms_mka_v2.Infrastructure.NinjectDependencyResolver()); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); SchedulerConfig.Start(); ClientDataTypeModelValidatorProvider.ResourceClassKey = "MyGlobalErrors"; DefaultModelBinder.ResourceClassKey = "MyGlobalErrors"; ModelBinders.Binders.Add(typeof(DateTime?), new DateCustomBinder()); ModelBinders.Binders.Add(typeof(Decimal?), new DecimalCustomBinder()); }
/// <summary> /// 创建一个scheduler(自定义调度器的相关参数) /// </summary> /// <param name="schedulerConfig"></param> public bool CreateScheduler(SchedulerConfig schedulerConfig) { try { var pool = new DefaultThreadPool() { ThreadCount = schedulerConfig.ThreadCount }; _factory.CreateScheduler(schedulerConfig.SchedulerName, schedulerConfig.SchedulerId, pool, new RAMJobStore()); return(true); } catch (Exception ex) { return(false); } }
public bool SetAlarm(int hoursFromNow, string alarmMessage, bool useTTS, TimeSpan repeatInterval, bool repeat = false) { if (hoursFromNow <= 0 || string.IsNullOrEmpty(alarmMessage)) { return(false); } string guid = Guid.NewGuid().ToString(); AlarmConfig alarm = new AlarmConfig() { AlarmAt = DateTime.Now.AddHours(hoursFromNow), AlarmGuid = guid, AlarmMessage = alarmMessage, ShouldRepeat = repeat, ShouldOverideSoundSetting = false, ShouldUseTTS = useTTS }; SchedulerConfig config = new SchedulerConfig() { Guid = guid, ScheduledSpan = TimeSpan.FromHours(hoursFromNow), RepeatInterval = repeatInterval }; config.SchedulerObjects.Add(alarm); if (alarm.Scheduler != null && alarm.Scheduler.SetScheduler(config)) { alarm.Scheduler.ScheduledTimeReached += OnScheduledTimeReached; Alarms.TryAdd(alarm, config); Logger.Log($"An alarm has been set at {hoursFromNow} hours from now."); return(true); } Logger.Log("Failed to set alarm.", LogLevels.Warn); return(false); }
private void OnScheduledTimeReached(object sender, ScheduledTaskEventArgs e) { if (Alarms.Count <= 0) { return; } if (sender == null || e == null) { return; } AlarmConfig?configToRemove = new AlarmConfig(); foreach (KeyValuePair <AlarmConfig, SchedulerConfig> alarmConfig in Alarms) { if (alarmConfig.Key == null || alarmConfig.Value == null) { continue; } if (alarmConfig.Value.Guid != null && alarmConfig.Value.Guid.Equals(e.Guid)) { Logger.Log($"ALARM >>> {alarmConfig.Key.AlarmMessage}"); if (alarmConfig.Key.ShouldUseTTS) { Task.Run(async() => await TTS.SpeakText($"Sir, {alarmConfig.Key.AlarmMessage}", true).ConfigureAwait(false)); } if (alarmConfig.Key.AlarmGuid != null) { Helpers.InBackgroundThread(async() => await PlayAlarmSound(alarmConfig.Key.AlarmGuid).ConfigureAwait(false)); } if (alarmConfig.Key.ShouldRepeat && !alarmConfig.Key.Snooze) { alarmConfig.Key.Scheduler = null; alarmConfig.Key.Scheduler = new Scheduler(); if (e.SchedulerConfig != null) { SchedulerConfig config = new SchedulerConfig() { Guid = e.Guid, ScheduledSpan = e.SchedulerConfig.RepeatInterval, RepeatInterval = e.SchedulerConfig.RepeatInterval }; config.SchedulerObjects.Add(alarmConfig.Key); alarmConfig.Key.Scheduler.SetScheduler(config); alarmConfig.Key.Scheduler.ScheduledTimeReached += OnScheduledTimeReached; Logger.Log($"Alarm will repeat exactly after {e.SchedulerConfig.RepeatInterval.Hours} from now."); } } else { configToRemove = alarmConfig.Key; } } } if (configToRemove != null) { Alarms.Remove(configToRemove); } }