cronjobber 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. data/CHANGELOG +1 -0
  2. data/Gemfile +11 -0
  3. data/Gemfile.lock +98 -0
  4. data/LICENSE +20 -0
  5. data/README.rdoc +81 -0
  6. data/Rakefile +45 -0
  7. data/VERSION +1 -0
  8. data/app/models/cronjobber/task.rb +170 -0
  9. data/cronjobber.gemspec +88 -0
  10. data/init.rb +1 -0
  11. data/lib/cronjobber.rb +21 -0
  12. data/lib/cronjobber/tasks_helper.rb +20 -0
  13. data/spec/cronjobber_spec.rb +256 -0
  14. data/spec/dummy/Rakefile +7 -0
  15. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  16. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  17. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  18. data/spec/dummy/config.ru +4 -0
  19. data/spec/dummy/config/application.rb +45 -0
  20. data/spec/dummy/config/boot.rb +10 -0
  21. data/spec/dummy/config/database.yml +22 -0
  22. data/spec/dummy/config/environment.rb +5 -0
  23. data/spec/dummy/config/environments/development.rb +26 -0
  24. data/spec/dummy/config/environments/production.rb +49 -0
  25. data/spec/dummy/config/environments/test.rb +35 -0
  26. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  27. data/spec/dummy/config/initializers/inflections.rb +10 -0
  28. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  29. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  30. data/spec/dummy/config/initializers/session_store.rb +8 -0
  31. data/spec/dummy/config/locales/en.yml +5 -0
  32. data/spec/dummy/config/routes.rb +58 -0
  33. data/spec/dummy/db/migrate/20110112183948_create_cronjobs.rb +18 -0
  34. data/spec/dummy/db/schema.rb +26 -0
  35. data/spec/dummy/public/404.html +26 -0
  36. data/spec/dummy/public/422.html +26 -0
  37. data/spec/dummy/public/500.html +26 -0
  38. data/spec/dummy/public/favicon.ico +0 -0
  39. data/spec/dummy/public/javascripts/application.js +2 -0
  40. data/spec/dummy/public/javascripts/controls.js +965 -0
  41. data/spec/dummy/public/javascripts/dragdrop.js +974 -0
  42. data/spec/dummy/public/javascripts/effects.js +1123 -0
  43. data/spec/dummy/public/javascripts/prototype.js +6001 -0
  44. data/spec/dummy/public/javascripts/rails.js +175 -0
  45. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  46. data/spec/dummy/script/rails +6 -0
  47. data/spec/spec_helper.rb +28 -0
  48. metadata +141 -0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'cronjob'
