async Task AddToQueueAsync(Visit visit, AdditionInfo info = null) { var entity = new PromiseEntity(visit); var entry = new PromiseEntry() { Visit = visit, Id = entity.Id = Guid.NewGuid(), LastRequestTime = entity.LastRequestTime = entity.CreateTime = DateTime.Now, RetryInternvals = entity.RetryInternvals = 1000, RequestCount = entity.RequestCount = 1 }; if (info != null) { entity.Category = info.Category; entity.BusinessId = info.BusinessId; } await this.Store.CreateAsync(entity); lock (this) { _NotCompletes.Enqueue(entry); if (_Thread == null) { _Thread = new Thread(new ThreadStart(this.Loop)); _Thread.Start(); } } }
async Task Retry(PromiseEntry entry) { await Ajax.InternalRequestAsync(entry.Visit, (r, v) => { var entity = PromiseEntity.FromVisit(v); entity.Id = entry.Id; entity.Id = entry.Id; entity.LastRequestTime = DateTime.Now; entity.RequestCount++; entity.IsSuccess = true; entity.Content = r.ToString(); this.Store.UpdateAsync(entity).Start(); }, (e, v) => { Task.Run(async() => { var entity = PromiseEntity.FromVisit(v); entity.Id = entry.Id; entity.LastRequestTime = DateTime.Now; entity.RequestCount++; entity.IsSuccess = false; entity.Exception = e.ToString(); await this.Store.UpdateAsync(entity); lock (this) { _NotCompletes.Enqueue(entry); } }); return(false); }); }
bool CheckRunable(PromiseEntry entity, DateTime now) { var escaped = now - entity.LastRequestTime; if (escaped.Milliseconds > entity.RetryInternvals) { return(true); } else { return(false); } }
public Promise(IPromiseStore promiseStore) { this.Store = promiseStore; var notCompletes = this.Store.ListWaitingPromises(); this._NotCompletes = new Queue <PromiseEntry>(); foreach (var entity in notCompletes) { var visit = entity.ToVisit(); var entry = new PromiseEntry() { Visit = visit, Id = entity.Id, LastRequestTime = entity.LastRequestTime, CreateTime = entity.CreateTime, RetryInternvals = entity.RetryInternvals, RequestCount = entity.RequestCount }; this._NotCompletes.Enqueue(entry); } }
void ComputeIntervals(PromiseEntry entity) { var start = entity.CreateTime; var last = entity.LastRequestTime; var d = (last - start); if (d.Days > 1) { //一天以后,一小时重发一次 entity.RetryInternvals = 1000 * 60 * 60; } else if (d.Hours > 1) { //一小时之后,30分钟重发一次 entity.RetryInternvals = 1000 * 60 * 30; } else if (d.Minutes > 20) { //20分钟后,每15分钟重发一次 entity.RetryInternvals = 1000 * 60 * 15; } if (d.Minutes > 10) { //20分钟后,每5分钟重发一次 entity.RetryInternvals = 1000 * 60 * 5; } else if (d.Minutes > 3) { //3分钟后,每1分半钟重发一次 entity.RetryInternvals = 1000 * 90; } else { //每30秒重发一次 entity.RetryInternvals = 1000 * 30; } }