Example #1
0
        public async Task RunSenderAsync(CancellationToken ct)
        {
            long currentRequestNo = -1;
            var  expiredTokens    = new List <long>();

            while (!ct.IsCancellationRequested)
            {
                try
                {
                    var token = await DequeueToken();

                    if (token != null)
                    {
                        if (expiredTokens.Count > 0)
                        {
                            await PushDB.ExpirePushTokensAsync(PushType.Apple, expiredTokens);

                            expiredTokens.Clear();
                        }

                        await Task.Delay(1000, ct);

                        continue;
                    }

                    if (token.Request.RequestNo != currentRequestNo)
                    {
                        // fail + 1
                    }

                    currentRequestNo = token.Request.RequestNo;

                    // send
                    var res = await _apnHttp.RequestAsync(token.Token, token.Alert);

                    if (res.StatusCode != HttpStatusCode.OK)
                    {
                        // fail + 1
                    }

                    if (res.StatusCode == HttpStatusCode.NotFound || res.StatusCode == HttpStatusCode.BadRequest)
                    {
                        expiredTokens.Add(token.Seq);
                        if (expiredTokens.Count > 100)
                        {
                            await PushDB.ExpirePushTokensAsync(PushType.Google, expiredTokens);

                            expiredTokens.Clear();
                        }
                    }
                }
                catch (Exception ex)
                {
                    _log.Error(ex.StackTrace);
                    await Task.Delay(1000);
                }
            }
        }
Example #2
0
        public async Task RunSchedulerAsync(CancellationToken ct)
        {
            while (!ct.IsCancellationRequested)
            {
                PushRequest request = await PushDB.RequestAllGetAsync(PushType.Google);

                if (request == null)
                {
                    await Task.Delay(5000, ct);

                    continue;
                }

                var dicLanguage = CheckAppleRequest(request);
                if (dicLanguage == null)
                {
                    await PushDB.RequestStateAsync(PushType.Google, request.RequestNo, PushState.Error);

                    continue;
                }

                long sentSeq = 0;

                while (!ct.IsCancellationRequested)
                {
                    List <PushToken> tokens = await PushDB.GetPushTokenAsync(PushType.Google, sentSeq, string.Empty, TokenLimit);

                    if (!tokens.Any())
                    {
                        await PushDB.RequestStateAsync(PushType.Google, request.RequestNo, PushState.Complete);

                        break;
                    }

                    sentSeq = tokens.Last().Seq;

                    await EnqueueTokens(ct, request, dicLanguage, tokens);

                    await PushDB.RequestSendAsync(PushType.Google, request.RequestNo, sentSeq, PushState.Sending);

                    _log.Info($"Send complete apple tokens | Last sent seq: {sentSeq}");
                }

                await Task.Delay(5000, ct);
            }
        }