From 4d3ba97d1df56ffebb4d7917eec7720d45a6d6ea Mon Sep 17 00:00:00 2001 From: Darren Ehlers Date: Tue, 20 Jan 2015 11:05:03 -0600 Subject: [PATCH 1/3] Added support for scheduling a job to 'runAfter' a specific time Signed-off-by: Darren Ehlers --- EDQueue/EDQueue.h | 2 ++ EDQueue/EDQueue.m | 23 ++++++++++++++++++++++- EDQueue/EDQueueStorageEngine.h | 1 + EDQueue/EDQueueStorageEngine.m | 32 ++++++++++++++++++++++++++------ 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/EDQueue/EDQueue.h b/EDQueue/EDQueue.h index da14d2a..40953b9 100755 --- a/EDQueue/EDQueue.h +++ b/EDQueue/EDQueue.h @@ -34,6 +34,8 @@ extern NSString *const EDQueueDidDrain; @property (nonatomic) NSUInteger retryLimit; - (void)enqueueWithData:(id)data forTask:(NSString *)task; +- (void)enqueueWithData:(id)data forTask:(NSString *)task runAfter:(NSDate*)runAfter; + - (void)start; - (void)stop; - (void)empty; diff --git a/EDQueue/EDQueue.m b/EDQueue/EDQueue.m index a26f870..03e6c64 100755 --- a/EDQueue/EDQueue.m +++ b/EDQueue/EDQueue.m @@ -83,7 +83,28 @@ - (void)enqueueWithData:(id)data forTask:(NSString *)task } /** - * Returns true if a job exists for this task. + * Adds a new scheduled job to the queue. + * + * @param {id} Data + * @param {NSString} Task label + * @param {NSDate} Run After + * + * @return {void} + */ +- (void)enqueueWithData:(id)data forTask:(NSString *)task runAfter:(NSDate*)runAfter +{ + if (!runAfter) + { + [self enqueueWithData:data forTask:task]; + return; + } + + if (data == nil) data = @{}; + [self.engine createJob:data forTask:task runAfter:runAfter]; + [self tick]; +} + +/** * Returns true if a job exists for this task. * * @param {NSString} Task label * diff --git a/EDQueue/EDQueueStorageEngine.h b/EDQueue/EDQueueStorageEngine.h index aec98f4..72142fe 100755 --- a/EDQueue/EDQueueStorageEngine.h +++ b/EDQueue/EDQueueStorageEngine.h @@ -14,6 +14,7 @@ @property (retain) FMDatabaseQueue *queue; - (void)createJob:(id)data forTask:(id)task; +- (void)createJob:(id)data forTask:(id)task runAfter:(NSDate*)runAfter; - (BOOL)jobExistsForTask:(id)task; - (void)incrementAttemptForJob:(NSNumber *)jid; - (void)removeJob:(NSNumber *)jid; diff --git a/EDQueue/EDQueueStorageEngine.m b/EDQueue/EDQueueStorageEngine.m index 9d2e907..5ca1193 100755 --- a/EDQueue/EDQueueStorageEngine.m +++ b/EDQueue/EDQueueStorageEngine.m @@ -24,12 +24,12 @@ - (id)init // Database path NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); NSString *documentsDirectory = [paths objectAtIndex:0]; - NSString *path = [documentsDirectory stringByAppendingPathComponent:@"edqueue_0.5.0d.db"]; + NSString *path = [documentsDirectory stringByAppendingPathComponent:@"edqueueDME001_0.5.0d.db"]; // Allocate the queue _queue = [[FMDatabaseQueue alloc] initWithPath:path]; [self.queue inDatabase:^(FMDatabase *db) { - [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)"]; + [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)"]; [self _databaseHadError:[db hadError] fromDatabase:db]; }]; } @@ -62,6 +62,25 @@ - (void)createJob:(id)data forTask:(id)task }]; } +/** + * Creates a new scheduled job within the datastore. + * + * @param {NSString} Data (JSON string) + * @param {NSString} Task name + * @param {NSDate} Run After + * + * @return {void} + */ +- (void)createJob:(id)data forTask:(id)task runAfter:(NSDate*)runAfter +{ + NSString *dataString = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:data options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding]; + + [self.queue inDatabase:^(FMDatabase *db) { + [db executeUpdate:@"INSERT INTO queue (task, data, runAfter) VALUES (?, ?, ?)", task, dataString, runAfter]; + [self _databaseHadError:[db hadError] fromDatabase:db]; + }]; +} + /** * Tells if a job exists for the specified task name. * @@ -140,7 +159,7 @@ - (NSUInteger)fetchJobCount __block NSUInteger count = 0; [self.queue inDatabase:^(FMDatabase *db) { - FMResultSet *rs = [db executeQuery:@"SELECT count(id) AS count FROM queue"]; + FMResultSet *rs = [db executeQuery:@"SELECT count(id) AS count FROM queue WHERE runafter <= strftime('%s','now')"]; [self _databaseHadError:[db hadError] fromDatabase:db]; while ([rs next]) { @@ -163,7 +182,7 @@ - (NSDictionary *)fetchJob __block id job; [self.queue inDatabase:^(FMDatabase *db) { - FMResultSet *rs = [db executeQuery:@"SELECT * FROM queue ORDER BY id ASC LIMIT 1"]; + FMResultSet *rs = [db executeQuery:@"SELECT * FROM queue WHERE runafter <= strftime('%s','now') ORDER BY id ASC LIMIT 1"]; [self _databaseHadError:[db hadError] fromDatabase:db]; while ([rs next]) { @@ -188,7 +207,7 @@ - (NSDictionary *)fetchJobForTask:(id)task __block id job; [self.queue inDatabase:^(FMDatabase *db) { - FMResultSet *rs = [db executeQuery:@"SELECT * FROM queue WHERE task = ? ORDER BY id ASC LIMIT 1", task]; + FMResultSet *rs = [db executeQuery:@"SELECT * FROM queue WHERE task = ? AND runafter <= strftime('%s','now')ORDER BY id ASC LIMIT 1", task]; [self _databaseHadError:[db hadError] fromDatabase:db]; while ([rs next]) { @@ -210,7 +229,8 @@ - (NSDictionary *)_jobFromResultSet:(FMResultSet *)rs @"task": [rs stringForColumn:@"task"], @"data": [NSJSONSerialization JSONObjectWithData:[[rs stringForColumn:@"data"] dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil], @"attempts": [NSNumber numberWithInt:[rs intForColumn:@"attempts"]], - @"stamp": [rs stringForColumn:@"stamp"] + @"stamp": [rs stringForColumn:@"stamp"], + @"runafter": [rs dateForColumn:@"runafter"] }; return job; } From 1b3bdf1f23e9c97bc204e0c59c3ff4fd1694770b Mon Sep 17 00:00:00 2001 From: Darren Ehlers Date: Tue, 20 Jan 2015 11:16:13 -0600 Subject: [PATCH 2/3] Updated database name revisition Signed-off-by: Darren Ehlers --- EDQueue/EDQueueStorageEngine.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/EDQueue/EDQueueStorageEngine.m b/EDQueue/EDQueueStorageEngine.m index 5ca1193..41e0194 100755 --- a/EDQueue/EDQueueStorageEngine.m +++ b/EDQueue/EDQueueStorageEngine.m @@ -24,7 +24,7 @@ - (id)init // Database path NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); NSString *documentsDirectory = [paths objectAtIndex:0]; - NSString *path = [documentsDirectory stringByAppendingPathComponent:@"edqueueDME001_0.5.0d.db"]; + NSString *path = [documentsDirectory stringByAppendingPathComponent:@"edqueue_0.5.0e.db"]; // Allocate the queue _queue = [[FMDatabaseQueue alloc] initWithPath:path]; From 70e14069c57f099c50c626e499e422f92a144159 Mon Sep 17 00:00:00 2001 From: Darren Ehlers Date: Tue, 20 Jan 2015 11:26:07 -0600 Subject: [PATCH 3/3] Updated podspec version --- EDQueue.podspec | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/EDQueue.podspec b/EDQueue.podspec index 445534b..48794b9 100644 --- a/EDQueue.podspec +++ b/EDQueue.podspec @@ -1,14 +1,14 @@ Pod::Spec.new do |s| s.name = 'EDQueue' - s.version = '0.7.0' + s.version = '0.7.1' s.license = 'MIT' s.summary = 'A persistent background job queue for iOS.' s.homepage = 'https://github.com/thisandagain/queue' s.authors = {'Andrew Sliwinski' => 'andrewsliwinski@acm.org', 'Francois Lambert' => 'flambert@mirego.com'} - s.source = { :git => 'https://github.com/thisandagain/queue.git', :tag => 'v0.7.0' } + s.source = { :git => 'https://github.com/gatewaytechnology/queue.git', :tag => 'v0.7.1' } s.platform = :ios, '5.0' s.source_files = 'EDQueue' s.library = 'sqlite3.0' s.requires_arc = true s.dependency 'FMDB', '~> 2.0' -end \ No newline at end of file +end