2 using System.Collections.Generic;
3 using System.ComponentModel.Composition;
4 using System.Diagnostics;
5 using System.Diagnostics.Contracts;
9 using Pithos.Interfaces;
11 namespace Pithos.Core.Agents
14 public class WorkflowAgent
16 Agent<WorkflowState> _agent;
18 public IStatusNotification StatusNotification { get; set; }
20 public IStatusKeeper StatusKeeper { get; set; }
22 public IPithosWorkflow Workflow { get; set; }
25 public NetworkAgent NetworkAgent { get; set; }
29 _agent = Agent<WorkflowState>.Start(inbox =>
34 var message = inbox.Receive();
35 var process = message.ContinueWith(t =>
41 process.ContinueWith(t =>
46 var ex = t.Exception.InnerException;
47 if (ex is OperationCanceledException)
49 Trace.TraceError("[ERROR] Synch for {0}:\r{1}", message.Result.FileName, ex);
58 public void RestartInterruptedFiles()
60 StatusNotification.NotifyChange("Restart processing interrupted files", TraceLevel.Verbose);
61 var interruptedStates = new[] { FileOverlayStatus.Unversioned, FileOverlayStatus.Modified };
62 var filesQuery = from state in FileState.Queryable
63 where interruptedStates.Contains(state.OverlayStatus)
64 select new WorkflowState
66 Path = state.FilePath.ToLower(),
67 FileName = Path.GetFileName(state.FilePath).ToLower(),
68 Hash = state.Checksum,
69 Status = state.OverlayStatus == FileOverlayStatus.Unversioned ?
72 TriggeringChange = state.OverlayStatus == FileOverlayStatus.Unversioned ?
73 WatcherChangeTypes.Created :
74 WatcherChangeTypes.Changed
76 _agent.AddFromEnumerable(filesQuery);
81 private void Process(WorkflowState state)
85 string path = state.Path.ToLower();
86 string fileName = Path.GetFileName(path);
88 //Bypass deleted files, unless the status is Deleted
89 if (!(File.Exists(path) || state.Status != FileStatus.Deleted))
92 this.StatusKeeper.RemoveFileOverlayStatus(path);
98 case FileStatus.Created:
99 case FileStatus.Modified:
100 var info = new FileInfo(path);
101 NetworkAgent.Post(new CloudAction(CloudActionType.UploadUnconditional, info, ObjectInfo.Empty));
103 case FileStatus.Deleted:
104 NetworkAgent.Post(new CloudAction(CloudActionType.DeleteCloud, null, new ObjectInfo {Name=fileName}));
106 case FileStatus.Renamed:
107 NetworkAgent.Post(new CloudAction(CloudActionType.RenameCloud, state.OldFileName,state.OldPath,state.FileName,state.Path));
116 public void Post(WorkflowState workflowState)
118 _agent.Post(workflowState);