Exemple #1
0
        public static void InsertJob(IDbConnection db, DatabaseDialect dialect, ScheduledJob job)
        {
            var sql = "INSERT INTO ScheduledJob " +
                      "(Priority, Attempts, Handler, RunAt) " +
                      "VALUES (@Priority, @Attempts, @Handler, @RunAt); ";

            sql           = AddInsertIdentity(dialect, sql);
            job.Id        = db.Execute(sql, job);
            job.CreatedAt = db.Query <DateTime>("SELECT CreatedAt FROM ScheduledJob WHERE Id = @Id", new { job.Id }).Single();
        }
Exemple #2
0
        public static void UpdateJob(IDbConnection db, ScheduledJob job)
        {
            const string sql = "UPDATE ScheduledJob SET " +
                               "Priority = @Priority, Attempts = @Attempts, LastError = @LastError, RunAt = @RunAt, " +
                               "FailedAt = @FailedAt, SucceededAt = @SucceededAt, LockedAt = @LockedAt, " +
                               "LockedBy = @LockedBy, UpdatedAt = @UpdatedAt " +
                               "WHERE Id = @Id";

            db.Execute(sql, job);
        }
Exemple #3
0
        internal static ScheduledJob CreateScheduledJob(dynamic job, int priority, DateTime?runAt)
        {
            var scheduled = new ScheduledJob
            {
                Priority = priority,
                Handler  = HandlerSerializer.Serialize(job),
                RunAt    = runAt
            };

            return(scheduled);
        }
Exemple #4
0
        private bool AttemptCycle(ScheduledJob job)
        {
            job.Attempts++;
            var success = Perform(job);

            if (!success)
            {
                var dueTime = DateTime.Now + Linger.IntervalFunction(job.Attempts);
                job.RunAt = dueTime;
            }
            return(success);
        }
Exemple #5
0
        public static RepeatInfo GetRepeatInfo(IDbConnection db, ScheduledJob job)
        {
            var result = db.Query("SELECT * FROM RepeatInfo WHERE ScheduledJobId = @Id", job).SingleOrDefault();

            if (result == null)
            {
                return(null);
            }
            var repeatInfo = new RepeatInfo(result.Start, new DatePeriod(result.PeriodFrequency, result.PeriodQuantifier));

            return(repeatInfo);
        }
Exemple #6
0
        private TaskScheduler AcquireScheduler(ScheduledJob job)
        {
            TaskScheduler scheduler;

            if (!_schedulers.TryGetValue(job.Priority, out scheduler))
            {
                scheduler = _queue.ActivateNewQueue(job.Priority);
                var factory = new TaskFactory(
                    _cancel.Token, TaskCreationOptions.LongRunning, TaskContinuationOptions.LongRunning, scheduler
                    );
                _schedulers.TryAdd(job.Priority, scheduler);
                _factories.TryAdd(scheduler, factory);
            }
            return(scheduler);
        }
Exemple #7
0
        internal bool AttemptJob(ScheduledJob job, bool persist = true)
        {
            if (_cancel.IsCancellationRequested)
            {
                return(false);
            }

            var success = AttemptCycle(job);

            if (persist)
            {
                SaveJobChanges(job, success);
            }

            _cancel.Token.ThrowIfCancellationRequested();

            return(success);
        }
Exemple #8
0
        private static void SaveJobChanges(ScheduledJob job, bool success)
        {
            if (!success)
            {
                if (JobWillFail(job))
                {
                    if (Linger.DeleteFailedJobs)
                    {
                        Linger.Backend.Delete(job);
                        return;
                    }
                    job.FailedAt = DateTime.Now;
                }
            }
            else
            {
                if (Linger.DeleteSuccessfulJobs)
                {
                    Linger.Backend.Delete(job);
                    return;
                }
                job.SucceededAt = DateTime.Now;
            }

            job.LockedAt = null;
            job.LockedBy = null;
            Linger.Backend.Save(job);

            // Spawn a new scheduled job using the repeat data
            if (success && job.RepeatInfo != null && job.RepeatInfo.NextOccurrence.HasValue)
            {
                job.Id               = 0;
                job.RunAt            = job.RepeatInfo.NextOccurrence;
                job.RepeatInfo.Start = job.RunAt;
                Linger.Backend.Save(job, job.RepeatInfo);
            }
        }
