Skip to content

Commit 9fddbc1

Browse files
committed
Merge pull request #1 from gatewaytechnology/apply-runafter-support
Apply runafter support
2 parents 8f8844b + 1b3bdf1 commit 9fddbc1

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

EDQueue/EDQueue.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ extern NSString *const EDQueueDidDrain;
3434
@property (nonatomic) NSUInteger retryLimit;
3535

3636
- (void)enqueueWithData:(id)data forTask:(NSString *)task;
37+
- (void)enqueueWithData:(id)data forTask:(NSString *)task runAfter:(NSDate*)runAfter;
38+
3739
- (void)start;
3840
- (void)stop;
3941
- (void)empty;

EDQueue/EDQueue.m

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,28 @@ - (void)enqueueWithData:(id)data forTask:(NSString *)task
8383
}
8484

8585
/**
86-
* Returns true if a job exists for this task.
86+
* Adds a new scheduled job to the queue.
87+
*
88+
* @param {id} Data
89+
* @param {NSString} Task label
90+
* @param {NSDate} Run After
91+
*
92+
* @return {void}
93+
*/
94+
- (void)enqueueWithData:(id)data forTask:(NSString *)task runAfter:(NSDate*)runAfter
95+
{
96+
if (!runAfter)
97+
{
98+
[self enqueueWithData:data forTask:task];
99+
return;
100+
}
101+
102+
if (data == nil) data = @{};
103+
[self.engine createJob:data forTask:task runAfter:runAfter];
104+
[self tick];
105+
}
106+
107+
/** * Returns true if a job exists for this task.
87108
*
88109
* @param {NSString} Task label
89110
*

EDQueue/EDQueueStorageEngine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
@property (retain) FMDatabaseQueue *queue;
1515

1616
- (void)createJob:(id)data forTask:(id)task;
17+
- (void)createJob:(id)data forTask:(id)task runAfter:(NSDate*)runAfter;
1718
- (BOOL)jobExistsForTask:(id)task;
1819
- (void)incrementAttemptForJob:(NSNumber *)jid;
1920
- (void)removeJob:(NSNumber *)jid;

EDQueue/EDQueueStorageEngine.m

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ - (id)init
2424
// Database path
2525
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
2626
NSString *documentsDirectory = [paths objectAtIndex:0];
27-
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"edqueue_0.5.0d.db"];
27+
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"edqueue_0.5.0e.db"];
2828

2929
// Allocate the queue
3030
_queue = [[FMDatabaseQueue alloc] initWithPath:path];
3131
[self.queue inDatabase:^(FMDatabase *db) {
32-
[db executeUpdate:@"CREATE TABLE IF NOT EXISTS queue (id INTEGER PRIMARY KEY, task TEXT NOT NULL, data TEXT NOT NULL, attempts INTEGER DEFAULT 0, stamp STRING DEFAULT (strftime('%s','now')) NOT NULL, udef_1 TEXT, udef_2 TEXT)"];
32+
[db executeUpdate:@"CREATE TABLE IF NOT EXISTS queue (id INTEGER PRIMARY KEY, task TEXT NOT NULL, data TEXT NOT NULL, attempts INTEGER DEFAULT 0, stamp STRING DEFAULT (strftime('%s','now')) NOT NULL, runafter TEXT DEFAULT (strftime('%s','now')) NOT NULL, udef_1 TEXT, udef_2 TEXT)"];
3333
[self _databaseHadError:[db hadError] fromDatabase:db];
3434
}];
3535
}
@@ -62,6 +62,25 @@ - (void)createJob:(id)data forTask:(id)task
6262
}];
6363
}
6464

65+
/**
66+
* Creates a new scheduled job within the datastore.
67+
*
68+
* @param {NSString} Data (JSON string)
69+
* @param {NSString} Task name
70+
* @param {NSDate} Run After
71+
*
72+
* @return {void}
73+
*/
74+
- (void)createJob:(id)data forTask:(id)task runAfter:(NSDate*)runAfter
75+
{
76+
NSString *dataString = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:data options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];
77+
78+
[self.queue inDatabase:^(FMDatabase *db) {
79+
[db executeUpdate:@"INSERT INTO queue (task, data, runAfter) VALUES (?, ?, ?)", task, dataString, runAfter];
80+
[self _databaseHadError:[db hadError] fromDatabase:db];
81+
}];
82+
}
83+
6584
/**
6685
* Tells if a job exists for the specified task name.
6786
*
@@ -140,7 +159,7 @@ - (NSUInteger)fetchJobCount
140159
__block NSUInteger count = 0;
141160

142161
[self.queue inDatabase:^(FMDatabase *db) {
143-
FMResultSet *rs = [db executeQuery:@"SELECT count(id) AS count FROM queue"];
162+
FMResultSet *rs = [db executeQuery:@"SELECT count(id) AS count FROM queue WHERE runafter <= strftime('%s','now')"];
144163
[self _databaseHadError:[db hadError] fromDatabase:db];
145164

146165
while ([rs next]) {
@@ -163,7 +182,7 @@ - (NSDictionary *)fetchJob
163182
__block id job;
164183

165184
[self.queue inDatabase:^(FMDatabase *db) {
166-
FMResultSet *rs = [db executeQuery:@"SELECT * FROM queue ORDER BY id ASC LIMIT 1"];
185+
FMResultSet *rs = [db executeQuery:@"SELECT * FROM queue WHERE runafter <= strftime('%s','now') ORDER BY id ASC LIMIT 1"];
167186
[self _databaseHadError:[db hadError] fromDatabase:db];
168187

169188
while ([rs next]) {
@@ -188,7 +207,7 @@ - (NSDictionary *)fetchJobForTask:(id)task
188207
__block id job;
189208

190209
[self.queue inDatabase:^(FMDatabase *db) {
191-
FMResultSet *rs = [db executeQuery:@"SELECT * FROM queue WHERE task = ? ORDER BY id ASC LIMIT 1", task];
210+
FMResultSet *rs = [db executeQuery:@"SELECT * FROM queue WHERE task = ? AND runafter <= strftime('%s','now')ORDER BY id ASC LIMIT 1", task];
192211
[self _databaseHadError:[db hadError] fromDatabase:db];
193212

194213
while ([rs next]) {
@@ -210,7 +229,8 @@ - (NSDictionary *)_jobFromResultSet:(FMResultSet *)rs
210229
@"task": [rs stringForColumn:@"task"],
211230
@"data": [NSJSONSerialization JSONObjectWithData:[[rs stringForColumn:@"data"] dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil],
212231
@"attempts": [NSNumber numberWithInt:[rs intForColumn:@"attempts"]],
213-
@"stamp": [rs stringForColumn:@"stamp"]
232+
@"stamp": [rs stringForColumn:@"stamp"],
233+
@"runafter": [rs dateForColumn:@"runafter"]
214234
};
215235
return job;
216236
}

0 commit comments

Comments
 (0)