コード例 #1
0
ファイル: TaskSchedule.cs プロジェクト: lulzzz/Gofer.NET
        /// <summary>
        /// Returns true if the task is run.
        /// </summary>
        public bool RunIfScheduleReached()
        {
            var backendLock = _backend.LockBlocking(LockKey);

            try
            {
                var lastRunTime = GetLastRunTime(LastRunValueKey);

                // If we've already run before, and aren't recurring, dont run again.
                if (lastRunTime.HasValue && !IsRecurring)
                {
                    return(true);
                }

                if (TaskShouldExecuteBasedOnSchedule(lastRunTime ?? _startTime))
                {
                    SetLastRunTime();
                    LogScheduledTaskRun();

                    _taskInfo.ExecuteTask();
                    return(true);
                }
            }
            finally
            {
                backendLock.Release();
            }

            return(false);
        }
コード例 #2
0
            public void RunIfIntervalReachedSinceLastRun(string taskId)
            {
                var lockKey         = $"{nameof(TaskSchedule)}::{taskId}::ScheduleLock";
                var lastRunValueKey = $"{nameof(TaskSchedule)}::{taskId}::LastRunValue";

                var backendLock = _backend.LockBlocking(lockKey);

                try
                {
                    var lastRunTime = GetLastRunTime(lastRunValueKey);

                    var difference = DateTime.Now - lastRunTime;
                    if (difference > Interval)
                    {
                        SetLastRunTime(lastRunValueKey);
                        LogScheduledTaskRun();

                        TaskInfo.ExecuteTask();
                    }
                }
                finally
                {
                    backendLock.Release();
                }
            }