@@ -0,0 +1,21 @@
1
+ require "cronjobber"
2
+ require "cronjobber/tasks_helper"
3
+
4
+ module Cronjobber
5
+
6
+ mattr_accessor :tasks
7
+ @@tasks = []
8
+
9
+ def self.setup
10
+ yield self
11
+ end
12
+
13
+ class Engine < Rails::Engine#:nodoc:
14
+ config.cronjobber = Cronjobber
15
+
16
+ initializer "cronjobber.initialize" do |app|
17
+ app.config.cronjobber = Cronjobber
18
+ ActionController::Base.send :include, Cronjobber::TasksHelper
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ module Cronjobber
2
+ module TasksHelper
3
+
4
+ def execute_cronjob_tasks tasks=nil
5
+ tasks = Array(tasks) + Cronjobber.tasks
6
+ tasks.map! do |task|
7
+ if task.is_a? String
8
+ task = task.camelize.constantize
9
+ else
10
+ task
11
+ end
12
+ end
13
+ tasks.map! do |task|
14
+ task.cronjob_perform
15
+ end
16
+ return tasks, tasks.map(&:format)
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,256 @@
1
+ require "spec_helper"
2
+
3
+ describe Cronjobber::Task do
4
+
5
+ after :each do
6
+ Cronjobber::Task.destroy_all
7
+ end
8
+
9
+ describe "initialization" do
10
+ before :each do
11
+ class DefaultJob < Cronjobber::Task
12
+ run_task
13
+ end
14
+
15
+ class CustomizedJob < Cronjobber::Task
16
+ run_task :every => 10.minutes, :in_background => true, :method => :custom_run_method, :at => ["12:34", "56:78"]
17
+ end
18
+
19
+ module ModulizedJob
20
+ class CustomizedJob < Cronjobber::Task
21
+ run_task :every => 10.minutes, :in_background => true, :method => :custom_run_method, :at => ["12:34", "56:78"]
22
+ end
23
+ end
24
+ end
25
+
26
+ it "should fill default values" do
27
+ DefaultJob.cronjob_frequency.should == 0.minutes
28
+ DefaultJob.cronjob_delayed.should be false
29
+ DefaultJob.cronjob_method.should == 'run'
30
+ DefaultJob.cronjob_timesteps.should == []
31
+ end
32
+
33
+ it "should change frequency" do
34
+ CustomizedJob.cronjob_frequency.should == 10.minutes
35
+ end
36
+
37
+ it "should change delay option" do
38
+ CustomizedJob.cronjob_delayed.should be true
39
+ end
40
+
41
+ it "should change run method" do
42
+ CustomizedJob.cronjob_method.should == "custom_run_method"
43
+ end
44
+
45
+ it "should initialize with timesteps" do
46
+ CustomizedJob.cronjob_timesteps.should == ["12:34", "56:78"]
47
+ end
48
+
49
+ it "should generate cronjob name" do
50
+ DefaultJob.cronjob_name.should == "default_job"
51
+ ModulizedJob::CustomizedJob.cronjob_name.should == "modulized_job/customized_job"
52
+ end
53
+ end
54
+
55
+ describe "instance" do
56
+ before :each do
57
+ class TestJob < Cronjobber::Task
58
+ run_task
59
+ end
60
+
61
+ class FailJob < Cronjobber::Task
62
+ run_task
63
+
64
+ def run
65
+ raise "job failed with exception message"
66
+ end
67
+ end
68
+
69
+ class BackgroundJob < Cronjobber::Task
70
+ run_task :in_background => true
71
+ def self.cronjob_enqueue
72
+ # mock with empty method for testing
73
+ end
74
+ end
75
+
76
+ @job = TestJob.create!({ :run_at => Time.now - 1.day, :name => TestJob.cronjob_name })
77
+ FailJob.create!({ :run_at => Time.now - 1.day, :name => FailJob.cronjob_name })
78
+ BackgroundJob.create!({ :run_at => Time.now - 1.day, :name => BackgroundJob.cronjob_name })
79
+ end
80
+
81
+ after :each do
82
+ Cronjobber::Task.destroy_all
83
+ end
84
+
85
+ it "should lock" do
86
+ @job.locked?.should be false
87
+ @job.lock!.should be true
88
+ @job.locked?.should be true
89
+ end
90
+
91
+ it "should lock only once" do
92
+ @job.lock!.should be true
93
+ @job.lock!.should be false
94
+ end
95
+
96
+ it "should unlock" do
97
+ @job.lock!.should be true
98
+ @job.unlock!.should be true
99
+ @job.locked?.should be false
100
+ end
101
+
102
+ it "should be locked for invalid key" do
103
+ @job.lock!.should be true
104
+ @job.locked?("123456").should be true
105
+ end
106
+
107
+ it "should not be locked for valid key" do
108
+ @job.lock!.should be true
109
+ @job.locked?(@job.locking_key).should be false
110
+ end
111
+
112
+ it "should perform the job" do
113
+ job = TestJob.cronjob_perform
114
+ job.id.should be @job.id
115
+
116
+ job.locked?.should be false
117
+ job.last_error.should be_nil
118
+ job.duration.should_not be_nil
119
+ job.status.should == "performed"
120
+ end
121
+
122
+ it "should not perform the job when exception occurs" do
123
+ job = FailJob.cronjob_perform
124
+ job.locked?.should be false
125
+ job.status.should == "exception"
126
+ job.last_error.starts_with?("job failed with exception message").should be true
127
+ end
128
+
129
+ it "should enqueue the job in background" do
130
+ job = BackgroundJob.cronjob_perform
131
+ job.locked?.should be true
132
+ job.status.should == "enqueued"
133
+ job.last_error.should be_nil
134
+ end
135
+
136
+ it "should perform background job with valid key" do
137
+ job = BackgroundJob.cronjob_perform
138
+ job.locked?.should be true
139
+ BackgroundJob.cronjob_perform_delayed(job.locking_key)
140
+ job.locked?.should be false
141
+ job.status.should == "performed"
142
+ job.last_error.should be_nil
143
+ end
144
+
145
+ it "should not perform background job with invalidvalid key" do
146
+ job = BackgroundJob.cronjob_perform
147
+ job.locked?.should be true
148
+ BackgroundJob.cronjob_perform_delayed("some invalid key")
149
+ job.locked?.should be true
150
+ job.status.should == "locked"
151
+ end
152
+ end
153
+
154
+ describe "without frequency and without time" do
155
+ before :each do
156
+ class TestJob < Cronjobber::Task
157
+ run_task :every => 0.minutes, :at => []
158
+ end
159
+ @test_job = TestJob.new({ :run_at => Time.parse("2011-1-1 12:00") })
160
+ @test_job.save!
161
+ end
162
+
163
+ it "should be runable before run_at time" do
164
+ @test_job.should_run?(Time.parse("2011-1-1 11:00")).should be true
165
+ end
166
+
167
+ it "should be runable on run_at time" do
168
+ @test_job.should_run?(Time.parse("2011-1-1 12:00")).should be true
169
+ end
170
+
171
+ it "should be runable after run_at time" do
172
+ @test_job.should_run?(Time.parse("2011-1-1 13:00")).should be true
173
+ end
174
+ end
175
+
176
+ describe "with frequency and without time" do
177
+ before :each do
178
+ class TestJob < Cronjobber::Task
179
+ run_task :every => 10.minutes, :at => []
180
+ end
181
+ @test_job = TestJob.new({ :run_at => Time.parse("2011-1-1 12:00") })
182
+ @test_job.save!
183
+ end
184
+
185
+ it "should not be runable before run_at time" do
186
+ @test_job.should_run?(Time.parse("2011-1-1 11:00")).should be false
187
+ end
188
+
189
+ it "should not be runable on run_at time" do
190
+ @test_job.should_run?(Time.parse("2011-1-1 12:00")).should be false
191
+ end
192
+
193
+ it "should not be runable after run_at time before frequency elapses" do
194
+ @test_job.should_run?(Time.parse("2011-1-1 12:09")).should be false
195
+ end
196
+
197
+ it "should be runable after run_at time after frequency elapsed" do
198
+ @test_job.should_run?(Time.parse("2011-1-1 12:10")).should be true
199
+ end
200
+ end
201
+
202
+ describe "without frequency and with time" do
203
+ before :each do
204
+ class TestJob < Cronjobber::Task
205
+ run_task :every => 0.minutes, :at => ["12:00"]
206
+ end
207
+ @test_job = TestJob.new({ :run_at => Time.parse("2011-1-1 12:00") })
208
+ @test_job.save!
209
+ end
210
+
211
+ it "should not be runable before run_at time" do
212
+ @test_job.should_run?(Time.parse("2010-1-1 11:00")).should be false
213
+ @test_job.should_run?(Time.parse("2011-1-1 11:00")).should be false
214
+ @test_job.should_run?(Time.parse("2011-1-1 11:59")).should be false
215
+ end
216
+
217
+ it "should not be runable on run_at time" do
218
+ @test_job.should_run?(Time.parse("2011-1-1 12:00")).should be false
219
+ end
220
+
221
+ it "should be runable after run_at time" do
222
+ @test_job.should_run?(Time.parse("2011-1-1 12:01")).should be true
223
+ @test_job.should_run?(Time.parse("2011-1-1 13:00")).should be true
224
+ @test_job.should_run?(Time.parse("2012-1-1 12:01")).should be true
225
+ end
226
+ end
227
+
228
+ describe "with frequency and with time" do
229
+ before :each do
230
+ class TestJob < Cronjobber::Task
231
+ run_task :every => 30.minutes, :at => ["12:00"]
232
+ end
233
+ @test_job = TestJob.new({ :run_at => Time.parse("2011-1-1 12:00") })
234
+ @test_job.save!
235
+ end
236
+
237
+ it "should not be runable before run_at time" do
238
+ @test_job.should_run?(Time.parse("2010-1-1 11:00")).should be false
239
+ @test_job.should_run?(Time.parse("2011-1-1 11:00")).should be false
240
+ @test_job.should_run?(Time.parse("2011-1-1 11:59")).should be false
241
+ end
242
+
243
+ it "should not be runable after run_at within frequency time" do
244
+ @test_job.should_run?(Time.parse("2011-1-1 12:00")).should be false
245
+ @test_job.should_run?(Time.parse("2011-1-1 12:01")).should be false
246
+ @test_job.should_run?(Time.parse("2011-1-1 12:29")).should be false
247
+ end
248
+
249
+ it "should be runable after run_at time after frequency time" do
250
+ @test_job.should_run?(Time.parse("2011-1-1 12:30")).should be true
251
+ @test_job.should_run?(Time.parse("2011-1-1 13:31")).should be true
252
+ @test_job.should_run?(Time.parse("2012-1-1 12:00")).should be true
253
+ end
254
+ end
255
+
256
+ end
@@ -0,0 +1,7 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+ require 'rake'
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag :all %>
6
+ <%= javascript_include_tag :defaults %>
7
+ <%= csrf_meta_tag %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Dummy::Application
@@ -0,0 +1,45 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require "active_model/railtie"
4
+ require "active_record/railtie"
5
+ require "action_controller/railtie"
6
+ require "action_view/railtie"
7
+ require "action_mailer/railtie"
8
+
9
+ Bundler.require
10
+ require "cronjobber"
11
+
12
+ module Dummy
13
+ class Application < Rails::Application
14
+ # Settings in config/environments/* take precedence over those specified here.
15
+ # Application configuration should go into files in config/initializers
16
+ # -- all .rb files in that directory are automatically loaded.
17
+
18
+ # Custom directories with classes and modules you want to be autoloadable.
19
+ # config.autoload_paths += %W(#{config.root}/extras)
20
+
21
+ # Only load the plugins named here, in the order given (default is alphabetical).
22
+ # :all can be used as a placeholder for all plugins not explicitly named.
23
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
24
+
25
+ # Activate observers that should always be running.
26
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
27
+
28
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
29
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
30
+ # config.time_zone = 'Central Time (US & Canada)'
31
+
32
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
33
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
34
+ # config.i18n.default_locale = :de
35
+
36
+ # JavaScript files you want as :defaults (application.js is always included).
37
+ # config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
38
+
39
+ # Configure the default encoding used in templates for Ruby 1.9.
40
+ config.encoding = "utf-8"
41
+
42
+ # Configure sensitive parameters which will be filtered from the log file.
43
+ config.filter_parameters += [:password]
44
+ end
45
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ gemfile = File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ if File.exist?(gemfile)
5
+ ENV['BUNDLE_GEMFILE'] = gemfile
6
+ require 'bundler'
7
+ Bundler.setup
8
+ end
9
+
10
+ $:.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,22 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3-ruby (not necessary on OS X Leopard)
3
+ development:
4
+ adapter: sqlite3
5
+ database: db/development.sqlite3
6
+ pool: 5
7
+ timeout: 5000
8
+
9
+ # Warning: The database defined as "test" will be erased and
10
+ # re-generated from your development database when you run "rake".
11
+ # Do not set this db to the same as development or production.
12
+ test:
13
+ adapter: sqlite3
14
+ database: db/test.sqlite3
15
+ pool: 5
16
+ timeout: 5000
17
+
18
+ production:
19
+ adapter: sqlite3
20
+ database: db/production.sqlite3
21
+ pool: 5
22
+ timeout: 5000
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Dummy::Application.initialize!
@@ -0,0 +1,26 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the webserver when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Log error messages when you accidentally call methods on nil.
10
+ config.whiny_nils = true
11
+
12
+ # Show full error reports and disable caching
13
+ config.consider_all_requests_local = true
14
+ config.action_view.debug_rjs = true
15
+ config.action_controller.perform_caching = false
16
+
17
+ # Don't care if the mailer can't send
18
+ config.action_mailer.raise_delivery_errors = false
19
+
20
+ # Print deprecation notices to the Rails logger
21
+ config.active_support.deprecation = :log
22
+
23
+ # Only use best-standards-support built into browsers
24
+ config.action_dispatch.best_standards_support = :builtin
25
+ end
26
+