Example #1
0
        public Timers(double updateInterval, TeamoEntry entry, TeamoContext context, IClientService clientService, ILogger logger)
        {
            _context       = context;
            _clientService = clientService;
            _entry         = entry;
            _logger        = logger;

            _semaphore = new System.Threading.SemaphoreSlim(1, 1);
            bool isUpdating = false;
            var  entryId    = _entry.Id.Value;

            // Update timer
            _updateTimer = new Timer
            {
                Interval  = updateInterval,
                AutoReset = true
            };
            _updateTimer.Elapsed += async(sender, e) =>
            {
                if (isUpdating)
                {
                    return;
                }
                Console.WriteLine($"[TIMER] Updating {entryId}");
                await ExclusiveAsync(async() =>
                {
                    var dbEntry = _context.GetEntry(entryId);
                    await _clientService.UpdateMessageAsync(dbEntry);
                });

                Console.WriteLine($"[TIMER] Update done {entryId}");
            };


            // Start timer
            _startTimer = new Timer
            {
                AutoReset = false
            };
            _startTimer.Elapsed += async(sender, e) =>
            {
                Console.WriteLine($"[TIMER] Starting finish {entryId}");
                _updateTimer.Stop();
                await ExclusiveAsync(async() =>
                {
                    _logger.LogInformation($"Creating start message for entry {entryId}");
                    var dbEntry = _context.GetEntry(entryId);
                    await _clientService.DeleteMessageAsync(entry.Message);
                    await _clientService.CreateStartMessageAsync(dbEntry);
                    await _context.DeleteAsync(entry.Id.Value);
                    EventHandler <ElapsedEventArgs> handler = TimerFinished;
                    handler?.Invoke(sender, e);
                    _logger.LogInformation($"Successfully created start message for {entryId}");
                });

                Console.WriteLine($"[TIMER] Finished {entryId}");
            };
        }
Example #2
0
 internal async Task StopAsync()
 {
     _updateTimer.Stop();
     _startTimer.Stop();
     await ExclusiveAsync(async() =>
     {
         var post = _context.GetEntry(_entry.Id.Value);
         await _clientService.DeleteMessageAsync(post.Message);
         await _context.DeleteAsync(_entry.Id.Value);
     });
 }