navvy 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,16 +1,18 @@
1
1
  h1. Navvy
2
2
 
3
- Navvy is a simple Ruby background job processor inspired by "delayed_job":http://github.com/tobi/delayed_job, but aiming for database agnosticism. Currently Navvy supports ActiveRecord, MongoMapper, Sequel and DataMapper but it's extremely easy to write an adapter for your favorite ORM. Besides plain Ruby (1.8 & 1.9) it completely supports Rails Edge.
3
+ Navvy is a simple Ruby background job processor inspired by "delayed_job":http://github.com/tobi/delayed_job, but aiming for database agnosticism. Currently Navvy supports ActiveRecord, MongoMapper, Sequel, DataMapper and Mongoid but it's extremely easy to write an adapter for your favorite ORM.
4
+
5
+ Navvy doesn't depend on Rails, it's a pure Ruby library. There are generators for Rails 2 and Rails 3, though.
4
6
 
5
7
  ??“Navvy is a shorter form of navigator (UK) or navigational engineer (USA) and is particularly applied to describe the manual labourers working on major civil engineering projects. The term was coined in the late 18th century in Britain when numerous canals were being built, which were also sometimes known as "navigations". Canal navvies typically worked with shovels, pickaxes and barrows.”?? - "Wikipedia":http://en.wikipedia.org/wiki/Navvy
6
8
 
7
9
  h2. Using Navvy
8
10
 
9
- There are some "Installation Guide":http://wiki.github.com/jeffkreeftmeijer/navvy/installation (even for "Rails 3":http://wiki.github.com/jeffkreeftmeijer/navvy/installation-on-rails-edge) and a "Getting Started Guide":http://wiki.github.com/jeffkreeftmeijer/navvy/getting-started to put you on the right track. Any questions? Don't hesitate to "ask":http://github.com/inbox/new/jeffkreeftmeijer.
11
+ Check out the "Installation Guide":http://wiki.github.com/jeffkreeftmeijer/navvy/installation and the "Getting Started Guide":http://wiki.github.com/jeffkreeftmeijer/navvy/getting-started to get up and running. If you have any questions or problems, don't hesitate to "ask":http://github.com/inbox/new/jeffkreeftmeijer.
10
12
 
11
13
  h2. Contributing
12
14
 
13
- Found any issues? Have a great idea? Want to help? Great! Create an issue "issue":http://github.com/jeffkreeftmeijer/navvy/issues for it, or even better; "fork the project":http://github.com/jeffkreeftmeijer/navvy/fork. Pull requests are always welcome. :)
15
+ Found an issue? Have a great idea? Want to help? Great! Create an issue "issue":http://github.com/jeffkreeftmeijer/navvy/issues for it, "ask":http://github.com/inbox/new/jeffkreeftmeijer, or even better; fork the project and fix the problem yourself. Pull requests are always welcome. :)
14
16
 
15
17
  h2. License
16
18
 
@@ -1,14 +1,19 @@
1
1
  class NavvyGenerator < Rails::Generator::Base
2
+ default_options :orm => 'active_record'
3
+
2
4
  def manifest
3
5
  record do |m|
4
- options = {
5
- :migration_file_name => 'create_jobs'
6
- }
7
- m.migration_template 'migration.rb', 'db/migrate', options
8
- m.file 'script', 'script/navvy', :chmod => 0755
6
+ m.migration_template "#{options[:orm]}_migration.rb", 'db/migrate', {:migration_file_name => 'create_jobs'}
9
7
  end
10
8
  end
11
9
 
10
+ def add_options!(opt)
11
+ opt.separator ''
12
+ opt.separator 'Options:'
13
+ opt.on('--active_record', 'Generate a migration file for ActiveRecord. (default)') { options[:orm] = 'active_record' }
14
+ opt.on('--sequel', 'Generate a migration file for Sequel.') { options[:orm] = 'sequel' }
15
+ end
16
+
12
17
  def banner
13
18
  "Usage: #{$0} #{spec.name}"
14
19
  end
@@ -0,0 +1,23 @@
1
+ Sequel.migration do
2
+ up do
3
+ create_table(:jobs) do
4
+ primary_key :id, :type => Integer
5
+ String :object
6
+ String :method_name
7
+ String :arguments, :text => true
8
+ Integer :priority, :default => 0
9
+ String :return
10
+ String :exception
11
+ Integer :parent_id
12
+ DateTime :created_at
13
+ DateTime :run_at
14
+ DateTime :started_at
15
+ DateTime :completed_at
16
+ DateTime :failed_at
17
+ end
18
+ end
19
+
20
+ down do
21
+ drop_table(:jobs)
22
+ end
23
+ end
@@ -2,21 +2,27 @@ require 'rails/generators/migration'
2
2
  class NavvyGenerator < Rails::Generators::Base
3
3
  include Rails::Generators::Migration
4
4
 
5
+ class_option :active_record,
6
+ :desc => 'Generate a migration file for ActiveRecord. (default)',
7
+ :type => 'boolean'
8
+
9
+ class_option :sequel,
10
+ :desc => 'Generate a migration file for Sequel.',
11
+ :type => 'boolean'
12
+
5
13
  def self.source_root
6
14
  File.join(File.dirname(__FILE__), '..', '..', 'generators', 'navvy', 'templates')
7
15
  end
8
16
 
9
17
  def install_navvy
10
18
  migration_template(
11
- 'migration.rb',
19
+ "#{orm}_migration.rb",
12
20
  'db/migrate/create_jobs.rb'
13
21
  )
22
+ end
14
23
 
