public override void Start() { Assert.IsFalse(IsRunning); Assert.IsNotNull(Synchronization); Assert.IsNotNull(Synchronization.StorageAccount); Assert.AreEqual(Models.EStorageAccountType.AmazonS3, Synchronization.StorageAccountType); AmazonS3AccountRepository dao = new AmazonS3AccountRepository(); Models.AmazonS3Account s3account = dao.GetForReadOnly(Synchronization.StorageAccount.Id); // // Dispose and recycle previous objects, if needed. // if (TransferAgent != null) { TransferAgent.Dispose(); } if (TransferListControl != null) { TransferListControl.ClearTransfers(); } if (SyncAgent != null) { SyncAgent.Dispose(); } // // Setup agents. // AWSCredentials awsCredentials = new BasicAWSCredentials(s3account.AccessKey, s3account.SecretKey); TransferAgentOptions options = new TransferAgentOptions { UploadChunkSizeInBytes = Teltec.Everest.Settings.Properties.Current.UploadChunkSize * 1024 * 1024, }; TransferAgent = new S3TransferAgent(options, awsCredentials, s3account.BucketName, CancellationTokenSource.Token); TransferAgent.RemoteRootDir = TransferAgent.PathBuilder.CombineRemotePath("TELTEC_BKP", s3account.Hostname); RemoteObjects = new List <ListingObject>(4096); // Avoid small resizes without compromising memory. SyncAgent = new CustomSynchronizationAgent(TransferAgent); RegisterEventHandlers(Synchronization); Report.PlanType = "synchronization"; Report.PlanName = "No plan"; Report.BucketName = s3account.BucketName; Report.HostName = Synchronization.StorageAccount.Hostname; Report.SyncResults = SyncAgent.Results; // // Start the sync. // DoSynchronization(SyncAgent, Synchronization, Options); }
// ... #endregion #region Event handlers public override void OnStart(CustomSynchronizationAgent agent, Models.Synchronization sync) { base.OnStart(agent, sync); _daoSynchronization.Insert(sync); var message = string.Format("Synchronization started at {0}", StartedAt); Info(message); //StatusInfo.Update(SyncStatusLevel.OK, message); OnUpdate(new SyncOperationEvent { Status = SyncOperationStatus.Started, Message = message }); }
public void OnFailure(CustomSynchronizationAgent agent, Models.Synchronization sync, Exception exception) { IsRunning = false; var message = string.Format("Synchronization failed: {0}", exception != null ? exception.Message : "Exception not informed"); Error(message); //StatusInfo.Update(SyncStatusLevel.ERROR, message); sync.DidFail(); _daoSynchronization.Update(sync); OnUpdate(new SyncOperationEvent { Status = SyncOperationStatus.Failed, Message = message }); }
/// <summary> /// Implements the Dispose pattern /// </summary> /// <param name="disposing">Whether this object is being disposed via a call to Dispose /// or garbage collected.</param> protected override void Dispose(bool disposing) { if (!this._isDisposed) { if (disposing && _shouldDispose) { if (SyncAgent != null) { logger.Info("DISPOSING SyncAgent."); SyncAgent.Dispose(); SyncAgent = null; } } this._isDisposed = true; } base.Dispose(disposing); }
public void OnFinish(CustomSynchronizationAgent agent, Models.Synchronization sync, Exception ex = null) { IsRunning = false; switch (CurrentState) { default: { var message = string.Format("Synchronization failed: {0}", ex.Message); Warn(message); //StatusInfo.Update(SyncStatusLevel.WARN, message); Report.AddErrorMessage(ex.Message); Report.OperationStatus = OperationStatus.FAILED; sync.DidFail(); _daoSynchronization.Update(sync); OnUpdate(new SyncOperationEvent { Status = SyncOperationStatus.Failed, Message = message }); break; } case SyncOperationState.FINISHING: { SyncResults.Statistics stats = agent.Results.Stats; var message = string.Format("Synchronization finished! Stats: {0} files", stats.FileCount); Info(message); //StatusInfo.Update(SyncStatusLevel.OK, message); // TODO(jweyrich): Handle overall failure and cancelation during Sync? sync.DidComplete(); _daoSynchronization.Update(sync); OnUpdate(new SyncOperationEvent { Status = SyncOperationStatus.Finished, Message = message }); /* * switch (agent.Results.OverallStatus) * //switch (sync.Status) * { * default: throw new InvalidOperationException("Unexpected TransferStatus"); * case TransferStatus.CANCELED: * Report.OperationStatus = OperationStatus.CANCELED; * sync.WasCanceled(); * _daoSynchronization.Update(sync); * OnUpdate(new SyncOperationEvent { Status = SyncOperationStatus.Canceled, Message = message }); * break; * case TransferStatus.FAILED: * Report.OperationStatus = OperationStatus.FAILED; * sync.DidFail(); * _daoSynchronization.Update(sync); * OnUpdate(new SyncOperationEvent { Status = SyncOperationStatus.Failed, Message = message }); * break; * case TransferStatus.COMPLETED: * Report.OperationStatus = OperationStatus.COMPLETED; * sync.DidComplete(); * _daoSynchronization.Update(sync); * OnUpdate(new SyncOperationEvent { Status = SyncOperationStatus.Finished, Message = message }); * break; * } */ break; } } }
public virtual void OnStart(CustomSynchronizationAgent agent, Models.Synchronization sync) { IsRunning = true; sync.DidStart(); }
protected async void DoSynchronization(CustomSynchronizationAgent agent, Models.Synchronization sync, SyncOperationOptions options) { try { CurrentState = SyncOperationState.STARTING; OnStart(agent, sync); // // Synchronization // CurrentState = SyncOperationState.SYNCHRONIZING_FILES; { Task syncTask = agent.Start(TransferAgent.RemoteRootDir, true); { var message = string.Format("Synchronizing files started."); Info(message); //StatusInfo.Update(SyncStatusLevel.INFO, message); OnUpdate(new SyncOperationEvent { Status = SyncOperationStatus.Started, Message = message }); } try { await syncTask; } catch (Exception ex) { if (ex.IsCancellation()) { logger.Warn("Synchronizing files was canceled."); } else { logger.Log(LogLevel.Error, ex, "Caught exception during synchronizing files"); } if (syncTask.IsFaulted || syncTask.IsCanceled) { if (syncTask.IsCanceled) { OnCancelation(agent, sync, ex); // syncTask.Exception } else { OnFailure(agent, sync, ex); // syncTask.Exception } return; } } { var message = string.Format("Synchronizing files finished."); Info(message); //StatusInfo.Update(SyncStatusLevel.INFO, message); //OnUpdate(new SyncOperationEvent { Status = SyncOperationStatus.Finished, Message = message }); } { var message = string.Format("Estimated synchronization size: {0} files, {1}", RemoteObjects.Count(), FileSizeUtils.FileSizeToString(agent.Results.Stats.TotalSize)); Info(message); } } // // Database files saving // CurrentState = SyncOperationState.SAVING_TO_DATABASE; { Task saveTask = ExecuteOnBackround(() => { // Save everything. Save(CancellationTokenSource.Token); }, CancellationTokenSource.Token); { var message = string.Format("Database files saving started."); Info(message); } try { await saveTask; } catch (Exception ex) { if (ex.IsCancellation()) { logger.Warn("Database files saving was canceled."); } else { logger.Log(LogLevel.Error, ex, "Caught exception during database files saving"); } if (saveTask.IsFaulted || saveTask.IsCanceled) { if (saveTask.IsCanceled) { OnCancelation(agent, sync, ex); // saveTask.Exception } else { OnFailure(agent, sync, ex); // saveTask.Exception } return; } } { var message = string.Format("Database files saving finished."); Info(message); } } CurrentState = SyncOperationState.FINISHING; OnFinish(agent, sync); } catch (Exception ex) { OnFinish(agent, sync, ex); } }