Exemplo n.º 1
0
        static bool canRun(SchedulerContext context, JobItem jobItem, DateTime start)
        {
            if (jobItem.LeaseExpire.GetValueOrDefault() > start)
            {
                return(false);
            }
            if (jobItem.AlwaysRun)
            {
                return(true);
            }

            var lastRun = jobItem.LastRun.GetValueOrDefault();
            var cron    = CrontabSchedule.TryParse(jobItem.Cron);

            if (cron == null)
            {
                if (jobItem.LastRunResult != "CRON ERROR")
                {
                    postJob(context, jobItem, -1, "CRON ERROR", 0);
                }
                return(false);
            }

            var nextRun = cron.GetNextOccurrence(lastRun);

            if (nextRun > start)
            {
                return(false);
            }

            jobItem.NextRun = cron.GetNextOccurrence(start);
            return(true);
        }
Exemplo n.º 2
0
        static async Task doJobAsync(SchedulerContext context, JobItem jobItem, DateTime start)
        {
            var cts      = new CancellationTokenSource(TimeSpan.FromMinutes(jobItem.LeaseMinutes));
            var response = await _client.GetAsync(jobItem.Url, cts.Token);

            var content = string.Empty;

            if (response.Content != null)
            {
                content = await response.Content.ReadAsStringAsync();
            }

            if (!jobItem.AlwaysRun)
            {
                postJob(context, jobItem, (int)response.StatusCode, content, (int)(DateTime.UtcNow - start).TotalSeconds);
            }
        }
Exemplo n.º 3
0
        static void postJob(SchedulerContext context, JobItem jobItem, int statusCode, string result, int runTime)
        {
            var jobLog = new JobLog
            {
                PartitionKey = TimeId.NewSortableId(),
                RowKey       = string.Empty,
                JobGroup     = jobItem.PartitionKey,
                JobName      = jobItem.RowKey,
                StatusCode   = statusCode,
                Result       = result,
                RunTime      = runTime,
            };

            context.JobLogs.Insert(jobLog, true);
            jobItem.LastRun           = DateTime.UtcNow;
            jobItem.LastRunResult     = jobLog.Result;
            jobItem.LastRunStatusCode = jobLog.StatusCode;
            if (jobLog.StatusCode >= 200 && jobLog.StatusCode < 400)
            {
                jobItem.LastSuccessRun = jobItem.LastRun;
            }
            context.JobItems.Replace(jobItem);
        }