15
- copy_file(
16
- 'script',
17
- 'script/navvy',
18
- :chmod => 0755
19
- )
24
+ def orm
25
+ options[:sequel] ? 'sequel' : 'active_record'
20
26
  end
21
27
 
22
28
  protected
@@ -1,16 +1,20 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/navvy/worker')
2
- require File.expand_path(File.dirname(__FILE__) + '/navvy/log')
2
+ require File.expand_path(File.dirname(__FILE__) + '/navvy/logger')
3
3
  require File.expand_path(File.dirname(__FILE__) + '/navvy/configuration')
4
4
 
5
5
  module Navvy
6
6
  class << self
7
7
  attr_writer :configuration
8
8
  end
9
-
9
+
10
+ def self.logger
11
+ @logger || Navvy.configuration.logger
12
+ end
13
+
10
14
  def self.configuration
11
15
  @configuration ||= Configuration.new
12
16
  end
13
-
17
+
14
18
  def self.configure
15
19
  yield(self.configuration)
16
20
  end
@@ -1,15 +1,13 @@
1
1
  module Navvy
2
2
  class Configuration
3
- attr_accessor :job_limit, :keep_jobs, :logger, :quiet, :sleep_time,
4
- :max_attempts
5
-
3
+ attr_accessor :job_limit, :keep_jobs, :logger, :sleep_time, :max_attempts
4
+
6
5
  def initialize
7
6
  @job_limit = 100
8
7
  @keep_jobs = false
9
- @logger = nil
10
- @quiet = false
8
+ @logger = Navvy::Logger.new
11
9
  @sleep_time = 5
12
10
  @max_attempts = 25
13
11
  end
14
12
  end
15
- end
13
+ end
@@ -152,14 +152,14 @@ module Navvy
152
152
 
153
153
  alias_method :completed?, :completed_at?
154
154
  alias_method :failed?, :failed_at?
155
-
155
+
156
156
  private
157
-
157
+
158
158
  ##
159
159
  # Turn a constant with potential namespacing into an object
160
160
  #
161
161
  # @return [Class] class
162
-
162
+
163
163
  def constantize(str)
164
164
  names = str.split('::')
165
165
  names.shift if names.empty? || names.first.empty?
@@ -1,4 +1,3 @@
1
- require 'rubygems'
2
1
  require 'active_record'
3
2
 
4
3
  module Navvy
@@ -1,4 +1,3 @@
1
- require 'rubygems'
2
1
  require 'dm-core'
3
2
 
4
3
  module Navvy
@@ -1,4 +1,3 @@
1
- require 'rubygems'
2
1
  require 'mongo_mapper'
3
2
 
4
3
  module Navvy
@@ -0,0 +1,139 @@
1
+ require 'mongoid'
2
+
3
+ module Navvy
4
+ class Job
5
+ include Mongoid::Document
6
+ include Mongoid::Timestamps
7
+
8
+ field :object, :type => String
9
+ field :method_name, :type => String
10
+ field :arguments, :type => String
11
+ field :priority, :type => Integer, :default => 0
12
+ field :return, :type => String
13
+ field :exception, :type => String
14
+ field :parent_id, :type => String
15
+ field :created_at, :type => Time
16
+ field :run_at, :type => Time
17
+ field :started_at, :type => Time
18
+ field :completed_at, :type => Time
19
+ field :failed_at, :type => Time
20
+
21
+ index [[:priority, Mongo::DESCENDING]]
22
+ index [[:created_at, Mongo::ASCENDING]]
23
+
24
+ ##
25
+ # Add a job to the job queue.
26
+ #
27
+ # @param [Object] object the object you want to run a method from
28
+ # @param [Symbol, String] method_name the name of the method you want to
29
+ # run
30
+ # @param [*] arguments optional arguments you want to pass to the method
31
+ #
32
+ # @return [true, false]
33
+
34
+ def self.enqueue(object, method_name, *args)
35
+ options = {}
36
+ if args.last.is_a?(Hash)
37
+ options = args.last.delete(:job_options) || {}
38
+ args.pop if args.last.empty?
39
+ end
40
+
41
+ create(
42
+ :object => object.to_s,
43
+ :method_name => method_name.to_sym,
44
+ :arguments => args.to_yaml,
45
+ :priority => options[:priority] || 0,
46
+ :parent_id => options[:parent_id],
47
+ :run_at => options[:run_at] || Time.now
48
+ )
49
+ end
50
+
51
+ ##
52
+ # Find the next available jobs in the queue. This will not include failed
53
+ # jobs (where :failed_at is not nil) and jobs that should run in the future
54
+ # (where :run_at is greater than the current time).
55
+ #
56
+ # @param [Integer] limit the limit of jobs to be fetched. Defaults to
57
+ # Navvy::Job.limit
58
+ #
59
+ # @return [array, nil] the next available jobs in an array or nil if no
60
+ # jobs were found.
61
+
62
+ def self.next(limit = self.limit)
63
+ where(:failed_at => nil).
64
+ where(:completed_at => nil).
65
+ where(:run_at.lte => Time.now).
66
+ order_by([[:priority, :desc], [:created_at, :asc]]).
67
+ limit(limit).to_a
68
+ end
69
+
70
+ ##
71
+ # Clean up jobs that we don't need to keep anymore. If Navvy::Job.keep is
72
+ # false it'll delete every completed job, if it's a timestamp it'll only
73
+ # delete completed jobs that have passed their keeptime.
74
+ #
75
+ # @return [true, false] delete_all the result of the delete_all call
76
+
77
+ def self.cleanup
78
+ if keep.is_a? Fixnum
79
+ destroy_all(:conditions => { :completed_at.lte => keep.ago })
80
+ else
81
+ destroy_all(:conditions => { :completed_at.ne => nil }) unless keep?
82
+ end
83
+ end
84
+
85
+ ##
86
+ # Mark the job as started. Will set started_at to the current time.
87
+ #
88
+ # @return [true, false] update_attributes the result of the
89
+ # update_attributes call
90
+
91
+ def started
92
+ update_attributes(:started_at => Time.now)
93
+ end
94
+
95
+ ##
96
+ # Mark the job as completed. Will set completed_at to the current time and
97
+ # optionally add the return value if provided.
98
+ #
99
+ # @param [String] return_value the return value you want to store.
100
+ #
101
+ # @return [true, false] update_attributes the result of the
102
+ # update_attributes call
103
+
104
+ def completed(return_value = nil)
105
+ update_attributes(:completed_at => Time.now, :return => return_value)
106
+ end
107
+
108
+ ##
109
+ # Mark the job as failed. Will set failed_at to the current time and
110
+ # optionally add the exception message if provided. Also, it will retry
111
+ # the job unless max_attempts has been reached.
112
+ #
113
+ # @param [String] exception the exception message you want to store.
114
+ #
115
+ # @return [true, false] update_attributes the result of the
116
+ # update_attributes call
117
+
118
+ def failed(message = nil)
119
+ self.retry unless times_failed >= self.class.max_attempts
120
+ update_attributes(:failed_at => Time.now, :exception => message)
121
+ end
122
+
123
+ ##
124
+ # Check how many times the job has failed. Will try to find jobs with a
125
+ # parent_id that's the same as self.id and count them
126
+ #
127
+ # @return [Integer] count the amount of times the job has failed
128
+
129
+ def times_failed
130
+ i = parent_id || id
131
+ self.class.
132
+ where(:failed_at.ne => nil).
133
+ where("this._id == '#{i}' || this.parent_id == '#{i}'").
134
+ count
135
+ end
136
+ end
137
+ end
138
+
139
+ require File.expand_path(File.dirname(__FILE__) + '/../job')
@@ -1,4 +1,3 @@
1
- require 'rubygems'
2
1
  require 'sequel'
