navvy 0.0.0 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +9 -1
- data/Rakefile +21 -5
- data/VERSION +1 -1
- data/generators/navvy/navvy_generator.rb +15 -0
- data/generators/navvy/templates/migration.rb +20 -0
- data/lib/generators/navvy_generator.rb +20 -0
- data/lib/navvy.rb +2 -2
- data/lib/navvy/job/active_record.rb +212 -0
- data/lib/navvy/job/mongo_mapper.rb +146 -27
- data/lib/navvy/job/sequel.rb +200 -0
- data/lib/navvy/log.rb +60 -0
- data/lib/navvy/tasks.rb +13 -0
- data/lib/navvy/worker.rb +21 -6
- data/navvy.gemspec +26 -8
- data/spec/job/active_record_spec.rb +325 -0
- data/spec/job/mongo_mapper_spec.rb +232 -18
- data/spec/job/sequel_spec.rb +324 -0
- data/spec/log_spec.rb +71 -0
- data/spec/setup/active_record.rb +20 -0
- data/spec/setup/sequel.rb +21 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/worker_spec.rb +5 -5
- metadata +39 -14
- data/lib/job/mongo_mapper.rb +0 -74
- data/lib/navvy/job.rb +0 -16
data/spec/log_spec.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
class LoggerNotFound < StandardError; end
|
4
|
+
|
5
|
+
describe Navvy::Log do
|
6
|
+
describe '.info' do
|
7
|
+
describe 'when using the rails default logger' do
|
8
|
+
before do
|
9
|
+
Navvy::Log.logger = :rails
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should raise an error when the logger can not be found' do
|
13
|
+
lambda { Navvy::Log.info('123') }.should raise_error
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should pass the log to RAILS_DEFAULT_LOGGER' do
|
17
|
+
class RailsLogger
|
18
|
+
def self.info(text);end
|
19
|
+
end
|
20
|
+
|
21
|
+
RAILS_DEFAULT_LOGGER = RailsLogger
|
22
|
+
|
23
|
+
RAILS_DEFAULT_LOGGER.should_receive(:info).with('123')
|
24
|
+
Navvy::Log.info('123')
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe 'when using justlogging' do
|
29
|
+
before do
|
30
|
+
Navvy::Log.logger = :justlogging
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'should raise an error when the logger can not be found' do
|
34
|
+
lambda { Navvy::Log.info('123') }.should raise_error
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'should pass the log to justlogging' do
|
38
|
+
class Justlogging
|
39
|
+
def self.log(text);end
|
40
|
+
end
|
41
|
+
|
42
|
+
Justlogging.should_receive(:log).with('123')
|
43
|
+
Navvy::Log.info('123')
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe 'when using both the rails default logger and justlogging' do
|
48
|
+
before do
|
49
|
+
Navvy::Log.logger = [:rails, :justlogging]
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'should pass the log to justlogging' do
|
53
|
+
RAILS_DEFAULT_LOGGER.should_receive(:info).with('123')
|
54
|
+
Justlogging.should_receive(:log).with('123')
|
55
|
+
Navvy::Log.info('123')
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe 'when not using any logger' do
|
60
|
+
before do
|
61
|
+
Navvy::Log.logger = nil
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'should not log' do
|
65
|
+
RAILS_DEFAULT_LOGGER.should_not_receive(:info)
|
66
|
+
Justlogging.should_not_receive(:log)
|
67
|
+
Navvy::Log.info('123')
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../lib/navvy/job/active_record')
|
2
|
+
require 'rubygems'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => '/tmp/navvy_test.sqlite')
|
6
|
+
|
7
|
+
ActiveRecord::Schema.define do
|
8
|
+
create_table :jobs, :force => true do |table|
|
9
|
+
table.string :object
|
10
|
+
table.string :method_name
|
11
|
+
table.text :arguments
|
12
|
+
table.string :return
|
13
|
+
table.string :exception
|
14
|
+
table.datetime :created_at
|
15
|
+
table.datetime :run_at
|
16
|
+
table.datetime :started_at
|
17
|
+
table.datetime :completed_at
|
18
|
+
table.datetime :failed_at
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'sequel'
|
3
|
+
require 'yaml'
|
4
|
+
|
5
|
+
Sequel.sqlite('/tmp/navvy_test_s.sqlite')
|
6
|
+
|
7
|
+
Sequel::DATABASES[0].create_table!(:jobs) do
|
8
|
+
primary_key :id, :type=>Integer
|
9
|
+
String :object
|
10
|
+
String :method_name
|
11
|
+
String :arguments, :text => true
|
12
|
+
String :return
|
13
|
+
String :exception
|
14
|
+
DateTime :created_at
|
15
|
+
DateTime :run_at
|
16
|
+
DateTime :started_at
|
17
|
+
DateTime :completed_at
|
18
|
+
DateTime :failed_at
|
19
|
+
end
|
20
|
+
|
21
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../lib/navvy/job/sequel')
|
data/spec/spec_helper.rb
CHANGED
data/spec/worker_spec.rb
CHANGED
@@ -8,15 +8,15 @@ describe Navvy::Worker do
|
|
8
8
|
Navvy::Job.enqueue(Cow, :speak),
|
9
9
|
Navvy::Job.enqueue(Cow, :speak)
|
10
10
|
]
|
11
|
-
|
11
|
+
|
12
12
|
Navvy::Job.stub!(:next).and_return(@jobs)
|
13
|
-
end
|
14
|
-
|
13
|
+
end
|
14
|
+
|
15
15
|
it 'should fetch jobs' do
|
16
16
|
Navvy::Job.should_receive(:next).and_return(@jobs)
|
17
17
|
Navvy::Worker.fetch_and_run_jobs
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
it 'should run three jobs' do
|
21
21
|
@jobs.each do |job|
|
22
22
|
job.should_receive(:run)
|
@@ -24,4 +24,4 @@ describe Navvy::Worker do
|
|
24
24
|
Navvy::Worker.fetch_and_run_jobs
|
25
25
|
end
|
26
26
|
end
|
27
|
-
end
|
27
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: navvy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Kreeftmeijer
|
@@ -9,19 +9,9 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-01-
|
12
|
+
date: 2010-01-21 00:00:00 +01:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
|
-
- !ruby/object:Gem::Dependency
|
16
|
-
name: mongo_mapper
|
17
|
-
type: :runtime
|
18
|
-
version_requirement:
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">="
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 0.6.10
|
24
|
-
version:
|
25
15
|
- !ruby/object:Gem::Dependency
|
26
16
|
name: rspec
|
27
17
|
type: :development
|
@@ -62,6 +52,16 @@ dependencies:
|
|
62
52
|
- !ruby/object:Gem::Version
|
63
53
|
version: 1.0.6
|
64
54
|
version:
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mongo_mapper
|
57
|
+
type: :development
|
58
|
+
version_requirement:
|
59
|
+
version_requirements: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: 0.6.10
|
64
|
+
version:
|
65
65
|
- !ruby/object:Gem::Dependency
|
66
66
|
name: machinist_mongomapper
|
67
67
|
type: :development
|
@@ -72,6 +72,16 @@ dependencies:
|
|
72
72
|
- !ruby/object:Gem::Version
|
73
73
|
version: 0.9.7
|
74
74
|
version:
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: sequel
|
77
|
+
type: :development
|
78
|
+
version_requirement:
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: 3.8.0
|
84
|
+
version:
|
75
85
|
description: Simple background job processor inspired by delayed_job, but aiming for database agnosticism.
|
76
86
|
email: jeff@kreeftmeijer.nl
|
77
87
|
executables: []
|
@@ -88,14 +98,24 @@ files:
|
|
88
98
|
- README.textile
|
89
99
|
- Rakefile
|
90
100
|
- VERSION
|
91
|
-
-
|
101
|
+
- generators/navvy/navvy_generator.rb
|
102
|
+
- generators/navvy/templates/migration.rb
|
103
|
+
- lib/generators/navvy_generator.rb
|
92
104
|
- lib/navvy.rb
|
93
|
-
- lib/navvy/job.rb
|
105
|
+
- lib/navvy/job/active_record.rb
|
94
106
|
- lib/navvy/job/mongo_mapper.rb
|
107
|
+
- lib/navvy/job/sequel.rb
|
108
|
+
- lib/navvy/log.rb
|
109
|
+
- lib/navvy/tasks.rb
|
95
110
|
- lib/navvy/worker.rb
|
96
111
|
- navvy.gemspec
|
112
|
+
- spec/job/active_record_spec.rb
|
97
113
|
- spec/job/mongo_mapper_spec.rb
|
114
|
+
- spec/job/sequel_spec.rb
|
115
|
+
- spec/log_spec.rb
|
116
|
+
- spec/setup/active_record.rb
|
98
117
|
- spec/setup/mongo_mapper.rb
|
118
|
+
- spec/setup/sequel.rb
|
99
119
|
- spec/spec_helper.rb
|
100
120
|
- spec/worker_spec.rb
|
101
121
|
has_rdoc: true
|
@@ -127,7 +147,12 @@ signing_key:
|
|
127
147
|
specification_version: 3
|
128
148
|
summary: Simple background job processor inspired by delayed_job, but aiming for database agnosticism.
|
129
149
|
test_files:
|
150
|
+
- spec/job/active_record_spec.rb
|
130
151
|
- spec/job/mongo_mapper_spec.rb
|
152
|
+
- spec/job/sequel_spec.rb
|
153
|
+
- spec/log_spec.rb
|
154
|
+
- spec/setup/active_record.rb
|
131
155
|
- spec/setup/mongo_mapper.rb
|
156
|
+
- spec/setup/sequel.rb
|
132
157
|
- spec/spec_helper.rb
|
133
158
|
- spec/worker_spec.rb
|
data/lib/job/mongo_mapper.rb
DELETED
@@ -1,74 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'mongo_mapper'
|
3
|
-
|
4
|
-
module Navvy
|
5
|
-
class Job
|
6
|
-
include MongoMapper::Document
|
7
|
-
|
8
|
-
key :object, String
|
9
|
-
key :method, Symbol
|
10
|
-
key :arguments, Array
|
11
|
-
key :exception, String
|
12
|
-
key :run_at, Time
|
13
|
-
key :failed_at, Time
|
14
|
-
|
15
|
-
##
|
16
|
-
# Add a job to the job queue.
|
17
|
-
#
|
18
|
-
# @param [Object] object the object you want to run a method from
|
19
|
-
# @param [Symbol, String] method the name of the method you want to run
|
20
|
-
# @param [*] arguments optional arguments you want to pass to the method
|
21
|
-
#
|
22
|
-
# @return [true, false]
|
23
|
-
|
24
|
-
def self.enqueue(object, method, *args)
|
25
|
-
create(
|
26
|
-
:object => object.name,
|
27
|
-
:method => method.to_sym,
|
28
|
-
:run_at => Time.now,
|
29
|
-
:arguments => args
|
30
|
-
)
|
31
|
-
end
|
32
|
-
|
33
|
-
##
|
34
|
-
# Find the next available job in the queue. This will not include failed
|
35
|
-
# jobs (where :failed_at is not nil) and jobs that should run in the future
|
36
|
-
# (where :run_at is greater than the current time).
|
37
|
-
#
|
38
|
-
#
|
39
|
-
# @return [Navvy::Job, nil] the next available job or nil if no jobs
|
40
|
-
# were found
|
41
|
-
|
42
|
-
def self.next
|
43
|
-
first(
|
44
|
-
:failed_at => nil,
|
45
|
-
:run_at => {'$lte', Time.now}
|
46
|
-
)
|
47
|
-
end
|
48
|
-
|
49
|
-
##
|
50
|
-
# Run the job. Will delete the Navvy::Job record and return its return
|
51
|
-
# value if it runs successfully. If a job fails, it'll update the
|
52
|
-
# Navvy::Job record to include the exception message it sent back and set
|
53
|
-
# the :failed_at date. Failed jobs don't get deleted.
|
54
|
-
#
|
55
|
-
# @example
|
56
|
-
# job = Navvy::Job.next # finds the next available job in the queue
|
57
|
-
# job.run # runs the job and returns the job's return value
|
58
|
-
#
|
59
|
-
# @return [String] return value of the called method
|
60
|
-
|
61
|
-
def run
|
62
|
-
begin
|
63
|
-
result = object.constantize.send(method)
|
64
|
-
destroy
|
65
|
-
result
|
66
|
-
rescue Exception => exception
|
67
|
-
update_attributes({
|
68
|
-
:exception => exception.message,
|
69
|
-
:failed_at => Time.now
|
70
|
-
})
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|