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 //We should avoid processing files stored in the Fragments folder
23 //The Full path to the fragments folder is stored in FragmentsPath
24 public string FragmentsPath { get; set; }
27 public NetworkAgent NetworkAgent { get; set; }
31 _agent = Agent<WorkflowState>.Start(inbox =>
36 var message = inbox.Receive();
37 var process = message.ContinueWith(t =>
43 process.ContinueWith(t =>
48 var ex = t.Exception.InnerException;
49 if (ex is OperationCanceledException)
51 Trace.TraceError("[ERROR] Synch for {0}:\r{1}", message.Result.FileName, ex);
60 public void RestartInterruptedFiles()
63 StatusNotification.NotifyChange("Restart processing interrupted files", TraceLevel.Verbose);
64 var interruptedStates = new[] { FileOverlayStatus.Unversioned, FileOverlayStatus.Modified };
66 var pendingEntries = (from state in FileState.Queryable
67 where interruptedStates.Contains(state.OverlayStatus) &&
68 !state.FilePath.StartsWith(FragmentsPath) &&
69 !state.FilePath.EndsWith(".ignore")
70 select state).ToList();
71 var staleEntries = from state in pendingEntries
72 where !File.Exists(state.FilePath)
74 var staleKeys = staleEntries.Select(state=>state.Id);
75 FileState.DeleteAll(staleKeys);
77 var validEntries = from state in pendingEntries.Except(staleEntries)
78 where File.Exists(state.FilePath)
79 select new WorkflowState
81 Path = state.FilePath.ToLower(),
82 FileName = Path.GetFileName(state.FilePath).ToLower(),
83 Hash = state.Checksum,
84 Status = state.OverlayStatus == FileOverlayStatus.Unversioned ?
87 TriggeringChange = state.OverlayStatus == FileOverlayStatus.Unversioned ?
88 WatcherChangeTypes.Created :
89 WatcherChangeTypes.Changed
92 _agent.AddFromEnumerable(validEntries);
97 private void Process(WorkflowState state)
101 string path = state.Path.ToLower();
102 string fileName = Path.GetFileName(path);
104 //Bypass deleted files, unless the status is Deleted
105 if (!(File.Exists(path) || state.Status != FileStatus.Deleted))
108 this.StatusKeeper.RemoveFileOverlayStatus(path);
112 switch (state.Status)
114 case FileStatus.Created:
115 case FileStatus.Modified:
116 var info = new FileInfo(path);
117 NetworkAgent.Post(new CloudAction(CloudActionType.UploadUnconditional, info, ObjectInfo.Empty));
119 case FileStatus.Deleted:
120 NetworkAgent.Post(new CloudAction(CloudActionType.DeleteCloud, null, new ObjectInfo {Name=fileName}));
122 case FileStatus.Renamed:
123 NetworkAgent.Post(new CloudAction(CloudActionType.RenameCloud, state.OldFileName,state.OldPath,state.FileName,state.Path));
132 public void Post(WorkflowState workflowState)
134 _agent.Post(workflowState);