Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional 'runAfter' timestamp to jobs. #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions EDQueue.podspec
Original file line number Diff line number Diff line change
@@ -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' => '[email protected]', 'Francois Lambert' => '[email protected]'}
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
end
2 changes: 2 additions & 0 deletions EDQueue/EDQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 22 additions & 1 deletion EDQueue/EDQueue.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
1 change: 1 addition & 0 deletions EDQueue/EDQueueStorageEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
32 changes: 26 additions & 6 deletions EDQueue/EDQueueStorageEngine.m
Original file line number Diff line number Diff line change
Expand Up @@ -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:@"edqueue_0.5.0e.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];
}];
}
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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]) {
Expand All @@ -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]) {
Expand All @@ -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]) {
Expand All @@ -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;
}
Expand Down