private async Task <bool> HandleUpdate(
            ScheduledPackageUpdate update,
            IPackageManager packages
            )
        {
            bool shouldUpdate = false;

            switch (update.Milestone)
            {
            case PackageUpdateMilestone.StartOfWeek:
            case PackageUpdateMilestone.Dependencies:
                shouldUpdate = DateTime.UtcNow.Date >= update.Week.WeekStart;
                break;

            case PackageUpdateMilestone.EndOfWeek:
                shouldUpdate = DateTime.UtcNow.Date >= update.Week.WeekEnd;
                break;

            default: break;
            }

            if (!shouldUpdate)
            {
                return(false);
            }

            await packages.UpdatePackageAsync(update.Package, update.Week, update.Milestone);

            return(true);
        }
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                this._logger.LogInformation("Performing updates.");

                using (var scope = this._services.CreateScope())
                {
                    var updateManager  = scope.ServiceProvider.GetRequiredService <IUpdateManager>();
                    var packageManager = scope.ServiceProvider.GetRequiredService <IPackageManager>();

                    ScheduledPackageUpdate update = await updateManager.GetNextUpdateAsync(null);

                    int?lastId = null;
                    while (update != null)
                    {
                        var updated = await this.HandleUpdate(update, packageManager);

                        if (updated)
                        {
                            await updateManager.FinaliseUpdateAsync(update);
                        }

                        lastId = update.ScheduledPackageUpdateId;
                        update = await updateManager.GetNextUpdateAsync(lastId);

                        if (updated)
                        {
                            await Task.Delay(Constants.PACKAGE_UPDATE_BETWEEN_DELAY, stoppingToken);
                        }
                    }
                }

                await Task.Delay(Constants.PACKAGE_UPDATE_CHECK_DELAY, stoppingToken);
            }
        }
Ejemplo n.º 3
0
 public Task FinaliseUpdateAsync(ScheduledPackageUpdate update)
 {
     this._db.Remove(update);
     return(this._db.SaveChangesAsync());
 }