Esempio n. 1
0
        public async void _do()
        {
            await Task.Delay(2000);

            DateTime after = DateTime.MinValue;

            while (true)
            {
                // set up the wait
                _cancellationTokenSource?.Dispose();
                _cancellationTokenSource = new CancellationTokenSource();
                var sleepTime = await _getSleepTime(after);

                if (sleepTime != 0)
                {
                    try
                    {
                        if (sleepTime > 0)
                        {
                            _cancellationTokenSource.CancelAfter(sleepTime);
                        }

                        await _notifier.Subscribe(BackgroundTaskPath, (a) => _cancellationTokenSource?.Cancel());

                        await _connection.WaitAsync(_cancellationTokenSource.Token);
                    }
                    catch (OperationCanceledException) { }
                }
                _connection.Close(); // workaround for npgsql issue??
                await _connection.OpenAsync();

                var transaction = _connection.BeginTransaction();
                var nextAction  = await _connection.QuerySingleOrDefaultAsync <EventQueueItem>("SELECT * FROM \"EventQueue\" WHERE \"NextAttempt\" < @time order by \"NextAttempt\" limit 1 for update skip locked", new { time = DateTime.Now.AddMinutes(1) });

                _logger.LogDebug($"Next action: {nextAction?.Action ?? "nothing"}");

                if (nextAction == null)
                {
                    transaction.Rollback();
                    transaction.Dispose();
                    continue;
                }

                await BaseTask.Go(_connection, nextAction, _serviceProvider, transaction);
            }
        }
Esempio n. 2
0
        public async void _do()
        {
            await Task.Delay(2000);

            DateTime after = DateTime.MinValue;

            while (true)
            {
                // set up the wait
                _cancellationTokenSource?.Dispose();
                _cancellationTokenSource = new CancellationTokenSource();
                var sleepTime = await _getSleepTime(after);

                if (sleepTime != 0)
                {
                    try
                    {
                        await Task.Delay(sleepTime, _cancellationTokenSource.Token);
                    }
                    catch (TaskCanceledException) { }
                }
                var nextAction = await _context.EventQueue.OrderBy(a => a.NextAttempt).Where(a => a.NextAttempt > after).FirstOrDefaultAsync();

                _logger.LogDebug($"Next action: {nextAction?.Action ?? "nothing"}");
                if (nextAction == null)
                {
                    continue;
                }

                if (await _notifier.Synchronize(BackgroundTaskPath + ":" + nextAction.Id + nextAction.NextAttempt.ToString()))
                {
                    // we won!
                    await BaseTask.Go(_context, nextAction, _serviceProvider);
                }
                else
                {
                    after = nextAction.NextAttempt;
                }
            }
        }