예제 #1
0
        private static void LockTasks(List <ScheduledTask> tasks, IDbConnection db, IDbTransaction t)
        {
            const string sql  = @"
UPDATE ScheduledTask 
SET 
    LockedAt = @Now, 
    LockedBy = @User 
WHERE Id IN 
    @Ids
";
            var          now  = DateTime.Now;
            var          user = LockedIdentity.Get();

            db.Execute(sql, new
            {
                Now  = now,
                Ids  = tasks.Select(task => task.Id),
                User = user
            }, t);

            foreach (var task in tasks)
            {
                task.LockedAt = now;
                task.LockedBy = user;
            }
        }
예제 #2
0
        public IList <ScheduledTask> GetAndLockNextAvailable(int readAhead)
        {
            var all = _tasks.SelectMany(t => t.Value);

            // None locked, failed or succeeded, must be due, ordered by due time then priority
            var now = DateTimeOffset.UtcNow;

            var query = all
                        .Where(t => !t.FailedAt.HasValue && !t.SucceededAt.HasValue && !t.LockedAt.HasValue)
                        .Where(t => t.RunAt <= now)
                        .OrderBy(t => t.RunAt)
                        .ThenBy(t => t.Priority);

            var tasks = query.Count() > readAhead?query.Take(readAhead).ToList() : query.ToList();

            // Lock tasks:
            if (tasks.Any())
            {
                foreach (ScheduledTask scheduledTask in tasks)
                {
                    scheduledTask.LockedAt = now;
                    scheduledTask.LockedBy = LockedIdentity.Get();
                }
            }

            return(tasks);
        }