New SQLite version
[pithos-ms-client] / trunk / Pithos.Core / Agents / WorkflowAgent.cs
index 77875ed..5ff697e 100644 (file)
@@ -46,6 +46,7 @@ using System.Diagnostics;
 using System.Diagnostics.Contracts;
 using System.IO;
 using System.Linq;
+using System.Reflection;
 using System.Text;
 using System.Threading.Tasks;
 using Castle.ActiveRecord;
@@ -58,6 +59,8 @@ namespace Pithos.Core.Agents
     [Export]
     public class WorkflowAgent
     {
+        private static readonly ILog Log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
+
         readonly Agent<WorkflowState> _agent;
                 
         public IStatusNotification StatusNotification { get; set; }
@@ -67,7 +70,9 @@ namespace Pithos.Core.Agents
         [System.ComponentModel.Composition.Import]
         public NetworkAgent NetworkAgent { get; set; }
 
-        private static readonly ILog Log = LogManager.GetLogger("WorkflowAgent");
+        [System.ComponentModel.Composition.Import]
+        public IPithosSettings Settings { get; set; }
+
 
         public WorkflowAgent()
         {
@@ -134,6 +139,13 @@ namespace Pithos.Core.Agents
                                 NetworkAgent.Post(new CloudDeleteAction(accountInfo, info, fileState));
                                 break;
                             case FileStatus.Renamed:
+                                if (state.OldPath == null)
+                                {
+                                    //We reach this point only if the app closed before propagating a rename to the server
+                                    Log.WarnFormat("Unfinished rename [{0}]",state.Path);
+                                    StatusKeeper.SetFileState(state.Path,FileStatus.Conflict,FileOverlayStatus.Conflict, "Rename without old path");
+                                    break;
+                                }
                                 FileSystemInfo oldInfo = Directory.Exists(state.OldPath)
                                                              ? (FileSystemInfo) new DirectoryInfo(state.OldPath)
                                                              : new FileInfo(state.OldPath);
@@ -145,7 +157,7 @@ namespace Pithos.Core.Agents
                                                                       newInfo));                                
                                 //TODO: Do I have to move children as well or will Pithos handle this?
                                //Need to find all children of the OLD filepath
-                                MoveChildObjects(state);
+                                //MoveChildObjects(state);
                                 break;
                         }
                     }
@@ -160,6 +172,7 @@ namespace Pithos.Core.Agents
             }
         }
 
+
         private void DeleteChildObjects(WorkflowState state, FileState fileState)
         {
             if (fileState != null)
@@ -175,7 +188,7 @@ namespace Pithos.Core.Agents
             }
         }
 
-        private void MoveChildObjects(WorkflowState state)
+        /*private void MoveChildObjects(WorkflowState state)
         {
             var oldFileState = StatusKeeper.GetStateByFilePath(state.OldPath);
             if (oldFileState != null)
@@ -198,16 +211,15 @@ namespace Pithos.Core.Agents
                                                           oldMoveInfo, newMoveInfo));
                 }
             }
-        }
+        }*/
 
 
         //Starts interrupted files for a specific account
         public void RestartInterruptedFiles(AccountInfo accountInfo)
         {
             
-            StatusNotification.NotifyChange("Restart processing interrupted files", TraceLevel.Verbose);
-
-            using (log4net.ThreadContext.Stacks["Workflow"].Push("Restart"))
+            StatusKeeper.CleanupOrphanStates();
+            using (log4net.ThreadContext.Stacks["Operation"].Push("RestartInterrupted"))
             {
                 if (Log.IsDebugEnabled)
                     Log.Debug("Starting interrupted files");
@@ -216,27 +228,28 @@ namespace Pithos.Core.Agents
                     .ToLower();
 
 
-
+                
+                
                 var account = accountInfo;
-                var pendingEntries = from state in FileState.Queryable
+                var pendingEntries = (from state in FileState.Queryable
                                      where state.FileStatus != FileStatus.Unchanged &&
+                                            state.FileStatus != FileStatus.Forbidden &&
+                                            state.FileStatus != FileStatus.Conflict &&
                                            !state.FilePath.StartsWith(cachePath) &&
                                            !state.FilePath.EndsWith(".ignore") &&
-                                           state.FilePath.StartsWith(account.AccountPath)
-                                     select state;
-                var pendingStates = new List<WorkflowState>();
-                foreach (var state in pendingEntries)
-                {
-                        pendingStates.Add(new WorkflowState(account, state));
-                }
+                                           state.FilePath.StartsWith(account.AccountPath)                                            
+                                     select state).ToList();
+                if (pendingEntries.Count>0)
+                    StatusNotification.NotifyChange("Restart processing interrupted files", TraceLevel.Verbose);
+
+                var pendingStates = pendingEntries
+                    .Select(state => new WorkflowState(account, state))
+                    .ToList();
+
                 if (Log.IsDebugEnabled)
                     Log.DebugFormat("Found {0} interrupted files", pendingStates.Count);
 
-
-                foreach (var entry in pendingStates)
-                {
-                       Post(entry);
-                }
+                pendingStates.ForEach(Post);
             }
         }