Beispiel #1
0
 private void QueueFilesAsPending(IReadOnlyCollection <string> files, BackupFolderJob job)
 {
     foreach (var file in files)
     {
         var fileJob = new BackupFileUploadJob(job, new System.IO.FileInfo(file));
         _uploadProcessor.AddJob(fileJob);
     }
 }
Beispiel #2
0
        public async Task Execute(IJobExecutionContext jobContext)
        {
            Folder = (BackupFolder)jobContext.MergedJobDataMap["Folder"] ?? throw new ArgumentNullException(nameof(Folder));
            _logger.LogInformation("Executing Check Files Job, fired at {date} for folder {path}", jobContext.FireTimeUtc.ToLocalTime(), Folder.Path);
            var job = new BackupFolderJob(Folder);

            try
            {
                using (var context = _contextFactory.CreateContext())
                {
                    // check if already running a job for this folder and return if so
                    if (context.BackupJobs.Any(j => j.BackupFolderId == Folder.BackupFolderId &&
                                               (j.Status == BackupFolderJobStatus.InProgress || j.Status == BackupFolderJobStatus.Pending)))
                    {
                        _logger.LogInformation("Job found already in progress, skipping...");
                        return;
                    }

                    var files = await GetFilesToTransfer();

                    if (files.Count == 0)
                    {
                        _logger.LogInformation($"{Folder.Path} All Files Unchanged. Skipping Sync.");
                        return;
                    }
                    ;
                    _logger.LogInformation($"{Folder.Path} Found {files.Count} files that need to be transferred.");

                    context.Entry(job.Folder).State = EntityState.Unchanged;
                    job.NumFiles = files.Count;

                    context.Attach(Folder);
                    Folder.LastSync = DateTime.UtcNow;

                    await context.BackupJobs.AddAsync(job);

                    await context.SaveChangesAsync(jobContext.CancellationToken);

                    QueueFilesAsPending(files, job);
                }
            }
            catch (Exception ex)
            {
                using (var context = _contextFactory.CreateContext())
                {
                    context.BackupJobs.Attach(job);
                    job.Status = BackupFolderJobStatus.Errored;
                    await context.SaveChangesAsync();
                }

                _logger.LogCritical(ex, "Exception while running job: {message}. {innerException} \n {stackTrace}", ex.Message, ex.InnerException?.Message, ex.StackTrace);
                throw;
            }
        }