Beispiel #1
0
        public JobList <FetchedJobDto> FetchedJobs([NotNull] string queue, int from, int perPage)
        {
            if (queue is null)
            {
                throw new ArgumentNullException(nameof(queue));
            }

            var provider      = _storage.GetQueueProvider(queue);
            var monitoringApi = provider.GetMonitoringApi();
            var ids           = monitoringApi.GetFetchedJobIds(queue, from, perPage);

            if (!ids.Any())
            {
                return(new JobList <FetchedJobDto>(
                           Array.Empty <KeyValuePair <string, FetchedJobDto> >()));
            }

            var keys = ids.Select(x =>
                                  long.Parse(x, NumberStyles.Integer, CultureInfo.InvariantCulture));

            var items = _storage.UseContext(context =>
                                            FetchedJobsFunc(context, keys).
                                            ToList());

            return(new JobList <FetchedJobDto>(
                       items.Select(x => new KeyValuePair <string, FetchedJobDto>(
                                        x.Key.ToString(CultureInfo.InvariantCulture),
                                        x.Value))));
        }
Beispiel #2
0
        public IList <string> GetEnqueuedJobIds([NotNull] string queue, int from, int perPage)
        {
            if (queue is null)
            {
                throw new ArgumentNullException(nameof(queue));
            }

            return(_storage.
                   UseContext(context =>
                              GetEnqueuedJobIdsFunc(context, queue, from, perPage).
                              ToList()).
                   Select(x => x.ToString(CultureInfo.InvariantCulture)).
                   ToList());
        }
        private void RemoveExpired <T>()
            where T : class, IExpirable
        {
            var type = typeof(T);

            _logger.Debug(CoreStrings.ExpirationManagerRemoveExpiredStarting(type.Name));

            UseLock(() =>
            {
                while (0 != _storage.UseContext(context =>
                {
                    var set = context.Set <T>();
                    var entitiesToRemove = set.
                                           Where(x => x.ExpireAt < DateTime.UtcNow).
                                           Take(BatchSize);
                    set.RemoveRange(entitiesToRemove);
                    return(context.SaveChanges());
                }))
                {
                    ;
                }
            });

            _logger.Trace(CoreStrings.ExpirationManagerRemoveExpiredCompleted(type.Name));
        }
 public void RemoveFromQueue()
 {
     lock (_lock)
     {
         _storage.UseContext(context =>
         {
             context.Remove(_queuedJob);
             try
             {
                 context.SaveChanges();
             }
             catch (DbUpdateConcurrencyException)
             {
                 // Someone else already has removed item, database wins
             }
         });
         _completed = true;
     }
 }
        public void Release([NotNull] string resource)
        {
            if (resource is null)
            {
                throw new ArgumentNullException(nameof(resource));
            }

            _storage.UseContext(context =>
            {
                context.Remove(new HangfireLock
                {
                    Id = resource,
                });
                try
                {
                    context.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    // Someone else already has deleted this record. Database wins.
                }
            });
        }
        public void Enqueue([NotNull] string queue, [NotNull] string jobId)
        {
            if (queue is null)
            {
                throw new ArgumentNullException(nameof(queue));
            }
            if (queue.Length == 0)
            {
                throw new ArgumentException(
                          CoreStrings.ArgumentExceptionCollectionCannotBeEmpty,
                          nameof(queue));
            }
            if (jobId is null)
            {
                throw new ArgumentNullException(nameof(jobId));
            }

            var id = long.Parse(jobId, CultureInfo.InvariantCulture);

            _storage.UseContext(context =>
            {
                context.Add(new HangfireQueuedJob
                {
                    JobId = id,
                    Queue = queue,
                });
                try
                {
                    context.SaveChanges();
                }
                catch (DbUpdateException exception)
                {
                    throw new InvalidOperationException(
                        CoreStrings.InvalidOperationExceptionJobDoesNotExists,
                        exception);
                }
            });
        }
Beispiel #7
0
        public override string CreateExpiredJob(
            [NotNull] Job job,
            [NotNull] IDictionary <string, string> parameters,
            DateTime createdAt,
            TimeSpan expireIn)
        {
            if (job is null)
            {
                throw new ArgumentNullException(nameof(job));
            }
            if (parameters is null)
            {
                throw new ArgumentNullException(nameof(parameters));
            }

            var invocationData = InvocationData.Serialize(job);

            var hangfireJob = new HangfireJob
            {
                CreatedAt      = createdAt,
                ExpireAt       = createdAt + expireIn,
                InvocationData = JobHelper.ToJson(invocationData),
                Parameters     = parameters.
                                 Select(x => new HangfireJobParameter
                {
                    Name  = x.Key,
                    Value = x.Value,
                }).
                                 ToList(),
            };

            return(_storage.UseContext(context =>
            {
                context.Add(hangfireJob);
                context.SaveChanges();
                return hangfireJob.Id.ToString(CultureInfo.InvariantCulture);
            }));
        }