2 /* -----------------------------------------------------------------------
3 * <copyright file="WorkflowAgent.cs" company="GRNet">
5 * Copyright 2011-2012 GRNET S.A. All rights reserved.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
11 * 1. Redistributions of source code must retain the above
12 * copyright notice, this list of conditions and the following
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
22 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
34 * The views and conclusions contained in the software and
35 * documentation are those of the authors and should not be
36 * interpreted as representing official policies, either expressed
37 * or implied, of GRNET S.A.
39 * -----------------------------------------------------------------------
43 using System.Collections.Generic;
44 using System.ComponentModel.Composition;
45 using System.Diagnostics;
46 using System.Diagnostics.Contracts;
49 using System.Reflection;
51 using System.Threading.Tasks;
52 using Castle.ActiveRecord;
53 using Pithos.Interfaces;
57 namespace Pithos.Core.Agents
61 public class WorkflowAgent
63 private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
65 readonly Agent<WorkflowState> _agent;
67 public IStatusNotification StatusNotification { get; set; }
68 [System.ComponentModel.Composition.Import]
69 public IStatusKeeper StatusKeeper { get; set; }
71 [System.ComponentModel.Composition.Import]
72 public NetworkAgent NetworkAgent { get; set; }
74 [System.ComponentModel.Composition.Import]
75 public IPithosSettings Settings { get; set; }
77 [System.ComponentModel.Composition.Import]
78 public Selectives Selectives { get; set; }
80 public WorkflowAgent()
82 /*_agent = Agent<WorkflowState>.Start(inbox =>
87 var message = inbox.Receive();
88 var process = message.Then(Process, inbox.CancellationToken);
89 inbox.LoopAsync(process,loop,ex=>
90 Log.ErrorFormat("[ERROR] Synch for {0}:\r{1}", message.Result.FileName, ex));
97 private Task<object> Process(WorkflowState state)
99 var accountInfo = state.AccountInfo;
100 using (log4net.ThreadContext.Stacks["Workflow"].Push("Process"))
105 if (Log.IsDebugEnabled)
106 Log.DebugFormat("State {0} {1} {2}", state.FileName, state.Status, state.TriggeringChange);
110 if (Log.IsDebugEnabled) Log.DebugFormat("Skipping {0}", state.FileName);
112 return CompletedTask<object>.Default;
115 var info = FileInfoExtensions.FromPath(state.Path);// Directory.Exists(state.Path) ? (FileSystemInfo)new DirectoryInfo(state.Path) : new FileInfo(state.Path);
117 //Bypass deleted files, unless the status is Deleted
118 if (!info.Exists && state.Status != FileStatus.Deleted)
121 this.StatusKeeper.ClearFileStatus(state.Path);
123 if (Log.IsDebugEnabled) Log.DebugFormat("Skipped missing {0}", state.FileName);
125 return CompletedTask<object>.Default;
128 using (new SessionScope(FlushAction.Never))
131 var fileState = StatusKeeper.GetStateByFilePath(state.Path);
133 switch (state.Status)
135 case FileStatus.Created:
136 case FileStatus.Modified:
137 NetworkAgent.Post(new CloudUploadAction(accountInfo, info, fileState,
138 accountInfo.BlockSize,
139 accountInfo.BlockHash,state,state.IsCreation));
141 case FileStatus.Deleted:
142 DeleteChildObjects(state, fileState);
143 NetworkAgent.Post(new CloudDeleteAction(accountInfo, info, fileState,state));
145 case FileStatus.Renamed:
146 if (state.OldPath == null)
148 //We reach this point only if the app closed before propagating a rename to the server
149 Log.WarnFormat("Unfinished rename [{0}]",state.Path);
150 StatusKeeper.SetFileState(state.Path,FileStatus.Conflict,FileOverlayStatus.Conflict, "Rename without old path");
153 FileSystemInfo oldInfo = Directory.Exists(state.OldPath)
154 ? (FileSystemInfo) new DirectoryInfo(state.OldPath)
155 : new FileInfo(state.OldPath);
156 FileSystemInfo newInfo = Directory.Exists(state.Path)
157 ? (FileSystemInfo) new DirectoryInfo(state.Path)
158 : new FileInfo(state.Path);
159 NetworkAgent.Post(new CloudMoveAction(accountInfo, CloudActionType.RenameCloud,
162 //TODO: Do I have to move children as well or will Pithos handle this?
163 //Need to find all children of the OLD filepath
164 //MoveChildObjects(state);
169 return CompletedTask<object>.Default;
173 Log.Error(ex.ToString());
182 private void DeleteChildObjects(WorkflowState state, FileState fileState)
184 if (fileState != null)
186 var children = StatusKeeper.GetChildren(fileState);
187 foreach (var child in children)
189 var childInfo = child.IsFolder
190 ? (FileSystemInfo) new DirectoryInfo(child.FilePath)
191 : new FileInfo(child.FilePath);
192 NetworkAgent.Post(new CloudDeleteAction(state.AccountInfo, childInfo, child,state));
198 /*private void MoveChildObjects(WorkflowState state)
200 var oldFileState = StatusKeeper.GetStateByFilePath(state.OldPath);
201 if (oldFileState != null)
203 var children = StatusKeeper.GetChildren(oldFileState);
204 foreach (var child in children)
206 var newPath = Path.Combine(state.Path, child.FilePath.Substring(state.OldPath.Length+1));
208 var oldMoveInfo = child.IsFolder
209 ? (FileSystemInfo) new DirectoryInfo(child.FilePath)
210 : new FileInfo(child.FilePath);
211 var newMoveInfo = child.IsFolder
212 ? (FileSystemInfo) new DirectoryInfo(newPath)
213 : new FileInfo(newPath);
214 //The new file path will be created by trimming the old root path
215 //and substituting the new root path
217 NetworkAgent.Post(new CloudMoveAction(state.AccountInfo, CloudActionType.RenameCloud,
218 oldMoveInfo, newMoveInfo));
224 //Starts interrupted files for a specific account
226 public void RestartInterruptedFiles(AccountInfo accountInfo)
229 StatusKeeper.CleanupOrphanStates();
230 using (log4net.ThreadContext.Stacks["Operation"].Push("RestartInterrupted"))
232 if (Log.IsDebugEnabled)
233 Log.Debug("Starting interrupted files");
235 var cachePath = Path.Combine(accountInfo.AccountPath, FolderConstants.CacheFolder)
241 var account = accountInfo;
242 var pendingEntries = (from state in FileState.Queryable
243 where state.FileStatus != FileStatus.Unchanged &&
244 state.FileStatus != FileStatus.Forbidden &&
245 state.FileStatus != FileStatus.Conflict &&
246 !state.FilePath.StartsWith(cachePath) &&
247 !state.FilePath.EndsWith(".ignore") &&
248 state.FilePath.StartsWith(account.AccountPath)
249 select state).ToList();
250 if (pendingEntries.Count>0)
251 StatusNotification.NotifyChange("Restart processing interrupted files", TraceLevel.Verbose);
253 var pendingStates = pendingEntries
254 .Select(state => new WorkflowState(account, state))
258 if (Log.IsDebugEnabled)
259 Log.DebugFormat("Found {0} interrupted files", pendingStates.Count);
261 pendingStates.ForEach(Post);
269 public void Post(WorkflowState workflowState)
271 if (Log.IsDebugEnabled)
272 Log.DebugFormat("Posted {0} {1} {2}", workflowState.Path, workflowState.Status, workflowState.TriggeringChange);
274 //Remove invalid state
275 //For now, ignore paths
276 /* if (Directory.Exists(workflowState.Path))
278 //TODO: Need to handle folder renames
281 if (!Selectives.IsSelected(workflowState.AccountInfo, workflowState.Path))
283 if (!workflowState.IsCreation || File.Exists(workflowState.Path))
285 Log.InfoFormat("File skipped, not under a selected folder [{0}] ", workflowState.Path);
290 Debug.Assert(workflowState.Path.StartsWith(workflowState.AccountInfo.AccountPath, StringComparison.InvariantCultureIgnoreCase), "File from wrong account posted");
292 _agent.Post(workflowState);