public void CaptureException_DisabledClient_DoesNotCaptureEvent()
        {
            _ = _sut.IsEnabled.Returns(false);
            var id = _sut.CaptureException(new Exception());

            _ = _sut.DidNotReceive().CaptureEvent(Arg.Any <SentryEvent>());
            Assert.Equal(default, id);
Esempio n. 2
0
        private async Task RunWorkerAsync(CancellationToken token)
        {
            while (true)
            {
                try
                {
                    await Task.Delay(1000, token).ConfigureAwait(false);
                }
                catch (TaskCanceledException)
                {
                    break;
                }

                var payload = await FetchLatestWorkAsync().ConfigureAwait(false);

                if (payload == null)
                {
                    continue;
                }

                if (payload.GetTimeRemaining().TotalSeconds > 0)
                {
                    await RequeueWorkAsync(payload, false).ConfigureAwait(false);

                    continue;
                }

                if (!taskCallbacks.TryGetValue(payload.TaskName, out var worker))
                {
                    Log.Warning($"task '{payload.TaskName}' was not registered with a proper handler.");
                    continue;
                }

                try
                {
                    using var context = new ContextObject(app.Services);
                    await worker(context, payload.PayloadJson).ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    Log.Error(e);
                    sentryClient?.CaptureException(e);
                }

                if (payload.IsRepeating)
                {
                    payload.StartTime = DateTime.UtcNow;
                    payload.TimeEpoch = payload.StartTime.Add(payload.Duration);
                    await RequeueWorkAsync(payload, true).ConfigureAwait(false);
                }
                else
                {
                    await DeletePayloadAsync(payload).ConfigureAwait(false);
                }
            }
        }
Esempio n. 3
0
        private void Update(object state)
        {
            try
            {
                lock (Lock)
                {
                    _logger.LogInformation("Starting showtimes update.");
                    var showTimesAgeHours = _showTimesProvider.GetAgeHours();
                    if (showTimesAgeHours != null)
                    {
                        _dataDogClient.SendGauge("kinodnes.showtimesage", showTimesAgeHours.Value).Wait();
                    }


                    if (showTimesAgeHours < 4)
                    {
                        _logger.LogInformation("Showtimes age is less than 4 hours, not updating it.");
                        return;
                    }


                    var cinemas = _csfdDataProvider.GetAllShowTimes().ToList();
                    if (cinemas.Any())
                    {
                        _showTimesProvider.Set(cinemas);
                    }
                    else
                    {
                        throw new Exception("Failed to get any showtimes");
                    }
                    _logger.LogInformation("Showtimes update finished.");
                }
            }
            catch (Exception e)
            {
                _logger.LogCritical(e, e.Message);
                _sentryClient?.CaptureException(e);
            }
        }
Esempio n. 4
0
        public async Task CheckAsync(IDiscordMessage e)
        {
            if (e.Author.IsBot)
            {
                return;
            }

            if (experienceLock.CurrentCount == 0)
            {
                return;
            }

            try
            {
                using var scope = app.Services.CreateScope();
                var services = scope.ServiceProvider;

                if (e is IDiscordGuildMessage guildMessage)
                {
                    var key = GetContextKey(guildMessage.GuildId, e.Author.Id);
                    if (lastTimeExpGranted
                        .GetOrAdd(e.Author.Id, DateTime.Now)
                        .AddMinutes(1) < DateTime.Now)
                    {
                        var bonusExp  = MikiRandom.Next(1, 4);
                        var expObject = new ExperienceAdded
                        {
                            UserId     = (long)e.Author.Id,
                            GuildId    = (long)guildMessage.GuildId,
                            Experience = bonusExp,
                            Name       = e.Author.Username,
                        };

                        int currentLocalExp = await cache.GetAsync <int>(key);

                        if (currentLocalExp == 0)
                        {
                            var dbContext  = services.GetService <DbContext>();
                            var expProfile = await GetOrCreateExperienceProfileAsync(
                                dbContext, e.Author as IDiscordGuildUser);

                            await UpdateCacheExperienceAsync(expObject);

                            currentLocalExp = expProfile.Experience;
                        }

                        currentLocalExp += bonusExp;
                        experienceQueue.AddOrUpdate(e.Author.Id, expObject, (_, experience) =>
                        {
                            experience.Experience += expObject.Experience;
                            return(experience);
                        });

                        int level = User.CalculateLevel(currentLocalExp);
                        if (User.CalculateLevel(currentLocalExp - bonusExp) != level)
                        {
                            await LevelUpLocalAsync(e, level)
                            .ConfigureAwait(false);
                        }

                        lastTimeExpGranted.AddOrUpdate(
                            e.Author.Id, DateTime.Now, (x, d) => DateTime.Now);
                        await UpdateCacheExperienceAsync(expObject);
                    }
                }

                if (DateTime.Now >= this.lastDbSync + new TimeSpan(0, 1, 0))
                {
                    try
                    {
                        await experienceLock.WaitAsync();

                        Log.Message($"Applying Experience for {this.experienceQueue.Count} users");
                        this.lastDbSync = DateTime.Now;
                        var context = services.GetService <DbContext>();

                        await UpdateGlobalDatabaseAsync(context);
                        await UpdateLocalDatabaseAsync(context);
                        await UpdateGuildDatabaseAsync(context);

                        await context.SaveChangesAsync();
                    }
                    catch (Exception ex)
                    {
                        Log.Error(ex.Message + "\n" + ex.StackTrace);
                        sentryClient.CaptureException(ex);
                    }
                    finally
                    {
                        this.experienceQueue.Clear();
                        experienceLock.Release();
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                sentryClient.CaptureException(ex);
            }
        }