@@ -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