Revision 025046f1 trunk/Pithos.Core/Agents/NetworkAgent.cs
b/trunk/Pithos.Core/Agents/NetworkAgent.cs | ||
---|---|---|
448 | 448 |
public async Task PollRemoteFiles(DateTime? since = null,bool repeat=true) |
449 | 449 |
{ |
450 | 450 |
|
451 |
_tcs = new TaskCompletionSource<bool>(); |
|
452 |
var wait = TaskEx.Delay(TimeSpan.FromSeconds(Settings.PollingInterval), _agent.CancellationToken); |
|
453 |
var signaledTask=await TaskEx.WhenAny(_tcs.Task,wait); |
|
454 |
//If polling is signalled by SynchNow, ignore the since tag |
|
455 |
if (signaledTask is Task<bool>) |
|
456 |
since = null; |
|
457 | 451 |
|
458 | 452 |
using (log4net.ThreadContext.Stacks["Retrieve Remote"].Push("All accounts")) |
459 | 453 |
{ |
460 |
|
|
454 |
//If this poll fails, we will retry with the same since value |
|
455 |
var nextSince = since; |
|
461 | 456 |
try |
462 | 457 |
{ |
463 | 458 |
//Next time we will check for all changes since the current check minus 1 second |
464 | 459 |
//This is done to ensure there are no discrepancies due to clock differences |
465 |
DateTime nextSince = DateTime.Now.AddSeconds(-1);
|
|
460 |
DateTime current = DateTime.Now.AddSeconds(-1);
|
|
466 | 461 |
|
467 | 462 |
var tasks = from accountInfo in _accounts |
468 | 463 |
select ProcessAccountFiles(accountInfo, since); |
469 | 464 |
|
470 | 465 |
await TaskEx.WhenAll(tasks.ToList()); |
471 |
|
|
466 |
|
|
472 | 467 |
_firstPoll = false; |
468 |
//Reschedule the poll with the current timestamp as a "since" value |
|
473 | 469 |
if (repeat) |
474 |
PollRemoteFiles(nextSince); |
|
470 |
nextSince = current; |
|
471 |
else |
|
472 |
return; |
|
475 | 473 |
} |
476 | 474 |
catch (Exception ex) |
477 | 475 |
{ |
478 | 476 |
Log.ErrorFormat("Error while processing accounts\r\n{0}",ex); |
479 |
//In case of failure retry with the same parameter |
|
480 |
PollRemoteFiles(since); |
|
477 |
//In case of failure retry with the same "since" value |
|
481 | 478 |
} |
482 |
|
|
479 |
|
|
480 |
//Wait for the polling interval to pass or the Manual flat to be toggled |
|
481 |
nextSince = await WaitForScheduledOrManualPoll(nextSince); |
|
482 |
|
|
483 |
PollRemoteFiles(nextSince); |
|
483 | 484 |
|
484 | 485 |
} |
485 | 486 |
} |
486 | 487 |
|
488 |
private async Task<DateTime?> WaitForScheduledOrManualPoll(DateTime? since) |
|
489 |
{ |
|
490 |
_tcs = new TaskCompletionSource<bool>(); |
|
491 |
var wait = TaskEx.Delay(TimeSpan.FromSeconds(Settings.PollingInterval), _agent.CancellationToken); |
|
492 |
var signaledTask = await TaskEx.WhenAny(_tcs.Task, wait); |
|
493 |
//If polling is signalled by SynchNow, ignore the since tag |
|
494 |
if (signaledTask is Task<bool>) |
|
495 |
return null; |
|
496 |
return since; |
|
497 |
} |
|
498 |
|
|
487 | 499 |
public async Task ProcessAccountFiles(AccountInfo accountInfo,DateTime? since=null) |
488 | 500 |
{ |
489 | 501 |
if (accountInfo==null) |
... | ... | |
632 | 644 |
//in two steps |
633 | 645 |
//NOTE: DON'T return files that are already in conflict. The first poll would mark them as |
634 | 646 |
//"In Conflict" but subsequent polls would delete them |
647 |
/* var t=FileState.Find(new Guid("{cd664c9a-5f17-47c9-b27f-3bcbcb0595ff}")); |
|
648 |
|
|
649 |
var d0 = FileState.Queryable |
|
650 |
.Where(state => |
|
651 |
state.FilePath.StartsWith(accountInfo.AccountPath)).ToList(); |
|
652 |
|
|
653 |
var d1 = FileState.Queryable |
|
654 |
.Where(state => state.Modified <= pollTime).ToList(); |
|
655 |
var d2= FileState.Queryable |
|
656 |
.Where(state => state.Modified <= pollTime |
|
657 |
&& |
|
658 |
state.FilePath.StartsWith(accountInfo.AccountPath)).ToList();*/ |
|
659 |
|
|
660 |
var deleteCandidates = FileState.Queryable |
|
661 |
.Where(state => state.Modified <= pollTime |
|
662 |
&& |
|
663 |
state.FilePath.StartsWith(accountInfo.AccountPath) |
|
664 |
&& state.FileStatus != FileStatus.Conflict).ToList(); |
|
665 |
/* |
|
635 | 666 |
var deleteCandidates = (from state in FileState.Queryable |
636 | 667 |
where |
637 | 668 |
state.Modified <= pollTime |
638 | 669 |
&& state.FilePath.StartsWith(accountInfo.AccountPath) |
639 | 670 |
&& state.FileStatus != FileStatus.Conflict |
640 | 671 |
select state).ToList(); |
672 |
*/ |
|
641 | 673 |
|
642 | 674 |
var filesToDelete = (from deleteCandidate in deleteCandidates |
643 | 675 |
let localFile = FileInfoExtensions.FromPath(deleteCandidate.FilePath) |
Also available in: Unified diff