2 using System.Collections.Generic;
3 using System.ComponentModel.Composition;
4 using System.Diagnostics;
5 using System.Diagnostics.Contracts;
9 using System.Threading.Tasks;
10 using Pithos.Interfaces;
14 namespace Pithos.Core.Agents
17 public class WorkflowAgent
19 Agent<WorkflowState> _agent;
21 public IStatusNotification StatusNotification { get; set; }
23 public IStatusKeeper StatusKeeper { get; set; }
26 public NetworkAgent NetworkAgent { get; set; }
28 private static readonly ILog Log = LogManager.GetLogger("WorkflowAgent");
32 _agent = Agent<WorkflowState>.Start(inbox =>
37 var message = inbox.Receive();
38 var process = message.Then(Process, inbox.CancellationToken);
39 inbox.LoopAsync(process,loop,ex=>
40 Log.ErrorFormat("[ERROR] Synch for {0}:\r{1}", message.Result.FileName, ex));
46 private Task<object> Process(WorkflowState state)
48 var accountInfo = state.AccountInfo;
49 using (log4net.ThreadContext.Stacks["Workflow"].Push("Process"))
51 if (Log.IsDebugEnabled) Log.DebugFormat("State {0} {1} {2}", state.FileName,state.Status,state.TriggeringChange);
55 if (Log.IsDebugEnabled) Log.DebugFormat("Skipping {0}",state.FileName);
57 return CompletedTask<object>.Default;
59 string path = state.Path.ToLower();
61 //Bypass deleted files, unless the status is Deleted
62 if (!File.Exists(path) && state.Status != FileStatus.Deleted)
65 this.StatusKeeper.ClearFileStatus(path);
67 if (Log.IsDebugEnabled) Log.DebugFormat("Skipped missing {0}", state.FileName);
69 return CompletedTask<object>.Default;
72 var fileState = FileState.FindByFilePath(path);
73 var info = new FileInfo(path);
78 case FileStatus.Created:
79 case FileStatus.Modified:
80 NetworkAgent.Post(new CloudUploadAction(accountInfo,info, fileState, accountInfo.BlockSize,
81 accountInfo.BlockHash));
83 case FileStatus.Deleted:
84 NetworkAgent.Post(new CloudDeleteAction(accountInfo,info, fileState));
86 case FileStatus.Renamed:
87 NetworkAgent.Post(new CloudMoveAction(accountInfo,CloudActionType.RenameCloud, new FileInfo(state.OldPath),
88 new FileInfo(state.Path)));
92 return CompletedTask<object>.Default;
97 //Starts interrupted files for a specific account
98 public void RestartInterruptedFiles(AccountInfo accountInfo)
101 StatusNotification.NotifyChange("Restart processing interrupted files", TraceLevel.Verbose);
103 using (log4net.ThreadContext.Stacks["Workflow"].Push("Restart"))
105 if (Log.IsDebugEnabled)
106 Log.Debug("Starting interrupted files");
108 var cachePath = Path.Combine(accountInfo.AccountPath, FolderConstants.CacheFolder)
112 var pendingEntries = from state in FileState.Queryable
113 where state.FileStatus != FileStatus.Unchanged &&
114 !state.FilePath.StartsWith(cachePath) &&
115 !state.FilePath.EndsWith(".ignore") &&
116 state.FilePath.StartsWith(accountInfo.AccountPath)
118 if (Log.IsDebugEnabled)
119 Log.DebugFormat("Found {0} interrupted files",pendingEntries.Count());
121 var validEntries = from state in pendingEntries
122 select new WorkflowState
124 AccountInfo=accountInfo,
125 Path = state.FilePath.ToLower(),
126 FileName = Path.GetFileName(state.FilePath).ToLower(),
127 Hash = state.Checksum,
128 Status = state.OverlayStatus == FileOverlayStatus.Unversioned
132 state.OverlayStatus == FileOverlayStatus.Unversioned
133 ? WatcherChangeTypes.Created
134 : WatcherChangeTypes.Changed
136 foreach (var entry in validEntries)
145 public void Post(WorkflowState workflowState)
147 if (Log.IsDebugEnabled)
148 Log.DebugFormat("Posted {0} {1} {2}", workflowState.Path, workflowState.Status, workflowState.TriggeringChange);
149 _agent.Post(workflowState);