Modifications to enable Sync Pausing for all operations
[pithos-ms-client] / trunk / Pithos.Core / Agents / WorkflowAgent.cs
1 #region
2 /* -----------------------------------------------------------------------
3  * <copyright file="WorkflowAgent.cs" company="GRNet">
4  * 
5  * Copyright 2011-2012 GRNET S.A. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or
8  * without modification, are permitted provided that the following
9  * conditions are met:
10  *
11  *   1. Redistributions of source code must retain the above
12  *      copyright notice, this list of conditions and the following
13  *      disclaimer.
14  *
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.
19  *
20  *
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.
33  *
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.
38  * </copyright>
39  * -----------------------------------------------------------------------
40  */
41 #endregion
42 using System;
43 using System.Collections.Generic;
44 using System.ComponentModel.Composition;
45 using System.Diagnostics;
46 using System.Diagnostics.Contracts;
47 using System.IO;
48 using System.Linq;
49 using System.Reflection;
50 using System.Text;
51 using System.Threading.Tasks;
52 using Castle.ActiveRecord;
53 using Pithos.Interfaces;
54 using Pithos.Network;
55 using log4net;
56
57 namespace Pithos.Core.Agents
58 {
59     [Export]
60     public class WorkflowAgent
61     {
62         private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
63
64         readonly Agent<WorkflowState> _agent;
65                 
66         public IStatusNotification StatusNotification { get; set; }
67         [System.ComponentModel.Composition.Import]
68         public IStatusKeeper StatusKeeper { get; set; }
69
70         [System.ComponentModel.Composition.Import]
71         public NetworkAgent NetworkAgent { get; set; }
72
73         [System.ComponentModel.Composition.Import]
74         public IPithosSettings Settings { get; set; }
75
76         [System.ComponentModel.Composition.Import]
77         public Selectives Selectives { get; set; }
78
79         public WorkflowAgent()
80         {
81             _agent = Agent<WorkflowState>.Start(inbox =>
82             {
83                 Action loop = null;
84                 loop = () =>
85                 {
86                     var message = inbox.Receive();
87                     var process = message.Then(Process, inbox.CancellationToken);                        
88                     inbox.LoopAsync(process,loop,ex=>
89                             Log.ErrorFormat("[ERROR] Synch for {0}:\r{1}", message.Result.FileName, ex));
90                 };
91                 loop();
92             });
93         }
94
95         private Task<object> Process(WorkflowState state)
96         {
97             var accountInfo = state.AccountInfo;
98             using (log4net.ThreadContext.Stacks["Workflow"].Push("Process"))
99             {
100                 try
101                 {
102
103                     if (Log.IsDebugEnabled)
104                         Log.DebugFormat("State {0} {1} {2}", state.FileName, state.Status, state.TriggeringChange);
105
106                     if (state.Skip)
107                     {
108                         if (Log.IsDebugEnabled) Log.DebugFormat("Skipping {0}", state.FileName);
109
110                         return CompletedTask<object>.Default;
111                     }                    
112
113                     var info = Directory.Exists(state.Path) ? (FileSystemInfo)new DirectoryInfo(state.Path) : new FileInfo(state.Path);
114
115                     //Bypass deleted files, unless the status is Deleted
116                     if (!info.Exists && state.Status != FileStatus.Deleted)
117                     {
118                         state.Skip = true;
119                         this.StatusKeeper.ClearFileStatus(state.Path);
120
121                         if (Log.IsDebugEnabled) Log.DebugFormat("Skipped missing {0}", state.FileName);
122
123                         return CompletedTask<object>.Default;
124                     }
125
126                     using (new SessionScope(FlushAction.Never))
127                     {
128
129                         var fileState = StatusKeeper.GetStateByFilePath(state.Path);
130
131                         switch (state.Status)
132                         {
133                             case FileStatus.Created:
134                             case FileStatus.Modified:
135                                 NetworkAgent.Post(new CloudUploadAction(accountInfo, info, fileState,
136                                                                         accountInfo.BlockSize,
137                                                                         accountInfo.BlockHash));
138                                 break;
139                             case FileStatus.Deleted:
140                                 DeleteChildObjects(state, fileState);
141                                 NetworkAgent.Post(new CloudDeleteAction(accountInfo, info, fileState));
142                                 break;
143                             case FileStatus.Renamed:
144                                 if (state.OldPath == null)
145                                 {
146                                     //We reach this point only if the app closed before propagating a rename to the server
147                                     Log.WarnFormat("Unfinished rename [{0}]",state.Path);
148                                     StatusKeeper.SetFileState(state.Path,FileStatus.Conflict,FileOverlayStatus.Conflict, "Rename without old path");
149                                     break;
150                                 }
151                                 FileSystemInfo oldInfo = Directory.Exists(state.OldPath)
152                                                              ? (FileSystemInfo) new DirectoryInfo(state.OldPath)
153                                                              : new FileInfo(state.OldPath);
154                                 FileSystemInfo newInfo = Directory.Exists(state.Path)
155                                                              ? (FileSystemInfo) new DirectoryInfo(state.Path)
156                                                              : new FileInfo(state.Path);
157                                 NetworkAgent.Post(new CloudMoveAction(accountInfo, CloudActionType.RenameCloud,
158                                                                       oldInfo,
159                                                                       newInfo));                                
160                                 //TODO: Do I have to move children as well or will Pithos handle this?
161                                //Need to find all children of the OLD filepath
162                                 //MoveChildObjects(state);
163                                 break;
164                         }
165                     }
166
167                     return CompletedTask<object>.Default;
168                 }
169                 catch (Exception ex)
170                 {
171                     Log.Error(ex.ToString());
172                     throw;
173                 }
174             }
175         }
176
177
178         private void DeleteChildObjects(WorkflowState state, FileState fileState)
179         {
180             if (fileState != null)
181             {
182                 var children = StatusKeeper.GetChildren(fileState);
183                 foreach (var child in children)
184                 {
185                     var childInfo = child.IsFolder
186                                         ? (FileSystemInfo) new DirectoryInfo(child.FilePath)
187                                         : new FileInfo(child.FilePath);
188                     NetworkAgent.Post(new CloudDeleteAction(state.AccountInfo, childInfo, child));
189                 }
190             }
191         }
192
193         /*private void MoveChildObjects(WorkflowState state)
194         {
195             var oldFileState = StatusKeeper.GetStateByFilePath(state.OldPath);
196             if (oldFileState != null)
197             {
198                 var children = StatusKeeper.GetChildren(oldFileState);
199                 foreach (var child in children)
200                 {
201                     var newPath = Path.Combine(state.Path, child.FilePath.Substring(state.OldPath.Length+1));
202
203                     var oldMoveInfo = child.IsFolder
204                                           ? (FileSystemInfo) new DirectoryInfo(child.FilePath)
205                                           : new FileInfo(child.FilePath);
206                     var newMoveInfo = child.IsFolder
207                                           ? (FileSystemInfo) new DirectoryInfo(newPath)
208                                           : new FileInfo(newPath);
209                     //The new file path will be created by trimming the old root path
210                     //and substituting the new root path
211
212                     NetworkAgent.Post(new CloudMoveAction(state.AccountInfo, CloudActionType.RenameCloud,
213                                                           oldMoveInfo, newMoveInfo));
214                 }
215             }
216         }*/
217
218
219         //Starts interrupted files for a specific account
220         public void RestartInterruptedFiles(AccountInfo accountInfo)
221         {
222             
223             StatusKeeper.CleanupOrphanStates();
224             using (log4net.ThreadContext.Stacks["Operation"].Push("RestartInterrupted"))
225             {
226                 if (Log.IsDebugEnabled)
227                     Log.Debug("Starting interrupted files");
228
229                 var cachePath = Path.Combine(accountInfo.AccountPath, FolderConstants.CacheFolder)
230                     .ToLower();
231
232
233                 
234                 
235                 var account = accountInfo;
236                 var pendingEntries = (from state in FileState.Queryable
237                                      where state.FileStatus != FileStatus.Unchanged &&
238                                             state.FileStatus != FileStatus.Forbidden &&
239                                             state.FileStatus != FileStatus.Conflict &&
240                                            !state.FilePath.StartsWith(cachePath) &&
241                                            !state.FilePath.EndsWith(".ignore") &&
242                                            state.FilePath.StartsWith(account.AccountPath)                                            
243                                      select state).ToList();
244                 if (pendingEntries.Count>0)
245                     StatusNotification.NotifyChange("Restart processing interrupted files", TraceLevel.Verbose);
246
247                 var pendingStates = pendingEntries
248                     .Select(state => new WorkflowState(account, state))
249                     .ToList();
250                 
251                                 
252                 if (Log.IsDebugEnabled)
253                     Log.DebugFormat("Found {0} interrupted files", pendingStates.Count);
254
255                 pendingStates.ForEach(Post);
256             }
257         }
258
259
260
261         public void Post(WorkflowState workflowState)
262         {
263             if (Log.IsDebugEnabled)
264                 Log.DebugFormat("Posted {0} {1} {2}", workflowState.Path, workflowState.Status, workflowState.TriggeringChange);
265
266             //Remove invalid state            
267             //For now, ignore paths
268            /* if (Directory.Exists(workflowState.Path))
269                 return;*/
270             //TODO: Need to handle folder renames            
271
272
273             if (!Selectives.IsSelected(workflowState.AccountInfo, workflowState.Path))
274             {
275                 Log.InfoFormat("File skipped, not under a selected folder [{0}] ", workflowState.Path);
276                 return;
277             }
278
279             Debug.Assert(workflowState.Path.StartsWith(workflowState.AccountInfo.AccountPath, StringComparison.InvariantCultureIgnoreCase), "File from wrong account posted");
280
281             _agent.Post(workflowState);
282         }     
283
284     }
285 }