public string StartLanCopyFileJob(BackupConfiguration item) { string jobId = $"{item.Id}";// _{Convert.ToBase64String(item.RowVersion)}"; Exception localEx = null; try { var msg = $"({item.Id}) - START"; logger.Debug(msg); webapi.LogActivity(item.Id, msg, string.Empty, "OK"); ReportAgentStatus(StatusType.Starting, $"Starting copy operation - ({item.Id})", 0, item.Id); if (item.SrcUser.IndexOf(@"@") < 0) { throw new ArgumentException("Source user must be in format john@computer"); } if (item.DestUser.IndexOf(@"@") < 0) { throw new ArgumentException("Destination user must be in format john@computer"); } string domainSrc = item.SrcUser.Substring(item.SrcUser.IndexOf(@"@") + 1); string usernameSrc = item.SrcUser.Substring(0, item.SrcUser.IndexOf(@"@")); string domainDest = item.DestUser.Substring(item.DestUser.IndexOf(@"@") + 1); string usernameDest = item.DestUser.Substring(0, item.DestUser.IndexOf(@"@")); Exception outEx; if (!CopyFolderAndFilesWithImpersonation(item.Id, item.SrcFolder, domainSrc, usernameSrc, item.SrcPass, item.DestLanFolder, domainDest, usernameDest, item.DestPass, out outEx)) { if (outEx != null) { localEx = outEx; logger.Error($"CopyFolderAndFilesWithImpersonation ERR : {outEx}"); ReportAgentStatus(StatusType.Error, "Error... " + outEx.Message, 100); } } else { ReportAgentStatus(StatusType.Idle, $"Finished - ({item.Id})...", 100); } } catch (Exception ex) { localEx = ex; logger.Error($"Background job ERR : {ex}"); ReportAgentStatus(StatusType.Error, $"Error - ({item.Id}) - " + ex.Message, 0); } finally { var msg = $"({item.Id}) - END"; logger.Debug(msg); webapi.LogActivity(item.Id, msg, localEx != null ? localEx.Message : string.Empty, localEx != null ? "ERR" : "OK"); } return(jobId); }
private async void TimerRefreshFromWebApi_Elapsed(object sender, ElapsedEventArgs e) { try { logger.Trace(string.Format("WebApi refresh at {0}", DateTime.Now.ToString("o"))); List <string> activeJobs = new List <string>(); var result = await webapi.LoadBackupConfigurations(); if (result != null) { foreach (var item in result) { try { string jobId = $"{item.Id}";// _{Convert.ToBase64String(item.RowVersion)}"; if (item.IsActive.Value) { //RecurringJob. var recJob = JobStorage.Current.GetMonitoringApi().JobDetails(jobId); //RecurringJob.AddOrUpdate(jobId, () => LanCopyFileJob(item), item.Crontab); if (recJob == null) { RecurringJob.AddOrUpdate(jobId, () => DoCopyJob(item), item.Crontab); } activeJobs.Add(jobId); RecurringJob.Trigger(jobId); } else { //stopping inactive var recJob = JobStorage.Current.GetMonitoringApi().JobDetails(jobId); if (recJob != null) { RecurringJob.RemoveIfExists(jobId); } } } catch (Exception ex) { logger.Error($"Reccuring jobs refresh failed: {ex}"); //report error to webAPI webapi.LogActivity(item.Id, $"Reccuring jobs refresh failed -({item.Id})- see Err", ex.Message, "ERR"); } } } //TODO - purge ofphan jobs //JobStorage.Current.GetMonitoringApi().PurgeOrfanJobsExceptList(activeJobs); } catch (Exception ex) { logger.Error($"ERR WebApi refresh: {ex}"); //report error to webAPI webapi.LogActivity(string.Empty, "Reccuring jobs refresh final failed - see Err", ex.Message, "ERR"); } }