Skip to content

Commit

Permalink
* code cleanup / comments
Browse files Browse the repository at this point in the history
  • Loading branch information
lpetre authored and bkeepers committed Mar 26, 2010
1 parent 3a80488 commit 97862c3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
24 changes: 13 additions & 11 deletions lib/delayed/backend/datamapper.rb
Expand Up @@ -42,17 +42,19 @@ def self.db_time_now
end

def self.find_available(worker_name, limit = 5, max_run_time = Worker.max_run_time)
where = "this.run_at <= new Date(#{db_time_now.to_f * 1000}) && (this.locked_at == null || this.locked_at < new Date(#{(db_time_now - max_run_time).to_f * 1000})) || this.locked_by == #{worker_name.to_json}"
# all(:limit => limit, :failed_at => nil, '$where' => where)

complex = all(:run_at.lte => db_time_now) &
(( all(:locked_at => nil ) | all(:locked_at.lt => db_time_now - max_run_time)) |
all(:locked_by => worker_name));

complex.all( :limit => limit,
:failed_at => nil,
:order => [:priority.asc, :run_at.asc] )
# not yet running
running = all(:run_at.lte => db_time_now)

# lockable
lockable = (
# not locked or past the max time
( all(:locked_at => nil ) | all(:locked_at.lt => db_time_now - max_run_time)) |

# OR locked by our worker
all(:locked_by => worker_name))

# plus some other boring junk
(running & lockable).all( :limit => limit, :failed_at => nil, :order => [:priority.asc, :run_at.asc] )
end

# When a worker is exiting, make sure we don't have any locked jobs.
Expand Down Expand Up @@ -97,4 +99,4 @@ class JobObserver
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/datamapper_job_spec.rb
Expand Up @@ -51,4 +51,4 @@ def tell
end
end
end
end
end

0 comments on commit 97862c3

Please sign in to comment.