Exemple #1
0
        private async Task GetOrAddOneCooperateItem(CancellationToken cancellationToken, string oneId)
        {
            using (var cooperateContext = new CooperateContext(_options))
            {
                try
                {
                    var findRes = await cooperateContext.CooperateEntities.FindAsync(oneId);

                    if (findRes != null)
                    {
                        var item = _cooperateItems[oneId];
                        item.ExpireKeyValuePair   = new KeyValuePair <bool, DateTime>(false, findRes.ExpireDateTime);
                        _cooperateEntities[oneId] = findRes;
                    }
                    else
                    {
                        var now    = DateTime.UtcNow;
                        var entity = new CooperateEntity
                        {
                            Id = oneId,
                            LastModifyDateTime = now,
                            ExpireDateTime     = now + _cooperateRequest.AliveTimeSpan
                        };
                        var res = await cooperateContext.CooperateEntities.AddAsync(entity, cancellationToken);

                        await cooperateContext.SaveChangesAsync(cancellationToken);

                        _cooperateEntities[oneId] = entity;
                        var item = _cooperateItems[oneId];
                        item.ExpireKeyValuePair = new KeyValuePair <bool, DateTime>(true, entity.ExpireDateTime);
                    }
                }
                catch (DbUpdateException)
                {
                    var findRes = await cooperateContext.CooperateEntities.FindAsync(oneId);

                    if (findRes != null)
                    {
                        var item = _cooperateItems[oneId];
                        item.ExpireKeyValuePair   = new KeyValuePair <bool, DateTime>(false, findRes.ExpireDateTime);
                        _cooperateEntities[oneId] = findRes;
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
Exemple #2
0
        private async Task UpdateCooperateEntity(CancellationToken cancellationToken, string oneId, DateTime now)
        {
            using (var cooperateContext = new CooperateContext(_options))
            {
                try
                {
                    var entity     = _cooperateEntities[oneId];
                    var backEntity = new CooperateEntity
                    {
                        Id = entity.Id,
                        LastModifyDateTime = entity.LastModifyDateTime,
                        ExpireDateTime     = entity.ExpireDateTime
                    };

                    var nowEntity = await cooperateContext.CooperateEntities.FindAsync(oneId);

                    if (nowEntity.LastModifyDateTime > backEntity.LastModifyDateTime)
                    {
                        _cooperateEntities[oneId] = nowEntity;
                        var item = _cooperateItems[oneId];
                        item.ExpireKeyValuePair = new KeyValuePair <bool, DateTime>(false, nowEntity.ExpireDateTime);
                        return;
                    }
                    else
                    {
                        nowEntity.LastModifyDateTime = now;
                        nowEntity.ExpireDateTime     = now + _cooperateRequest.AliveTimeSpan;

                        cooperateContext.Update(nowEntity);
                        await cooperateContext.SaveChangesAsync(cancellationToken);

                        _cooperateEntities[oneId] = nowEntity;
                        var item = _cooperateItems[oneId];
                        item.ExpireKeyValuePair = new KeyValuePair <bool, DateTime>(true, nowEntity.ExpireDateTime);
                    }
                }
                catch (DbUpdateConcurrencyException)
                {
                    var entity = await cooperateContext.CooperateEntities.FindAsync(oneId);

                    _cooperateEntities[oneId] = entity;
                    var item = _cooperateItems[oneId];
                    item.ExpireKeyValuePair = new KeyValuePair <bool, DateTime>(false, entity.ExpireDateTime);
                }
            }
        }
Exemple #3
0
        private async Task InitCooperateRequestAsync(CancellationToken cancellationToken)
        {
            try
            {
                var ids        = _cooperateRequest.Ids;
                var random     = new Random();
                var orderedIds = ids.OrderBy(k => random.Next(ids.Count));
                try
                {
                    using (var cooperateContext = new CooperateContext(_options))
                    {
                        await cooperateContext.Database.EnsureCreatedAsync(cancellationToken);
                    }
                }
                catch
                {
                    // ignored
                }

                foreach (var oneId in orderedIds)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }

                    await GetOrAddOneCooperateItem(cancellationToken, oneId);

                    await Task.Delay(random.Next(20), cancellationToken);
                }
            }
            catch (Exception e)
            {
                _exceptionsInBackground.Add(e);
            }
        }