Repository navigation
Expand file tree
/
Copy pathWQScanTask.m
More file actions
123 lines (122 loc) · 4.6 KB
/
Copy pathWQScanTask.m
File metadata and controls
123 lines (122 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#import "WQScanTask.h"
#import "WQScanJournal.h"
NSString *const WQScanTaskChangedNotification = @"WQScanTaskChanged";
static NSMutableDictionary *gTasks;
static BOOL gTasksPaused;
@implementation WQScanTask {
void (^_cancel)(void);
}
+ (instancetype)taskNamed:(NSString *)name
{
NSAssert(NSThread.isMainThread, @"task state belongs to the main thread");
if (!gTasks) {
gTasks = [NSMutableDictionary new];
[NSNotificationCenter.defaultCenter addObserverForName:@"WQScanJournalWriteFailed" object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification *note) {
NSString *key = note.object;
for (WQScanTask *task in gTasks.allValues) {
if ([key isEqual:[@"task:" stringByAppendingString:task.name]] ||
[key hasPrefix:[NSString stringWithFormat:@"measure:%@:", task.name]] ||
([key hasPrefix:@"inodes:"] && task.running)) {
task->_persistenceFailed = YES;
[task notify];
}
}
}];
}
WQScanTask *task = gTasks[name];
if (task) return task;
task = [WQScanTask new];
task->_name = [name copy];
NSDictionary *saved = WQScanJournalLoad([@"task:" stringByAppendingString:name]);
task->_identifier = [saved[@"identifier"] isKindOfClass:NSString.class] ? saved[@"identifier"] : NSUUID.UUID.UUIDString;
task->_result = [saved[@"result"] isKindOfClass:NSDictionary.class] ? saved[@"result"] : nil;
task->_finishedAt = [saved[@"finishedAt"] isKindOfClass:NSDate.class] ? saved[@"finishedAt"] : nil;
task->_userPaused = [saved[@"userPaused"] isEqual:@YES];
task->_interrupted = [saved[@"unfinished"] isEqual:@YES] && !task->_userPaused;
task->_status = [saved[@"status"] isKindOfClass:NSString.class] ? saved[@"status"] : @"";
task->_paused = gTasksPaused;
task->_fraction = -1;
gTasks[name] = task;
return task;
}
+ (void)setPaused:(BOOL)paused
{
NSAssert(NSThread.isMainThread, @"task state belongs to the main thread");
gTasksPaused = paused;
for (WQScanTask *task in gTasks.allValues) { task->_paused = paused; [task notify]; }
}
- (void)save
{
NSMutableDictionary *saved = [@{@"identifier": _identifier, @"unfinished": @(_running || _interrupted || _userPaused), @"userPaused": @(_userPaused), @"status": _status ?: @""} mutableCopy];
if (_result) saved[@"result"] = _result;
if (_finishedAt) saved[@"finishedAt"] = _finishedAt;
if (!WQScanJournalSave([@"task:" stringByAppendingString:_name], saved)) {
_persistenceFailed = YES;
NSLog(@"[wqfiles] Cannot save scan task %@; in-memory results retained", _name);
}
}
- (void)notify
{
[NSNotificationCenter.defaultCenter postNotificationName:WQScanTaskChangedNotification object:self];
}
- (NSUInteger)beginFresh:(BOOL)fresh cancel:(void (^)(void))cancel
{
NSAssert(NSThread.isMainThread, @"task state belongs to the main thread");
if (_running && !fresh) return 0;
if (_cancel) _cancel();
if (fresh || (!_interrupted && !_userPaused)) _identifier = NSUUID.UUID.UUIDString;
_generation++;
_cancel = [cancel copy];
_running = YES;
_interrupted = NO;
_userPaused = NO;
_status = @"";
_fraction = -1;
[self save];
[self notify];
return _generation;
}
- (void)pause
{
NSAssert(NSThread.isMainThread, @"task state belongs to the main thread");
if (!_running) return;
if (_cancel) _cancel();
_cancel = nil;
_generation++;
_running = NO;
_interrupted = NO;
_userPaused = YES;
_status = @"Scan paused";
_fraction = -1;
[self save];
[self notify];
}
- (void)progress:(NSString *)status generation:(NSUInteger)generation
{
[self progress:status fraction:-1 generation:generation];
}
- (void)progress:(NSString *)status fraction:(float)fraction generation:(NSUInteger)generation
{
NSAssert(NSThread.isMainThread, @"task state belongs to the main thread");
if (generation != _generation || !_running) return;
_status = [status copy];
_fraction = fraction;
[self notify];
}
- (void)finish:(NSDictionary *)result generation:(NSUInteger)generation
{
NSAssert(NSThread.isMainThread, @"task state belongs to the main thread");
if (generation != _generation || !_running) return;
BOOL failed = [result[@"complete"] isEqual:@NO];
if (!failed || !_result) {
_result = [result copy];
_finishedAt = NSDate.date;
}
_running = NO;
_cancel = nil;
_persistenceFailed = NO;
_status = failed && [result[@"note"] isKindOfClass:NSString.class] ? result[@"note"] : @"";
[self save];
[self notify];
}
@end