root / trunk / Pithos.Core / Agents / WorkflowAgent.cs @ 6bcdd8e2
History | View | Annotate | Download (11.1 kB)
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 |
|
77 |
public WorkflowAgent() |
78 |
{ |
79 |
_agent = Agent<WorkflowState>.Start(inbox => |
80 |
{ |
81 |
Action loop = null; |
82 |
loop = () => |
83 |
{ |
84 |
var message = inbox.Receive(); |
85 |
var process = message.Then(Process, inbox.CancellationToken); |
86 |
inbox.LoopAsync(process,loop,ex=> |
87 |
Log.ErrorFormat("[ERROR] Synch for {0}:\r{1}", message.Result.FileName, ex)); |
88 |
}; |
89 |
loop(); |
90 |
}); |
91 |
} |
92 |
|
93 |
private Task<object> Process(WorkflowState state) |
94 |
{ |
95 |
var accountInfo = state.AccountInfo; |
96 |
using (log4net.ThreadContext.Stacks["Workflow"].Push("Process")) |
97 |
{ |
98 |
try |
99 |
{ |
100 |
|
101 |
if (Log.IsDebugEnabled) |
102 |
Log.DebugFormat("State {0} {1} {2}", state.FileName, state.Status, state.TriggeringChange); |
103 |
|
104 |
if (state.Skip) |
105 |
{ |
106 |
if (Log.IsDebugEnabled) Log.DebugFormat("Skipping {0}", state.FileName); |
107 |
|
108 |
return CompletedTask<object>.Default; |
109 |
} |
110 |
|
111 |
var info = Directory.Exists(state.Path) ? (FileSystemInfo)new DirectoryInfo(state.Path) : new FileInfo(state.Path); |
112 |
|
113 |
//Bypass deleted files, unless the status is Deleted |
114 |
if (!info.Exists && state.Status != FileStatus.Deleted) |
115 |
{ |
116 |
state.Skip = true; |
117 |
this.StatusKeeper.ClearFileStatus(state.Path); |
118 |
|
119 |
if (Log.IsDebugEnabled) Log.DebugFormat("Skipped missing {0}", state.FileName); |
120 |
|
121 |
return CompletedTask<object>.Default; |
122 |
} |
123 |
|
124 |
using (new SessionScope(FlushAction.Never)) |
125 |
{ |
126 |
|
127 |
var fileState = StatusKeeper.GetStateByFilePath(state.Path); |
128 |
|
129 |
switch (state.Status) |
130 |
{ |
131 |
case FileStatus.Created: |
132 |
case FileStatus.Modified: |
133 |
NetworkAgent.Post(new CloudUploadAction(accountInfo, info, fileState, |
134 |
accountInfo.BlockSize, |
135 |
accountInfo.BlockHash)); |
136 |
break; |
137 |
case FileStatus.Deleted: |
138 |
DeleteChildObjects(state, fileState); |
139 |
NetworkAgent.Post(new CloudDeleteAction(accountInfo, info, fileState)); |
140 |
break; |
141 |
case FileStatus.Renamed: |
142 |
FileSystemInfo oldInfo = Directory.Exists(state.OldPath) |
143 |
? (FileSystemInfo) new DirectoryInfo(state.OldPath) |
144 |
: new FileInfo(state.OldPath); |
145 |
FileSystemInfo newInfo = Directory.Exists(state.Path) |
146 |
? (FileSystemInfo) new DirectoryInfo(state.Path) |
147 |
: new FileInfo(state.Path); |
148 |
NetworkAgent.Post(new CloudMoveAction(accountInfo, CloudActionType.RenameCloud, |
149 |
oldInfo, |
150 |
newInfo)); |
151 |
//TODO: Do I have to move children as well or will Pithos handle this? |
152 |
//Need to find all children of the OLD filepath |
153 |
//MoveChildObjects(state); |
154 |
break; |
155 |
} |
156 |
} |
157 |
|
158 |
return CompletedTask<object>.Default; |
159 |
} |
160 |
catch (Exception ex) |
161 |
{ |
162 |
Log.Error(ex.ToString()); |
163 |
throw; |
164 |
} |
165 |
} |
166 |
} |
167 |
|
168 |
|
169 |
private void DeleteChildObjects(WorkflowState state, FileState fileState) |
170 |
{ |
171 |
if (fileState != null) |
172 |
{ |
173 |
var children = StatusKeeper.GetChildren(fileState); |
174 |
foreach (var child in children) |
175 |
{ |
176 |
var childInfo = child.IsFolder |
177 |
? (FileSystemInfo) new DirectoryInfo(child.FilePath) |
178 |
: new FileInfo(child.FilePath); |
179 |
NetworkAgent.Post(new CloudDeleteAction(state.AccountInfo, childInfo, child)); |
180 |
} |
181 |
} |
182 |
} |
183 |
|
184 |
/*private void MoveChildObjects(WorkflowState state) |
185 |
{ |
186 |
var oldFileState = StatusKeeper.GetStateByFilePath(state.OldPath); |
187 |
if (oldFileState != null) |
188 |
{ |
189 |
var children = StatusKeeper.GetChildren(oldFileState); |
190 |
foreach (var child in children) |
191 |
{ |
192 |
var newPath = Path.Combine(state.Path, child.FilePath.Substring(state.OldPath.Length+1)); |
193 |
|
194 |
var oldMoveInfo = child.IsFolder |
195 |
? (FileSystemInfo) new DirectoryInfo(child.FilePath) |
196 |
: new FileInfo(child.FilePath); |
197 |
var newMoveInfo = child.IsFolder |
198 |
? (FileSystemInfo) new DirectoryInfo(newPath) |
199 |
: new FileInfo(newPath); |
200 |
//The new file path will be created by trimming the old root path |
201 |
//and substituting the new root path |
202 |
|
203 |
NetworkAgent.Post(new CloudMoveAction(state.AccountInfo, CloudActionType.RenameCloud, |
204 |
oldMoveInfo, newMoveInfo)); |
205 |
} |
206 |
} |
207 |
}*/ |
208 |
|
209 |
|
210 |
//Starts interrupted files for a specific account |
211 |
public void RestartInterruptedFiles(AccountInfo accountInfo) |
212 |
{ |
213 |
|
214 |
|
215 |
using (log4net.ThreadContext.Stacks["Operation"].Push("RestartInterrupted")) |
216 |
{ |
217 |
if (Log.IsDebugEnabled) |
218 |
Log.Debug("Starting interrupted files"); |
219 |
|
220 |
var cachePath = Path.Combine(accountInfo.AccountPath, FolderConstants.CacheFolder) |
221 |
.ToLower(); |
222 |
|
223 |
|
224 |
|
225 |
|
226 |
var account = accountInfo; |
227 |
var pendingEntries = (from state in FileState.Queryable |
228 |
where state.FileStatus != FileStatus.Unchanged && |
229 |
!state.FilePath.StartsWith(cachePath) && |
230 |
!state.FilePath.EndsWith(".ignore") && |
231 |
state.FilePath.StartsWith(account.AccountPath) |
232 |
select state).ToList(); |
233 |
if (pendingEntries.Count>0) |
234 |
StatusNotification.NotifyChange("Restart processing interrupted files", TraceLevel.Verbose); |
235 |
|
236 |
var pendingStates = pendingEntries |
237 |
.Select(state => new WorkflowState(account, state)) |
238 |
.ToList(); |
239 |
|
240 |
if (Log.IsDebugEnabled) |
241 |
Log.DebugFormat("Found {0} interrupted files", pendingStates.Count); |
242 |
|
243 |
pendingStates.ForEach(Post); |
244 |
} |
245 |
} |
246 |
|
247 |
|
248 |
|
249 |
public void Post(WorkflowState workflowState) |
250 |
{ |
251 |
if (Log.IsDebugEnabled) |
252 |
Log.DebugFormat("Posted {0} {1} {2}", workflowState.Path, workflowState.Status, workflowState.TriggeringChange); |
253 |
|
254 |
//Remove invalid state |
255 |
//For now, ignore paths |
256 |
/* if (Directory.Exists(workflowState.Path)) |
257 |
return;*/ |
258 |
//TODO: Need to handle folder renames |
259 |
|
260 |
Debug.Assert(workflowState.Path.StartsWith(workflowState.AccountInfo.AccountPath, StringComparison.InvariantCultureIgnoreCase), "File from wrong account posted"); |
261 |
|
262 |
_agent.Post(workflowState); |
263 |
} |
264 |
|
265 |
} |
266 |
} |