3
2
  require 'yaml'
4
3
 
@@ -48,7 +47,7 @@ module Navvy
48
47
  def self.next(limit = self.limit)
49
48
  filter(
50
49
  (:run_at <= Time.now),
51
- {:failed_at => nil,
50
+ {:failed_at => nil,
52
51
  :completed_at => nil}
53
52
  ).order(:priority.desc, :created_at).first(limit)
54
53
  end
@@ -0,0 +1,38 @@
1
+ require 'logger'
2
+
3
+ module Navvy
4
+ class Logger < Logger
5
+ ##
6
+ # Create a new logger. Works like Logger from Ruby's standard library, but
7
+ # defaults to STDOUT instead of failing. You can pass a filename to log to.
8
+ #
9
+ # @param [String] logdev a filename to log to, defaults to STDOUT
10
+ #
11
+ # @example
12
+ # logger = Navvy::Logger.new
13
+ # logger = Navvy::Logger.new('~/file.log')
14
+
15
+ def initialize(logdev = STDOUT)
16
+ super logdev
17
+ end
18
+
19
+ ##
20
+ # Send colored logs to the logger. Will only colorize output sent to
21
+ # STDOUT and will call the regular info method when writing to file.
22
+ #
23
+ # @param [String] message the message you want to log
24
+ # @param [String] color the color code you want to use to color your
25
+ # message
26
+ #
27
+ # @example
28
+ # logger = Navvy::Logger.new
29
+ # logger.colorized_info "I'm green!", 32
30
+
31
+ def colorized_info(message, color)
32
+ unless @logdev.filename
33
+ return info("\e[#{color}m#{message}\e[0m")
34
+ end
35
+ info(message)
36
+ end
37
+ end
38
+ end
@@ -1,11 +1,11 @@
1
1
  task :environment
2
-
2
+
3
3
  namespace :navvy do
4
4
  desc "Clear the Navvy queue."
5
5
  task :clear => :environment do
6
6
  Navvy::Job.delete_all
7
7
  end
8
-
8
+
9
9
  desc "Start a Navvy worker."
10
10
  task :work => :environment do
11
11
  Navvy::Worker.start
@@ -1,6 +1,3 @@
1
- require 'rubygems'
2
- require 'daemons'
3
-
4
1
  module Navvy
5
2
  class Worker
6
3
  class << self
@@ -20,15 +17,15 @@ module Navvy
20
17
  # Start the worker.
21
18
 
22
19
  def self.start
23
- Navvy::Log.info '*** Starting ***'
24
- trap('TERM') { Navvy::Log.info '*** Exiting ***'; $exit = true }
25
- trap('INT') { Navvy::Log.info '*** Exiting ***'; $exit = true }
20
+ Navvy.logger.info '*** Starting ***'
21
+ trap('TERM') { Navvy.logger.info '*** Exiting ***'; $exit = true }
22
+ trap('INT') { Navvy.logger.info '*** Exiting ***'; $exit = true }
26
23
 
27
24
  loop do
28
25
  fetch_and_run_jobs
29
26
 
30
27
  if $exit
31
- Navvy::Log.info '*** Cleaning up ***'
28
+ Navvy.logger.info '*** Cleaning up ***'
32
29
  Navvy::Job.cleanup
33
30
  break
34
31
  end
@@ -42,41 +39,12 @@ module Navvy
42
39
  def self.fetch_and_run_jobs
43
40
  Job.next.each do |job|
44
41
  result = job.run
45
- Navvy::Log.info(
42
+ Navvy.logger.colorized_info(
46
43
  "* #{job.object.to_s}.#{job.method_name}" <<
47
44
  "(#{job.args.join(', ')}) => #{(job.exception || result).to_s}",
48
45
  job.failed? ? 31 : 32
49
46
  )
50
47
  end
51
48
  end
52
-
53
- ##
54
- # Daemonize the worker
55
-
56
- def self.daemonize(*args)
57
- if defined?(ActiveRecord)
58
- # Sets ActiveRecord's logger to Navvy a new Logger instance
59
- ActiveRecord::Base.logger = Logger.new(STDOUT)
60
- end
61
-
62
- # If #daemonize does not receive any arguments, the options variable will
63
- # contain an empty hash, and the ARGV of the environment will be used instead
64
- # of the :ARGV options from Daemons#run_proc. However, if the *args param has been set
65
- # this will be used instead of the environment's ARGV for the Daemons.
66
- options = args.empty? ? {} : {:ARGV => args}
67
-
68
- # Finally, the directory store mode will be set to normal and the Daemons PID file
69
- # will be stored inside tmp/pids of the application.
70
- options.merge!({:dir_mode => :normal, :dir => 'tmp/pids'})
71
-
72
- # Ensures that the tmp/pids folder exists so that the process id file can properly be stored
73
- %x(mkdir -p tmp/pids)
74
-
75
- # Runs the Navvy Worker inside a Daemon
76
- Daemons.run_proc('navvy', options) do
77
- Navvy::Worker.start
78
- end
79
- end
80
-
81
49
  end
82
50
  end
@@ -5,81 +5,64 @@ describe Navvy::Configuration do
5
5
  Navvy.configure do |config|
6
6
  config.job_limit = 100
7
7
  config.keep_jobs = false
8
- config.logger = nil
9
- config.quiet = true
8
+ config.logger = Navvy::Logger.new('/dev/null')
10
9
  config.sleep_time = 5
11
10
  end
12
11
  end
13
-
12
+
14
13
  it 'should have a job limit of 100 by default' do
15
14
  Navvy::Job.limit.should == 100
16
15
  end
17
-
16
+
18
17
  it 'should set the job limit' do
19
18
  Navvy.configure do |config|
20
19
  config.job_limit = 10
21
20
  end
22
-
21
+
23
22
  Navvy::Job.limit.should == 10
24
23
  end
25
-
24
+
26
25
  it 'should have keep_jobs off by default' do
27
26
  Navvy::Job.keep.should == false
28
27
  end
29
-
28
+
30
29
  it 'should set keep_jobs' do
31
30
  Navvy.configure do |config|
32
31
  config.keep_jobs = 10
33
32
  end
34
-
33
+
35
34
  Navvy::Job.keep.should == 10
36
35
  end
37
-
38
- it 'should not have a logger by default' do
39
- Navvy::Log.logger.should == nil
40
- end
41
-
42
- it 'should set the keep_jobs' do
43
- Navvy.configure do |config|
44
- config.logger = :rails
45
- end
46
-
47
- Navvy::Log.logger.should == :rails
48
- end
49
-
50
- it 'should be quiet in the specs' do
51
- Navvy::Log.quiet.should == true
52
- end
53
-
54
- it 'should turn quiet off' do
36
+
37
+ it 'should set the logger' do
55
38
  Navvy.configure do |config|
56
- config.quiet = false
39
+ config.logger = Navvy::Logger.new
57
40
  end
58
-
59
- Navvy::Log.quiet.should == false
41
+
42
+ Navvy.logger.instance_variable_get(:@logdev).filename.should == nil
60
43
  end
61
-
44
+
62
45
  it 'should have a default sleep time of 5' do
63
46
  Navvy::Worker.sleep_time.should == 5
64
47
  end
65
-
48
+
66
49
  it 'should turn quiet off' do
67
50
  Navvy.configure do |config|
68
51
  config.sleep_time = 10
69
52
  end
70
-
53
+
71
54
  Navvy::Worker.sleep_time.should == 10
72
55
  end
73
-
56
+
74
57
  it 'should have a default max_attempts of 25' do
75
58
  Navvy::Job.max_attempts.should == 25
76
59
  end
77
-
60
+
78
61
  it 'should set max_attempts to 15' do
79
62
  Navvy.configure do |config|
80
63
  config.max_attempts = 15
81
- end
82
-
64
+ end
65
+
83
66
  Navvy::Job.max_attempts.should == 15
84
67
  end
85
- end
68
+ end
@@ -40,7 +40,7 @@ describe 'Navvy::Job' do
40
40
 
41
41
  describe '.enqueue' do
42
42
  before(:each) do
43
- delete_all_jobs
43
+ Navvy::Job.delete_all
44
44
  end
45
45
 
46
46
  it 'should enqueue a job' do
@@ -91,7 +91,7 @@ describe 'Navvy::Job' do
91
91
 
92
92
  describe '.next' do
93
93
  before(:each) do
94
- delete_all_jobs
94
+ Navvy::Job.delete_all
95
95
  Navvy::Job.create(
96
96
  :object => 'Cow',
97
97
  :method_name => :last,
@@ -122,7 +122,7 @@ describe 'Navvy::Job' do
122
122
 
123
123
  it 'should find the next 10 available jobs' do
124
124
  jobs = Navvy::Job.next
125
- jobs.count.should == 100
125
+ jobs.length.should == 100
126
126
  jobs.each do |job|
127
127
  job.should be_instance_of Navvy::Job
128
128
  job.method_name.to_s.should == 'speak'
@@ -130,18 +130,18 @@ describe 'Navvy::Job' do
130
130
  end
131
131
 
132
132
  it 'should find the next 2 available jobs' do
133
- Navvy::Job.next(2).count.should == 2
133
+ Navvy::Job.next(2).length.should == 2
134
134
  end
135
135
 
136
136
  it 'should find the next 4 available jobs' do
137
137
  Navvy::Job.limit = 4
138
- Navvy::Job.next.count.should == 4
138
+ Navvy::Job.next.length.should == 4
139
139
  end
140
140
  end
141
141
 
142
142
  describe '.cleanup' do
143
143
  before(:each) do
144
- delete_all_jobs
144
+ Navvy::Job.delete_all
145
145
  Navvy::Job.create(
146
146
  :object => 'Cow',
147
147
  :method_name => :speak,
@@ -186,7 +186,7 @@ describe 'Navvy::Job' do
186
186
 
187
187
  describe '#run' do
188
188
  it 'should pass the arguments' do
189
- delete_all_jobs
189
+ Navvy::Job.delete_all
190
190
  job = Navvy::Job.enqueue(Cow, :name, 'Betsy')
191
191
  Cow.should_receive(:name).with('Betsy')
192
192
  job.run
@@ -194,7 +194,7 @@ describe 'Navvy::Job' do
194
194
 
195
195
  describe 'when everything goes well' do
196
196
  before(:each) do
197
- delete_all_jobs
197
+ Navvy::Job.delete_all
198
198
  Navvy::Job.enqueue(Cow, :speak)
199
199
  Navvy::Job.keep = false
200
200
  end
@@ -235,7 +235,7 @@ describe 'Navvy::Job' do
235
235
 
236
236
  describe 'when a job fails' do
237
237
  before(:each) do
238
- delete_all_jobs
238
+ Navvy::Job.delete_all
239
239
  Navvy::Job.enqueue(Cow, :broken)
240
240
  end
241
241
 
@@ -251,7 +251,7 @@ describe 'Navvy::Job' do
251
251
 
252
252
  describe '#started' do
253
253
  before(:each) do
254
- delete_all_jobs
254
+ Navvy::Job.delete_all
255
255
  Navvy::Job.enqueue(Cow, :speak)
256
256
  end
257
257
 
@@ -264,7 +264,7 @@ describe 'Navvy::Job' do
264
264
 
265
265
  describe '#completed' do
266
266
  before(:each) do
267
- delete_all_jobs
267
+ Navvy::Job.delete_all
268
268
  Navvy::Job.enqueue(Cow, :speak)
269
269
  end
270
270
 
@@ -283,7 +283,7 @@ describe 'Navvy::Job' do
283
283
 
284
284
  describe '#failed' do
285
285
  before(:each) do
286
- delete_all_jobs
286
+ Navvy::Job.delete_all
287
287
  Navvy::Job.enqueue(Cow, :speak)
288
288
  end
289
289
 
@@ -323,7 +323,7 @@ describe 'Navvy::Job' do
323
323
 
324
324
  describe '#retry' do
325
325
  before(:each) do
326
- delete_all_jobs
326
+ Navvy::Job.delete_all
327
327
  end
328
328
 
329
329
  it 'should enqueue a child for the failed job' do
@@ -388,7 +388,7 @@ describe 'Navvy::Job' do
388
388
 
389
389
  describe '#times_failed' do
390
390
  before(:each) do
391
- delete_all_jobs
391
+ Navvy::Job.delete_all
392
392
  @failed_job = Navvy::Job.create(
393
393
  :failed_at => Time.now
394
394
  )
@@ -0,0 +1,23 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Navvy::Logger do
4
+ describe '#colorized_info' do
5
+ describe 'when logging to STDOUT' do
6
+ it 'should use the provided colors' do
7
+ logger = Navvy::Logger.new
8
+ logger.should_not_receive(:info).with('colors!')
9
+ logger.should_receive(:info).with("\e[32mcolors!\e[0m")
10
+ logger.colorized_info 'colors!', 32
11
+ end
12
+ end
13
+
14
+ describe 'when logging to a file' do
15
+ it 'should not use the provided colors' do
16
+ logger = Navvy::Logger.new('/dev/null')
17
+ logger.should_receive(:info).with('colors!')
18
+ logger.should_not_receive(:info).with("\e[32mcolors!\e[0m")
19
+ logger.colorized_info 'colors!', 32
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,5 +1,4 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../../lib/navvy/job/active_record')
2
- require 'rubygems'
3
2
 
4
3
  ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => '/tmp/navvy_test.sqlite')
5
4
 
@@ -0,0 +1,9 @@
1
+ require 'mongoid'
2
+
3
+ Mongoid.configure do |config|
4
+ name = "navvy_test"
5
+ config.allow_dynamic_fields = false
6
+ config.master = Mongo::Connection.new.db(name)
7
+ end
8
+
9
+ require File.expand_path(File.dirname(__FILE__) + '/../../lib/navvy/job/mongoid')
@@ -1,4 +1,3 @@
1
- require 'rubygems'
2
1
  require 'sequel'
3
2
 
4
3
  Sequel.sqlite('/tmp/navvy_test.sqlite')
@@ -7,16 +7,6 @@ require 'spec/autorun'
7
7
  Spec::Runner.configure do |config|
8
8
  end
9
9
 
10
- def delete_all_jobs
11
- if defined? Navvy::Job.delete_all
12
- Navvy::Job.delete_all
13
- elsif defined? Navvy::Job.all.destroy
14
- Navvy::Job.all.destroy
15
- else
16
- Navvy::Job.delete
17
- end
18
- end
19
-
20
10
  def job_count
21
11
  if defined? Navvy::Job.count
22
12
  Navvy::Job.count
@@ -50,7 +40,3 @@ module Animals
50
40
  end
51
41
  end
52
42
  end
53
-
54
- Navvy.configure do |config|
55
- config.quiet = true
56
- end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 2
8
- - 5
9
- version: 0.2.5
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jeff Kreeftmeijer
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-29 00:00:00 +02:00
17
+ date: 2010-06-28 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -45,97 +45,49 @@ dependencies:
45
45
  version: 0.5.2
46
46
  type: :development
47
47
  version_requirements: *id002
48
- - !ruby/object:Gem::Dependency
49
- name: sequel
50
- prerelease: false
51
- requirement: &id003 !ruby/object:Gem::Requirement
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- segments:
56
- - 3
57
- - 8
58
- - 0
59
- version: 3.8.0
60
- type: :development
61
- version_requirements: *id003
62
- - !ruby/object:Gem::Dependency
63
- name: sqlite3-ruby
64
- prerelease: false
65
- requirement: &id004 !ruby/object:Gem::Requirement
66
- requirements:
67
- - - ">="
68
- - !ruby/object:Gem::Version
69
- segments:
70
- - 1
71
- - 2
72
- - 5
73
- version: 1.2.5
74
- type: :development
75
- version_requirements: *id004
76
- - !ruby/object:Gem::Dependency
77
- name: daemons
78
- prerelease: false
79
- requirement: &id005 !ruby/object:Gem::Requirement
80
- requirements:
81
- - - ">="
82
- - !ruby/object:Gem::Version
83
- segments:
84
- - 1
85
- - 0
86
- - 10
87
- version: 1.0.10
88
- type: :runtime
89
- version_requirements: *id005
90
48
  description: Simple background job processor inspired by delayed_job, but aiming for database agnosticism.
91
49
  email: jeff@kreeftmeijer.nl
92
50
  executables: []
93
51
 
94
52
  extensions: []
95
53
 
96
- extra_rdoc_files:
97
- - LICENSE
98
- - README.textile
54
+ extra_rdoc_files: []
55
+
99
56
  files:
100
- - .document
101
- - .gitignore
102
- - LICENSE
103
- - README.textile
104
- - Rakefile
105
- - VERSION
106
57
  - generators/navvy/navvy_generator.rb
107
- - generators/navvy/templates/migration.rb
108
- - generators/navvy/templates/script
58
+ - generators/navvy/templates/active_record_migration.rb
59
+ - generators/navvy/templates/sequel_migration.rb
109
60
  - lib/generators/navvy_generator.rb
110
- - lib/navvy.rb
111
61
  - lib/navvy/configuration.rb
112
- - lib/navvy/job.rb
113
62
  - lib/navvy/job/active_record.rb
114
63
  - lib/navvy/job/data_mapper.rb
115
64
  - lib/navvy/job/mongo_mapper.rb
65
+ - lib/navvy/job/mongoid.rb
116
66
  - lib/navvy/job/sequel.rb
117
- - lib/navvy/log.rb
67
+ - lib/navvy/job.rb
68
+ - lib/navvy/logger.rb
118
69
  - lib/navvy/tasks.rb
119
70
  - lib/navvy/worker.rb
120
- - navvy.gemspec
71
+ - lib/navvy.rb
121
72
  - spec/configuration_spec.rb
122
73
  - spec/job_spec.rb
123
- - spec/log_spec.rb
74
+ - spec/logger_spec.rb
124
75
  - spec/setup/active_record.rb
125
76
  - spec/setup/data_mapper.rb
126
- - spec/setup/justlogging.rb
127
77
  - spec/setup/mongo_mapper.rb
128
- - spec/setup/rails_default_logger.rb
78
+ - spec/setup/mongoid.rb
129
79
  - spec/setup/sequel.rb
130
80
  - spec/spec_helper.rb
131
81
  - spec/worker_spec.rb
82
+ - README.textile
83
+ - LICENSE
132
84
  has_rdoc: true
133
85
  homepage: http://github.com/jeffkreeftmeijer/navvy
134
86
  licenses: []
135
87
 
136
88
  post_install_message:
137
- rdoc_options:
138
- - --charset=UTF-8
89
+ rdoc_options: []
90
+
139
91
  require_paths:
140
92
  - lib
141
93
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -159,15 +111,5 @@ rubygems_version: 1.3.6
159
111
  signing_key:
160
112
  specification_version: 3
161
113
  summary: Simple background job processor inspired by delayed_job, but aiming for database agnosticism.
162
- test_files:
163
- - spec/configuration_spec.rb
164
- - spec/job_spec.rb
165
- - spec/log_spec.rb
166
- - spec/setup/active_record.rb
167
- - spec/setup/data_mapper.rb
168
- - spec/setup/justlogging.rb
169
- - spec/setup/mongo_mapper.rb
170
- - spec/setup/rails_default_logger.rb
171
- - spec/setup/sequel.rb
172
- - spec/spec_helper.rb
173
- - spec/worker_spec.rb
114
+ test_files: []
115
+
data/.document DELETED
@@ -1,5 +0,0 @@
1
- README.rdoc
2
- lib/**/*.rb
3
- bin/*
4
- features/**/*.feature
5
- LICENSE
data/.gitignore DELETED
@@ -1,8 +0,0 @@
1
- *.sw?
2
- .DS_Store
3
- .yardoc
4
- coverage
5
- rdoc
6
- doc
7
- pkg
8
- tmp
data/Rakefile DELETED
@@ -1,67 +0,0 @@
1
- require 'rubygems'
2
- require 'rake'
3
-
4
- begin
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |gem|
7
- gem.name = "navvy"
8
- gem.summary = %Q{Simple background job processor inspired by delayed_job, but aiming for database agnosticism.}
9
- gem.description = %Q{Simple background job processor inspired by delayed_job, but aiming for database agnosticism.}
10
- gem.email = "jeff@kreeftmeijer.nl"
11
- gem.homepage = "http://github.com/jeffkreeftmeijer/navvy"
12
- gem.authors = ["Jeff Kreeftmeijer"]
13
-
14
- gem.add_development_dependency "rspec", ">= 1.2.9"
15
- gem.add_development_dependency "yard", ">= 0.5.2"
16
- gem.add_development_dependency "sequel", ">= 3.8.0"
17
- gem.add_development_dependency "sqlite3-ruby", ">= 1.2.5"
18
- gem.add_dependency "daemons", ">= 1.0.10"
19
- end
20
- Jeweler::GemcutterTasks.new
21
- rescue LoadError
22
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
23
- end
24
-
25
- require 'spec/rake/spectask'
26
-
27
- task :spec do
28
- ['spec:active_record', 'spec:mongo_mapper', 'spec:sequel', 'spec:data_mapper'].each do |spec|
29
- Rake::Task[spec].invoke
30
- end
31
- end
32
-
33
- namespace :spec do
34
- Spec::Rake::SpecTask.new(:active_record) do |spec|
35
- spec.spec_files = FileList['spec/setup/active_record.rb', 'spec/*_spec.rb']
36
- end
37
-
38
- Spec::Rake::SpecTask.new(:mongo_mapper) do |spec|
39
- spec.spec_files = FileList['spec/setup/mongo_mapper.rb', 'spec/*_spec.rb']
40
- end
41
-
42
- Spec::Rake::SpecTask.new(:sequel) do |spec|
43
- spec.spec_files = FileList['spec/setup/sequel.rb', 'spec/*_spec.rb']
44
- end
45
-
46
- Spec::Rake::SpecTask.new(:data_mapper) do |spec|
47
- spec.spec_files = FileList['spec/setup/data_mapper.rb', 'spec/*_spec.rb']
48
- end
49
- end
50
-
51
- Spec::Rake::SpecTask.new(:rcov) do |spec|
52
- spec.libs << 'lib' << 'spec'
53
- spec.pattern = 'spec/**/*_spec.rb'
54
- spec.rcov = true
55
- end
56
-
57
- task :spec => :check_dependencies
58
- task :default => :spec
59
-
60
- begin
61
- require 'yard'
62
- YARD::Rake::YardocTask.new
63
- rescue LoadError
64
- task :yardoc do
65
- abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
66
- end
67
- end
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.2.5
@@ -1,4 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
4
- Navvy::Worker.daemonize
@@ -1,74 +0,0 @@
1
- module Navvy
2
- class Log
3
- class << self
4
- attr_writer :logger
5
- attr_accessor :quiet
6
- end
7
-
8
- class LoggerNotFound < StandardError; end
9
-
10
- ##
11
- # Default logger
12
- #
13
- # @return [Symbol, nil] logger
14
-
15
- def self.logger
16
- @logger || Navvy.configuration.logger
17
- end
18
-
19
- ##
20
- # Be quiet?
21
- #
22
- # @return [true, false] quiet
23
-
24
- def self.quiet
25
- @quiet || Navvy.configuration.quiet
26
- end
27
-
28
- ##
29
- # Pass a log to the logger. It will check if self.logger is an array. If it
30
- # is, it'll loop through it and log to every logger. If it's not, it'll
31
- # just log once.
32
- #
33
- # @param [String] message the message you want to log
34
- # @param [Integer] color an optional color code to use in the terminal
35
- # output
36
-
37
- def self.info(message, color = nil)
38
- if logger.is_a? Array
39
- logger.each do |logger|
40
- write(logger, message, color)
41
- end
42
- else
43
- write(logger, message, color)
44
- end
45
- end
46
-
47
- ##
48
- # Actually write the log to the logger. It'll check self.logger and use
49
- # that to define a logger
50
- #
51
- # @param [Symbol] logger the logger you want to use
52
- # @param [String] message the message you want to log
53
- # @param [Integer] color an optional color code to use in the terminal
54
- # output
55
-
56
- def self.write(logger, message, color = nil)
57
- puts "\e[#{color}m#{message}\e[0m" unless quiet
58
- case logger
59
- when :justlogging
60
- raise(
61
- LoggerNotFound,
62
- 'JustLogging could not be found. No logs were created.'
63
- ) unless defined? Justlogging.log
64
- Justlogging.log(message)
65
- when :rails
66
- raise(
67
- LoggerNotFound,
68
- 'Rails.logger could not be found. No logs were created.'
69
- ) unless defined? Rails.logger.info
70
- Rails.logger.info(message)
71
- end
72
- end
73
- end
74
- end
@@ -1,96 +0,0 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
- # -*- encoding: utf-8 -*-
5
-
6
- Gem::Specification.new do |s|
7
- s.name = %q{navvy}
8
- s.version = "0.2.5"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Jeff Kreeftmeijer"]
12
- s.date = %q{2010-05-29}
13
- s.description = %q{Simple background job processor inspired by delayed_job, but aiming for database agnosticism.}
14
- s.email = %q{jeff@kreeftmeijer.nl}
15
- s.extra_rdoc_files = [
16
- "LICENSE",
17
- "README.textile"
18
- ]
19
- s.files = [
20
- ".document",
21
- ".gitignore",
22
- "LICENSE",
23
- "README.textile",
24
- "Rakefile",
25
- "VERSION",
26
- "generators/navvy/navvy_generator.rb",
27
- "generators/navvy/templates/migration.rb",
28
- "generators/navvy/templates/script",
29
- "lib/generators/navvy_generator.rb",
30
- "lib/navvy.rb",
31
- "lib/navvy/configuration.rb",
32
- "lib/navvy/job.rb",
33
- "lib/navvy/job/active_record.rb",
34
- "lib/navvy/job/data_mapper.rb",
35
- "lib/navvy/job/mongo_mapper.rb",
36
- "lib/navvy/job/sequel.rb",
37
- "lib/navvy/log.rb",
38
- "lib/navvy/tasks.rb",
39
- "lib/navvy/worker.rb",
40
- "navvy.gemspec",
41
- "spec/configuration_spec.rb",
42
- "spec/job_spec.rb",
43
- "spec/log_spec.rb",
44
- "spec/setup/active_record.rb",
45
- "spec/setup/data_mapper.rb",
46
- "spec/setup/justlogging.rb",
47
- "spec/setup/mongo_mapper.rb",
48
- "spec/setup/rails_default_logger.rb",
49
- "spec/setup/sequel.rb",
50
- "spec/spec_helper.rb",
51
- "spec/worker_spec.rb"
52
- ]
53
- s.homepage = %q{http://github.com/jeffkreeftmeijer/navvy}
54
- s.rdoc_options = ["--charset=UTF-8"]
55
- s.require_paths = ["lib"]
56
- s.rubygems_version = %q{1.3.6}
57
- s.summary = %q{Simple background job processor inspired by delayed_job, but aiming for database agnosticism.}
58
- s.test_files = [
59
- "spec/configuration_spec.rb",
60
- "spec/job_spec.rb",
61
- "spec/log_spec.rb",
62
- "spec/setup/active_record.rb",
63
- "spec/setup/data_mapper.rb",
64
- "spec/setup/justlogging.rb",
65
- "spec/setup/mongo_mapper.rb",
66
- "spec/setup/rails_default_logger.rb",
67
- "spec/setup/sequel.rb",
68
- "spec/spec_helper.rb",
69
- "spec/worker_spec.rb"
70
- ]
71
-
72
- if s.respond_to? :specification_version then
73
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
74
- s.specification_version = 3
75
-
76
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
77
- s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
78
- s.add_development_dependency(%q<yard>, [">= 0.5.2"])
79
- s.add_development_dependency(%q<sequel>, [">= 3.8.0"])
80
- s.add_development_dependency(%q<sqlite3-ruby>, [">= 1.2.5"])
81
- s.add_runtime_dependency(%q<daemons>, [">= 1.0.10"])
82
- else
83
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
84
- s.add_dependency(%q<yard>, [">= 0.5.2"])
85
- s.add_dependency(%q<sequel>, [">= 3.8.0"])
86
- s.add_dependency(%q<sqlite3-ruby>, [">= 1.2.5"])
87
- s.add_dependency(%q<daemons>, [">= 1.0.10"])
88
- end
89
- else
90
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
91
- s.add_dependency(%q<yard>, [">= 0.5.2"])
92
- s.add_dependency(%q<sequel>, [">= 3.8.0"])
93
- s.add_dependency(%q<sqlite3-ruby>, [">= 1.2.5"])
94
- s.add_dependency(%q<daemons>, [">= 1.0.10"])
95
- end
96
- end
@@ -1,63 +0,0 @@
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.logger' do
17
- require File.expand_path(File.dirname(__FILE__) + '/setup/rails_default_logger')
18
- Rails.logger.should_receive(:info).with('123')
19
- Navvy::Log.info('123')
20
- end
21
- end
22
-
23
- describe 'when using justlogging' do
24
- before do
25
- Navvy::Log.logger = :justlogging
26
- end
27
-
28
- it 'should raise an error when the logger can not be found' do
29
- lambda { Navvy::Log.info('123') }.should raise_error
30
- end
31
-
32
- it 'should pass the log to justlogging' do
33
- require File.expand_path(File.dirname(__FILE__) + '/setup/justlogging')
34
- Justlogging.should_receive(:log).with('123')
35
- Navvy::Log.info('123')
36
- end
37
- end
38
-
39
- describe 'when using both the rails default logger and justlogging' do
40
- before do
41
- Navvy::Log.logger = [:rails, :justlogging]
42
- end
43
-
44
- it 'should pass the log to justlogging' do
45
- Rails.logger.should_receive(:info).with('123')
46
- Justlogging.should_receive(:log).with('123')
47
- Navvy::Log.info('123')
48
- end
49
- end
50
-
51
- describe 'when not using any logger' do
52
- before do
53
- Navvy::Log.logger = nil
54
- end
55
-
56
- it 'should not log' do
57
- Rails.logger.should_not_receive(:info)
58
- Justlogging.should_not_receive(:log)
59
- Navvy::Log.info('123')
60
- end
61
- end
62
- end
63
- end
@@ -1,3 +0,0 @@
1
- class Justlogging
2
- def self.log(text);end
3
- end
@@ -1,9 +0,0 @@
1
- class Rails
2
- def self.logger
3
- Logger
4
- end
5
-
6
- class Logger
7
- def self.info(text);end
8
- end
9
- end