Skip to content
This repository has been archived by the owner on Apr 11, 2023. It is now read-only.

Commit

Permalink
added #pending_count, #completed_count and #failed_count to Navvy::Job.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Kreeftmeijer committed Jan 31, 2010
1 parent 7ddb93f commit 2bcc915
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 5 deletions.
1 change: 0 additions & 1 deletion Rakefile
Expand Up @@ -15,7 +15,6 @@ begin
gem.add_development_dependency "mongo_mapper", ">= 0.6.10"
gem.add_development_dependency "sequel", ">= 3.8.0"
gem.add_development_dependency "sqlite3-ruby", ">= 1.2.5"
gem.add_development_dependency "rack-test", ">= 0.5.3"
end
Jeweler::GemcutterTasks.new
rescue LoadError
Expand Down
38 changes: 37 additions & 1 deletion lib/navvy/job/active_record.rb
Expand Up @@ -72,7 +72,43 @@ def self.cleanup
) unless keep?
end
end


##
# The amount of pending jobs
#
# @return [Integer] count

def self.pending_count
count(
:conditions => {
:failed_at => nil,
:completed_at => nil
}
)
end

##
# The amount of completed jobs
#
# @return [Integer] count

def self.completed_count
count(
:conditions => '`completed_at` IS NOT NULL'
)
end

##
# The amount of failed jobs
#
# @return [Integer] count

def self.failed_count
count(
:conditions => '`failed_at` IS NOT NULL'
)
end

##
# Mark the job as started. Will set started_at to the current time.
#
Expand Down
34 changes: 34 additions & 0 deletions lib/navvy/job/data_mapper.rb
Expand Up @@ -86,6 +86,40 @@ def self.cleanup
end
end

##
# The amount of pending jobs
#
# @return [Integer] count

def self.pending_count
all(
:failed_at => nil,
:completed_at => nil
).count
end

##
# The amount of completed jobs
#
# @return [Integer] count

def self.completed_count
all(
:completed_at.not => nil
).count
end

##
# The amount of failed jobs
#
# @return [Integer] count

def self.failed_count
all(
:failed_at.not => nil
).count
end

##
# Mark the job as started. Will set started_at to the current time.
#
Expand Down
36 changes: 35 additions & 1 deletion lib/navvy/job/mongo_mapper.rb
Expand Up @@ -85,7 +85,41 @@ def self.cleanup
) unless keep?
end
end

##
# The amount of pending jobs
#
# @return [Integer] count

def self.pending_count
count(
:failed_at => nil,
:completed_at => nil
)
end

##
# The amount of completed jobs
#
# @return [Integer] count

def self.completed_count
count(
:completed_at => {'$ne' => nil}
)
end

##
# The amount of failed jobs
#
# @return [Integer] count

def self.failed_count
count(
:failed_at => {'$ne' => nil}
)
end

##
# Mark the job as started. Will set started_at to the current time.
#
Expand All @@ -97,7 +131,7 @@ def started
:started_at => Time.now
})
end

##
# Mark the job as completed. Will set completed_at to the current time and
# optionally add the return value if provided.
Expand Down
34 changes: 34 additions & 0 deletions lib/navvy/job/sequel.rb
Expand Up @@ -66,6 +66,40 @@ def self.cleanup
end
end

##
# The amount of pending jobs
#
# @return [Integer] count

def self.pending_count
filter(
:failed_at => nil,
:completed_at => nil
).count
end

##
# The amount of completed jobs
#
# @return [Integer] count

def self.completed_count
filter(
'`completed_at` IS NOT NULL'
).count
end

##
# The amount of failed jobs
#
# @return [Integer] count

def self.failed_count
filter(
'`failed_at` IS NOT NULL'
).count
end

##
# Mark the job as started. Will set started_at to the current time.
#
Expand Down
39 changes: 39 additions & 0 deletions spec/job_spec.rb
Expand Up @@ -205,6 +205,45 @@
end
end

describe '.pending_count' do
before(:each) do
delete_all_jobs
3.times do; Navvy::Job.create; end
2.times do; Navvy::Job.create(:completed_at => Time.now); end
Navvy::Job.create(:failed_at => Time.now)
end

it 'should return 3' do
Navvy::Job.pending_count.should == 3
end
end

describe '.completed_count' do
before(:each) do
delete_all_jobs
3.times do; Navvy::Job.create; end
2.times do; Navvy::Job.create(:completed_at => Time.now); end
Navvy::Job.create(:failed_at => Time.now)
end

it 'should return 2' do
Navvy::Job.completed_count.should == 2
end
end

describe '.failed_count' do
before(:each) do
delete_all_jobs
3.times do; Navvy::Job.create; end
2.times do; Navvy::Job.create(:completed_at => Time.now); end
Navvy::Job.create(:failed_at => Time.now)
end

it 'should return 1' do
Navvy::Job.failed_count.should == 1
end
end

describe '#run' do
it 'should pass the arguments' do
delete_all_jobs
Expand Down
2 changes: 0 additions & 2 deletions spec/spec_helper.rb
@@ -1,8 +1,6 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'navvy'
require 'sinatra'
require 'rack/test'
require 'spec'
require 'spec/autorun'

Expand Down

0 comments on commit 2bcc915

Please sign in to comment.