Exemple #9
0
        public static void AddToBatch(IDbConnection db, DatabaseDialect dialect, Batch batch, ScheduledJob job)
        {
            const string sql = "INSERT INTO BatchJob (BatchId, ScheduledJobId) VALUES (@BatchId, @ScheduledJobId)";

            db.Execute(sql, new { BatchId = batch.Id, ScheduledJobId = job.Id });
        }
Exemple #10
0
        public void Delete(ScheduledJob job)
        {
            var db = _connectionBuilder();

            Queries.DeleteJob(db, job);
        }
Exemple #11
0
 public Every(ScheduledJob job) : this(job, 1)
 {
     _job = job;
 }
Exemple #12
0
 public Every(ScheduledJob job, int n)
 {
     _job = job;
     _n   = n;
 }
Exemple #13
0
        private bool Perform(ScheduledJob job)
        {
            var            success = false;
            Perform        handler = null;
            IList <string> methods = null;

            try
            {
                // Acquire the handler
                handler = HandlerSerializer.Deserialize <object>(job.Handler).ActLike <Perform>();
                if (handler == null)
                {
                    job.LastError = "Missing handler";
                    return(false);
                }

                // Acquire and cache method manifest
                var handlerType = handler.GetType();
                if (!Cache.TryGetValue(handlerType, out methods))
                {
                    methods = handlerType.GetMethods().Select(m => m.Name).ToList();
                    Cache.Add(handlerType, methods);
                }

                _pending.TryAdd(handler, methods);

                // Before
                if (methods.Contains("Before"))
                {
                    handler.ActLike <Before>().Before();
                }

                // Perform
                success = handler.Perform();

                if (success)
                {
                    if (methods.Contains("Success"))
                    {
                        handler.ActLike <Success>().Success();
                    }
                }

                // Failure
                if (JobWillFail(job) && methods.Contains("Failure"))
                {
                    {
                        handler.ActLike <Failure>().Failure();
                    }
                }

                // After
                if (methods.Contains("After"))
                {
                    handler.ActLike <After>().After();
                }

                _pending.TryRemove(handler, out methods);
            }
            catch (OperationCanceledException)
            {
                job.LastError = "Cancelled";
            }
            catch (Exception ex)
            {
                job.LastError = ex.Message;
                if (methods != null && methods.Contains("Error"))
                {
                    handler.ActLike <Error>().Error(ex);
                }
            }
            return(success);
        }
Exemple #14
0
 private static bool JobWillFail(ScheduledJob job)
 {
     return(job.Attempts >= Linger.MaximumAttempts);
 }
Exemple #15
0
        public static void InsertRepeatInfo(IDbConnection db, DatabaseDialect dialect, ScheduledJob job, RepeatInfo info)
        {
            const string sql = "INSERT INTO RepeatInfo " +
                               "(ScheduledJobId, PeriodFrequency, PeriodQuantifier, Start, IncludeWeekends) " +
                               "VALUES (@ScheduledJobId, @PeriodFrequency, @PeriodQuantifier, @Start, @IncludeWeekends);";

            db.Execute(sql, new
            {
                ScheduledJobId = job.Id,
                info.PeriodFrequency,
                info.PeriodQuantifier,
                info.Start,
                info.IncludeWeekends
            });
        }
Exemple #16
0
        public static void UpdateRepeatInfo(IDbConnection db, DatabaseDialect dialect, ScheduledJob job, RepeatInfo info)
        {
            const string sql = "UPDATE RepeatInfo SET " +
                               "PeriodFrequency = @PeriodFrequency, " +
                               "PeriodQuantifier = @PeriodQuantifier, " +
                               "Start = @Start, " +
                               "IncludeWeekends = @IncludeWeekends " +
                               "WHERE ScheduledJobId = @ScheduledJobId;";

            db.Execute(sql, new
            {
                ScheduledJobId = job.Id,
                info.PeriodFrequency,
                info.PeriodQuantifier,
                info.Start,
                info.IncludeWeekends
            });
        }
Exemple #17
0
        public static void DeleteJob(IDbConnection db, ScheduledJob job)
        {
            const string sql = "DELETE FROM ScheduledJob WHERE Id = @Id; DELETE FROM RepeatInfo WHERE ScheduledJobId = @Id;";

            db.Execute(sql, job);
        }