public override void SetRangeInHash(string key, IEnumerable <KeyValuePair <string, string> > keyValuePairs) { Guard.ArgumentNotNull(key, "key"); Guard.ArgumentNotNull(keyValuePairs, "keyValuePairs"); foreach (var kvp in keyValuePairs) { var local = kvp; QueueCommand(() => { var hash = _data.GetEnumeration <HashDto>().SingleOrDefault(h => h.Key == key && h.Field == local.Key); if (hash == null) { hash = new HashDto { Id = AutoIncrementIdGenerator.GenerateId(typeof(HashDto)), Key = key, Field = local.Key }; _data.Create(hash); } hash.Value = local.Value; }); } }
public override void AddJobState(string jobId, IHangfireState state) { QueueCommand(() => { var job = _data.Get <JobDto>(jobId); if (job == null) { return; } DateTime createdAt = DateTime.UtcNow; Dictionary <string, string> serializedStateData = state.SerializeData(); var stateData = new StateDto { Id = AutoIncrementIdGenerator.GenerateId(typeof(StateDto)), JobId = jobId, Name = state.Name, Reason = state.Reason, CreatedAt = createdAt, Data = SerializationHelper.Serialize(serializedStateData, null, SerializationOption.User) }; var stateHistory = new StateHistoryDto { StateName = state.Name, CreatedAt = createdAt, Reason = state.Reason, Data = serializedStateData }; job.History.Add(stateHistory); }); }
public override string CreateExpiredJob(Job job, IDictionary <string, string> parameters, DateTime createdAt, TimeSpan expireIn) { Guard.ArgumentNotNull(job, "job"); Guard.ArgumentNotNull(parameters, "parameters"); var invocationData = InvocationData.Serialize(job); var jobData = new JobDto { Id = Guid.NewGuid().ToString(), InvocationData = JobHelper.ToJson(invocationData), Arguments = invocationData.Arguments, CreatedAt = createdAt, ExpireAt = createdAt.Add(expireIn) }; _data.Create(jobData); if (parameters.Count > 0) { var list = parameters.Select(kvp => new JobParameterDto { Id = AutoIncrementIdGenerator.GenerateId(typeof(JobParameterDto)), JobId = jobData.Id, Name = kvp.Key, Value = kvp.Value }).ToList(); jobData.Parameters = list; } return(jobData.Id); }
public override void SetJobParameter(string id, string name, string value) { Guard.ArgumentNotNull(id, "id"); Guard.ArgumentNotNull(name, "name"); var jobData = _data.Get <JobDto>(id); if (jobData == null) { return; } var parameter = jobData.Parameters.Where(p => p.Name == name).FirstOrDefault(); if (parameter == null) { parameter = new JobParameterDto { Id = AutoIncrementIdGenerator.GenerateId(typeof(JobParameterDto)), JobId = id, Name = name }; jobData.Parameters.Add(parameter); } parameter.Value = value; }
public static CounterDto IncrementCounter(string key, bool decrement) { var counter = Data.GetEnumeration <CounterDto>() .Where(c => c.Key == key) .OrderByDescending(c => c.Value) .FirstOrDefault(); if (counter != null) { counter.Value = counter.Value + (decrement ? -1 : 1); } else { counter = new CounterDto { Id = AutoIncrementIdGenerator.GenerateId(typeof(ListDto)), Key = key, Value = (decrement ? 0 : 1) }; Data.Create(counter); } return(counter); }
public void Execute(CancellationToken cancellationToken) { var removedCount = 0; do { var counters = Data.GetEnumeration <CounterDto>().Take(NumberOfRecordsInSinglePass).ToList(); var groupedCounters = counters.GroupBy(c => c.Key).Select(g => new { g.Key, Value = g.Sum(c => c.Value), ExpireAt = g.Max(c => c.ExpireAt) }); foreach (var counter in groupedCounters) { var aggregate = Data.GetEnumeration <AggregatedCounterDto>() .FirstOrDefault(a => a.Key == counter.Key); if (aggregate == null) { aggregate = new AggregatedCounterDto { Id = AutoIncrementIdGenerator.GenerateId(typeof(AggregatedCounterDto)), Key = counter.Key, Value = 0, ExpireAt = DateTime.MinValue }; Data.Create(aggregate); } aggregate.Value += counter.Value; if (counter.ExpireAt > aggregate.ExpireAt) { aggregate.ExpireAt = counter.ExpireAt; } } removedCount = counters.Count; Data.Delete(counters); if (removedCount > 0) { cancellationToken.WaitHandle.WaitOne(DelayBetweenPasses); cancellationToken.ThrowIfCancellationRequested(); } } while (removedCount != 0); cancellationToken.WaitHandle.WaitOne(_aggregateInterval); }
public override void InsertToList(string key, string value) { QueueCommand(() => { var list = new ListDto { Id = AutoIncrementIdGenerator.GenerateId(typeof(ListDto)), Key = key, Value = value }; _data.Create(list); }); }
public override void AddToQueue(string queue, string jobId) { QueueCommand(() => { var jobQueue = new JobQueueDto { Id = AutoIncrementIdGenerator.GenerateId(typeof(JobQueueDto)), Queue = queue, AddedAt = DateTime.UtcNow, JobId = jobId }; _data.Create(jobQueue); }); }
public override void AddRangeToSet(string key, IList <string> items) { this.QueueCommand(() => { var existingSet = _data.GetEnumeration <SetDto>().Where(s => s.Key == key).Select(s => s.Value).ToList(); foreach (var item in items.Where(i => !existingSet.Contains(i))) { var newSet = new SetDto { Id = AutoIncrementIdGenerator.GenerateId(typeof(SetDto)), Key = key, Value = item }; _data.Create(newSet); } }); }
public override void AddToSet(string key, string value, double score) { QueueCommand(() => { var set = _data.GetEnumeration <SetDto>().SingleOrDefault(s => s.Key == key && s.Value == value); if (set == null) { set = new SetDto { Id = AutoIncrementIdGenerator.GenerateId(typeof(SetDto)), Key = key, Value = value }; _data.Create(set); } set.Score = (long)score; }); }