resque-scheduler 2.5.1 → 4.10.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (83) hide show
  1. checksums.yaml +7 -0
  2. data/.github/dependabot.yml +12 -0
  3. data/.github/funding.yml +4 -0
  4. data/.github/workflows/codeql-analysis.yml +59 -0
  5. data/.github/workflows/rubocop.yml +27 -0
  6. data/.github/workflows/ruby.yml +81 -0
  7. data/AUTHORS.md +25 -0
  8. data/CHANGELOG.md +539 -0
  9. data/CODE_OF_CONDUCT.md +74 -0
  10. data/Gemfile +26 -2
  11. data/README.md +291 -70
  12. data/Rakefile +8 -19
  13. data/exe/resque-scheduler +5 -0
  14. data/lib/resque/scheduler/cli.rb +147 -0
  15. data/lib/resque/scheduler/configuration.rb +102 -0
  16. data/lib/resque/scheduler/delaying_extensions.rb +371 -0
  17. data/lib/resque/scheduler/env.rb +85 -0
  18. data/lib/resque/scheduler/extension.rb +13 -0
  19. data/lib/resque/scheduler/failure_handler.rb +11 -0
  20. data/lib/resque/scheduler/lock/base.rb +13 -4
  21. data/lib/resque/scheduler/lock/basic.rb +4 -5
  22. data/lib/resque/scheduler/lock/resilient.rb +52 -43
  23. data/lib/resque/scheduler/lock.rb +2 -1
  24. data/lib/resque/scheduler/locking.rb +104 -0
  25. data/lib/resque/scheduler/logger_builder.rb +83 -0
  26. data/lib/resque/scheduler/plugin.rb +31 -0
  27. data/lib/resque/scheduler/scheduling_extensions.rb +142 -0
  28. data/lib/{resque_scheduler → resque/scheduler}/server/views/delayed.erb +21 -12
  29. data/lib/{resque_scheduler → resque/scheduler}/server/views/delayed_schedules.erb +1 -1
  30. data/lib/{resque_scheduler → resque/scheduler}/server/views/delayed_timestamp.erb +1 -1
  31. data/lib/resque/scheduler/server/views/scheduler.erb +58 -0
  32. data/lib/{resque_scheduler → resque/scheduler}/server/views/search.erb +4 -7
  33. data/lib/{resque_scheduler → resque/scheduler}/server/views/search_form.erb +1 -5
  34. data/lib/resque/scheduler/server.rb +268 -0
  35. data/lib/resque/scheduler/signal_handling.rb +40 -0
  36. data/lib/{resque_scheduler → resque/scheduler}/tasks.rb +3 -6
  37. data/lib/resque/scheduler/util.rb +39 -0
  38. data/lib/resque/scheduler/version.rb +7 -0
  39. data/lib/resque/scheduler.rb +271 -199
  40. data/lib/resque-scheduler.rb +3 -1
  41. data/resque-scheduler.gemspec +53 -20
  42. metadata +176 -132
  43. data/.gitignore +0 -11
  44. data/.rubocop.yml +0 -129
  45. data/.simplecov +0 -1
  46. data/.travis.yml +0 -21
  47. data/HISTORY.md +0 -226
  48. data/ROADMAP.md +0 -10
  49. data/bin/resque-scheduler +0 -5
  50. data/examples/Rakefile +0 -2
  51. data/examples/config/initializers/resque-web.rb +0 -37
  52. data/examples/dynamic-scheduling/README.md +0 -28
  53. data/examples/dynamic-scheduling/app/jobs/fix_schedules_job.rb +0 -54
  54. data/examples/dynamic-scheduling/app/jobs/send_email_job.rb +0 -9
  55. data/examples/dynamic-scheduling/app/models/user.rb +0 -16
  56. data/examples/dynamic-scheduling/config/resque.yml +0 -4
  57. data/examples/dynamic-scheduling/config/static_schedule.yml +0 -7
  58. data/examples/dynamic-scheduling/lib/tasks/resque.rake +0 -48
  59. data/examples/run-resque-web +0 -3
  60. data/lib/resque/scheduler_locking.rb +0 -91
  61. data/lib/resque_scheduler/cli.rb +0 -160
  62. data/lib/resque_scheduler/logger_builder.rb +0 -70
  63. data/lib/resque_scheduler/plugin.rb +0 -28
  64. data/lib/resque_scheduler/server/views/scheduler.erb +0 -36
  65. data/lib/resque_scheduler/server.rb +0 -182
  66. data/lib/resque_scheduler/util.rb +0 -34
  67. data/lib/resque_scheduler/version.rb +0 -5
  68. data/lib/resque_scheduler.rb +0 -386
  69. data/script/migrate_to_timestamps_set.rb +0 -14
  70. data/tasks/resque_scheduler.rake +0 -2
  71. data/test/cli_test.rb +0 -286
  72. data/test/delayed_queue_test.rb +0 -449
  73. data/test/redis-test.conf +0 -108
  74. data/test/resque-web_test.rb +0 -199
  75. data/test/scheduler_args_test.rb +0 -190
  76. data/test/scheduler_hooks_test.rb +0 -23
  77. data/test/scheduler_locking_test.rb +0 -242
  78. data/test/scheduler_setup_test.rb +0 -95
  79. data/test/scheduler_task_test.rb +0 -35
  80. data/test/scheduler_test.rb +0 -344
  81. data/test/support/redis_instance.rb +0 -134
  82. data/test/test_helper.rb +0 -131
  83. /data/lib/{resque_scheduler → resque/scheduler}/server/views/requeue-params.erb +0 -0
@@ -1,386 +0,0 @@
1
- require 'rubygems'
2
- require 'resque'
3
- require 'resque_scheduler/version'
4
- require 'resque_scheduler/util'
5
- require 'resque/scheduler'
6
- require 'resque_scheduler/plugin'
7
-
8
- module ResqueScheduler
9
- autoload :Cli, 'resque_scheduler/cli'
10
-
11
- #
12
- # Accepts a new schedule configuration of the form:
13
- #
14
- # {
15
- # "MakeTea" => {
16
- # "every" => "1m" },
17
- # "some_name" => {
18
- # "cron" => "5/* * * *",
19
- # "class" => "DoSomeWork",
20
- # "args" => "work on this string",
21
- # "description" => "this thing works it"s butter off" },
22
- # ...
23
- # }
24
- #
25
- # Hash keys can be anything and are used to describe and reference
26
- # the scheduled job. If the "class" argument is missing, the key
27
- # is used implicitly as "class" argument - in the "MakeTea" example,
28
- # "MakeTea" is used both as job name and resque worker class.
29
- #
30
- # Any jobs that were in the old schedule, but are not
31
- # present in the new schedule, will be removed.
32
- #
33
- # :cron can be any cron scheduling string
34
- #
35
- # :every can be used in lieu of :cron. see rufus-scheduler's 'every' usage
36
- # for valid syntax. If :cron is present it will take precedence over :every.
37
- #
38
- # :class must be a resque worker class. If it is missing, the job name (hash key)
39
- # will be used as :class.
40
- #
41
- # :args can be any yaml which will be converted to a ruby literal and
42
- # passed in a params. (optional)
43
- #
44
- # :rails_envs is the list of envs where the job gets loaded. Envs are
45
- # comma separated (optional)
46
- #
47
- # :description is just that, a description of the job (optional). If
48
- # params is an array, each element in the array is passed as a separate
49
- # param, otherwise params is passed in as the only parameter to perform.
50
- def schedule=(schedule_hash)
51
- # clean the schedules as it exists in redis
52
- clean_schedules
53
-
54
- schedule_hash = prepare_schedule(schedule_hash)
55
-
56
- # store all schedules in redis, so we can retrieve them back everywhere.
57
- schedule_hash.each do |name, job_spec|
58
- set_schedule(name, job_spec)
59
- end
60
-
61
- # ensure only return the successfully saved data!
62
- reload_schedule!
63
- end
64
-
65
- # Returns the schedule hash
66
- def schedule
67
- @schedule ||= get_schedules
68
- if @schedule.nil?
69
- return {}
70
- end
71
- @schedule
72
- end
73
-
74
- # reloads the schedule from redis
75
- def reload_schedule!
76
- @schedule = get_schedules
77
- end
78
-
79
- # gets the schedules as it exists in redis
80
- def get_schedules
81
- unless redis.exists(:schedules)
82
- return nil
83
- end
84
-
85
- redis.hgetall(:schedules).tap do |h|
86
- h.each do |name, config|
87
- h[name] = decode(config)
88
- end
89
- end
90
- end
91
-
92
- # clean the schedules as it exists in redis, useful for first setup?
93
- def clean_schedules
94
- if redis.exists(:schedules)
95
- redis.hkeys(:schedules).each do |key|
96
- remove_schedule(key) if !schedule_persisted?(key)
97
- end
98
- end
99
- @schedule = nil
100
- true
101
- end
102
-
103
- # Create or update a schedule with the provided name and configuration.
104
- #
105
- # Note: values for class and custom_job_class need to be strings,
106
- # not constants.
107
- #
108
- # Resque.set_schedule('some_job', {:class => 'SomeJob',
109
- # :every => '15mins',
110
- # :queue => 'high',
111
- # :args => '/tmp/poop'})
112
- def set_schedule(name, config)
113
- existing_config = get_schedule(name)
114
- persist = config.delete(:persist) || config.delete('persist')
115
- unless existing_config && existing_config == config
116
- redis.pipelined do
117
- redis.hset(:schedules, name, encode(config))
118
- redis.sadd(:schedules_changed, name)
119
- if persist
120
- redis.sadd(:persisted_schedules, name)
121
- end
122
- end
123
- end
124
- config
125
- end
126
-
127
- # retrive the schedule configuration for the given name
128
- def get_schedule(name)
129
- decode(redis.hget(:schedules, name))
130
- end
131
-
132
- def schedule_persisted?(name)
133
- redis.sismember(:persisted_schedules, name)
134
- end
135
-
136
- # remove a given schedule by name
137
- def remove_schedule(name)
138
- redis.pipelined do
139
- redis.hdel(:schedules, name)
140
- redis.srem(:persisted_schedules, name)
141
- redis.sadd(:schedules_changed, name)
142
- end
143
- end
144
-
145
- # This method is nearly identical to +enqueue+ only it also
146
- # takes a timestamp which will be used to schedule the job
147
- # for queueing. Until timestamp is in the past, the job will
148
- # sit in the schedule list.
149
- def enqueue_at(timestamp, klass, *args)
150
- validate(klass)
151
- enqueue_at_with_queue(queue_from_class(klass), timestamp, klass, *args)
152
- end
153
-
154
- # Identical to +enqueue_at+, except you can also specify
155
- # a queue in which the job will be placed after the
156
- # timestamp has passed. It respects Resque.inline option, by
157
- # creating the job right away instead of adding to the queue.
158
- def enqueue_at_with_queue(queue, timestamp, klass, *args)
159
- return false unless Plugin.run_before_schedule_hooks(klass, *args)
160
-
161
- if Resque.inline? || timestamp.to_i < Time.now.to_i
162
- # Just create the job and let resque perform it right away with inline.
163
- # If the class is a custom job class, call self#scheduled on it. This allows you to do things like
164
- # Resque.enqueue_at(timestamp, CustomJobClass, :opt1 => val1). Otherwise, pass off to Resque.
165
- if klass.respond_to?(:scheduled)
166
- klass.scheduled(queue, klass.to_s(), *args)
167
- else
168
- Resque::Job.create(queue, klass, *args)
169
- end
170
- else
171
- delayed_push(timestamp, job_to_hash_with_queue(queue, klass, args))
172
- end
173
-
174
- Plugin.run_after_schedule_hooks(klass, *args)
175
- end
176
-
177
- # Identical to enqueue_at but takes number_of_seconds_from_now
178
- # instead of a timestamp.
179
- def enqueue_in(number_of_seconds_from_now, klass, *args)
180
- enqueue_at(Time.now + number_of_seconds_from_now, klass, *args)
181
- end
182
-
183
- # Identical to +enqueue_in+, except you can also specify
184
- # a queue in which the job will be placed after the
185
- # number of seconds has passed.
186
- def enqueue_in_with_queue(queue, number_of_seconds_from_now, klass, *args)
187
- enqueue_at_with_queue(queue, Time.now + number_of_seconds_from_now, klass, *args)
188
- end
189
-
190
- # Used internally to stuff the item into the schedule sorted list.
191
- # +timestamp+ can be either in seconds or a datetime object
192
- # Insertion if O(log(n)).
193
- # Returns true if it's the first job to be scheduled at that time, else false
194
- def delayed_push(timestamp, item)
195
- # First add this item to the list for this timestamp
196
- redis.rpush("delayed:#{timestamp.to_i}", encode(item))
197
-
198
- # Store the timestamps at with this item occurs
199
- redis.sadd("timestamps:#{encode(item)}", "delayed:#{timestamp.to_i}")
200
-
201
- # Now, add this timestamp to the zsets. The score and the value are
202
- # the same since we'll be querying by timestamp, and we don't have
203
- # anything else to store.
204
- redis.zadd :delayed_queue_schedule, timestamp.to_i, timestamp.to_i
205
- end
206
-
207
- # Returns an array of timestamps based on start and count
208
- def delayed_queue_peek(start, count)
209
- Array(redis.zrange(:delayed_queue_schedule, start, start+count-1)).collect { |x| x.to_i }
210
- end
211
-
212
- # Returns the size of the delayed queue schedule
213
- def delayed_queue_schedule_size
214
- redis.zcard :delayed_queue_schedule
215
- end
216
-
217
- # Returns the number of jobs for a given timestamp in the delayed queue schedule
218
- def delayed_timestamp_size(timestamp)
219
- redis.llen("delayed:#{timestamp.to_i}").to_i
220
- end
221
-
222
- # Returns an array of delayed items for the given timestamp
223
- def delayed_timestamp_peek(timestamp, start, count)
224
- if 1 == count
225
- r = list_range "delayed:#{timestamp.to_i}", start, count
226
- r.nil? ? [] : [r]
227
- else
228
- list_range "delayed:#{timestamp.to_i}", start, count
229
- end
230
- end
231
-
232
- # Returns the next delayed queue timestamp
233
- # (don't call directly)
234
- def next_delayed_timestamp(at_time=nil)
235
- items = redis.zrangebyscore :delayed_queue_schedule, '-inf', (at_time || Time.now).to_i, :limit => [0, 1]
236
- timestamp = items.nil? ? nil : Array(items).first
237
- timestamp.to_i unless timestamp.nil?
238
- end
239
-
240
- # Returns the next item to be processed for a given timestamp, nil if
241
- # done. (don't call directly)
242
- # +timestamp+ can either be in seconds or a datetime
243
- def next_item_for_timestamp(timestamp)
244
- key = "delayed:#{timestamp.to_i}"
245
-
246
- encoded_item = redis.lpop(key)
247
- redis.srem("timestamps:#{encoded_item}", key)
248
- item = decode(encoded_item)
249
-
250
- # If the list is empty, remove it.
251
- clean_up_timestamp(key, timestamp)
252
- item
253
- end
254
-
255
- # Clears all jobs created with enqueue_at or enqueue_in
256
- def reset_delayed_queue
257
- Array(redis.zrange(:delayed_queue_schedule, 0, -1)).each do |item|
258
- key = "delayed:#{item}"
259
- items = redis.lrange(key, 0, -1)
260
- redis.pipelined do
261
- items.each { |ts_item| redis.del("timestamps:#{ts_item}") }
262
- end
263
- redis.del key
264
- end
265
-
266
- redis.del :delayed_queue_schedule
267
- end
268
-
269
- # Given an encoded item, remove it from the delayed_queue
270
- def remove_delayed(klass, *args)
271
- search = encode(job_to_hash(klass, args))
272
- timestamps = redis.smembers("timestamps:#{search}")
273
-
274
- replies = redis.pipelined do
275
- timestamps.each do |key|
276
- redis.lrem(key, 0, search)
277
- redis.srem("timestamps:#{search}", key)
278
- end
279
- end
280
-
281
- (replies.nil? || replies.empty?) ? 0 : replies.each_slice(2).collect { |slice| slice.first }.inject(:+)
282
- end
283
-
284
- # Given an encoded item, enqueue it now
285
- def enqueue_delayed(klass, *args)
286
- hash = job_to_hash(klass, args)
287
- remove_delayed(klass, *args).times { Resque::Scheduler.enqueue_from_config(hash) }
288
- end
289
-
290
- # Given a block, remove jobs that return true from a block
291
- #
292
- # This allows for removal of delayed jobs that have arguments matching certain criteria
293
- def remove_delayed_selection
294
- fail ArgumentError, "Please supply a block" unless block_given?
295
-
296
- destroyed = 0
297
- # There is no way to search Redis list entries for a partial match, so we query for all
298
- # delayed job tasks and do our matching after decoding the payload data
299
- jobs = Resque.redis.keys("delayed:*")
300
- jobs.each do |job|
301
- index = Resque.redis.llen(job) - 1
302
- while index >= 0
303
- payload = Resque.redis.lindex(job, index)
304
- decoded_payload = decode(payload)
305
- if yield(decoded_payload['args'])
306
- removed = redis.lrem job, 0, payload
307
- destroyed += removed
308
- index -= removed
309
- else
310
- index -= 1
311
- end
312
- end
313
- end
314
- destroyed
315
- end
316
-
317
- # Given a timestamp and job (klass + args) it removes all instances and
318
- # returns the count of jobs removed.
319
- #
320
- # O(N) where N is the number of jobs scheduled to fire at the given
321
- # timestamp
322
- def remove_delayed_job_from_timestamp(timestamp, klass, *args)
323
- key = "delayed:#{timestamp.to_i}"
324
- encoded_job = encode(job_to_hash(klass, args))
325
-
326
- redis.srem("timestamps:#{encoded_job}", key)
327
- count = redis.lrem(key, 0, encoded_job)
328
- clean_up_timestamp(key, timestamp)
329
-
330
- count
331
- end
332
-
333
- def count_all_scheduled_jobs
334
- total_jobs = 0
335
- Array(redis.zrange(:delayed_queue_schedule, 0, -1)).each do |timestamp|
336
- total_jobs += redis.llen("delayed:#{timestamp}").to_i
337
- end
338
- total_jobs
339
- end
340
-
341
- # Returns delayed jobs schedule timestamp for +klass+, +args+.
342
- def scheduled_at(klass, *args)
343
- search = encode(job_to_hash(klass, args))
344
- redis.smembers("timestamps:#{search}").collect do |key|
345
- key.tr('delayed:', '').to_i
346
- end
347
- end
348
-
349
- private
350
-
351
- def job_to_hash(klass, args)
352
- {:class => klass.to_s, :args => args, :queue => queue_from_class(klass)}
353
- end
354
-
355
- def job_to_hash_with_queue(queue, klass, args)
356
- {:class => klass.to_s, :args => args, :queue => queue}
357
- end
358
-
359
- def clean_up_timestamp(key, timestamp)
360
- # If the list is empty, remove it.
361
-
362
- # Use a watch here to ensure nobody adds jobs to this delayed
363
- # queue while we're removing it.
364
- redis.watch key
365
- if 0 == redis.llen(key).to_i
366
- redis.multi do
367
- redis.del key
368
- redis.zrem :delayed_queue_schedule, timestamp.to_i
369
- end
370
- else
371
- redis.unwatch
372
- end
373
- end
374
-
375
- def prepare_schedule(schedule_hash)
376
- prepared_hash = {}
377
- schedule_hash.each do |name, job_spec|
378
- job_spec = job_spec.dup
379
- job_spec['class'] = name unless job_spec.key?('class') || job_spec.key?(:class)
380
- prepared_hash[name] = job_spec
381
- end
382
- prepared_hash
383
- end
384
- end
385
-
386
- Resque.extend ResqueScheduler
@@ -1,14 +0,0 @@
1
- require 'redis'
2
- require 'resque'
3
-
4
- if ARGV.size != 1
5
- puts "migrate_to_timestamps_set.rb <redis-host:redis-port>"
6
- exit
7
- end
8
-
9
- Resque.redis = ARGV[0]
10
- redis = Resque.redis
11
- Array(redis.keys("delayed:*")).each do |key|
12
- jobs = redis.lrange(key, 0, -1)
13
- jobs.each {|job| redis.sadd("timestamps:#{job}", key)}
14
- end
@@ -1,2 +0,0 @@
1
- $LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'
2
- require 'resque_scheduler/tasks'
data/test/cli_test.rb DELETED
@@ -1,286 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- context 'Cli' do
4
- def new_cli(argv = [], env = {})
5
- ResqueScheduler::Cli.new(argv, env)
6
- end
7
-
8
- test 'does not require any positional arguments' do
9
- assert(!new_cli.nil?)
10
- end
11
-
12
- test 'initializes verbose from the env' do
13
- cli = new_cli([], { 'VERBOSE' => 'foo' })
14
- assert_equal('foo', cli.send(:options)[:verbose])
15
- end
16
-
17
- test 'defaults to non-verbose' do
18
- assert_equal(false, !!new_cli.send(:options)[:verbose])
19
- end
20
-
21
- test 'accepts verbose via -v' do
22
- cli = new_cli(%w(-v))
23
- cli.parse_options
24
- assert_equal(true, cli.send(:options)[:verbose])
25
- end
26
-
27
- test 'accepts verbose via --verbose' do
28
- cli = new_cli(%w(--verbose))
29
- cli.parse_options
30
- assert_equal(true, cli.send(:options)[:verbose])
31
- end
32
-
33
- test 'initializes background from the env' do
34
- cli = new_cli([], { 'BACKGROUND' => '1' })
35
- assert_equal('1', cli.send(:options)[:background])
36
- end
37
-
38
- test 'defaults to background=false' do
39
- assert_equal(false, !!new_cli.send(:options)[:background])
40
- end
41
-
42
- test 'accepts background via -B' do
43
- cli = new_cli(%w(-B))
44
- cli.parse_options
45
- assert_equal(true, cli.send(:options)[:background])
46
- end
47
-
48
- test 'accepts background via --background' do
49
- cli = new_cli(%w(--background))
50
- cli.parse_options
51
- assert_equal(true, cli.send(:options)[:background])
52
- end
53
-
54
- test 'daemonizes when background is true' do
55
- Process.expects(:daemon)
56
- cli = new_cli(%w(--background))
57
- cli.pre_run
58
- end
59
-
60
- test 'reconnects redis when background is true' do
61
- Process.stubs(:daemon)
62
- mock_redis_client = mock('redis_client')
63
- mock_redis = mock('redis')
64
- mock_redis.expects(:client).returns(mock_redis_client)
65
- mock_redis_client.expects(:reconnect)
66
- Resque.expects(:redis).returns(mock_redis)
67
- cli = new_cli(%w(--background))
68
- cli.pre_run
69
- end
70
-
71
- test 'aborts when background is given and Process does not support daemon' do
72
- Process.stubs(:daemon)
73
- Process.expects(:respond_to?).with('daemon').returns(false)
74
- cli = new_cli(%w(--background))
75
- cli.expects(:abort)
76
- cli.pre_run
77
- end
78
-
79
- test 'initializes pidfile from the env' do
80
- cli = new_cli([], { 'PIDFILE' => 'bar' })
81
- assert_equal('bar', cli.send(:options)[:pidfile])
82
- end
83
-
84
- test 'defaults to nil pidfile' do
85
- assert_equal(nil, new_cli.send(:options)[:pidfile])
86
- end
87
-
88
- test 'accepts pidfile via -P' do
89
- cli = new_cli(%w(-P foo))
90
- cli.parse_options
91
- assert_equal('foo', cli.send(:options)[:pidfile])
92
- end
93
-
94
- test 'accepts pidfile via --pidfile' do
95
- cli = new_cli(%w(--pidfile foo))
96
- cli.parse_options
97
- assert_equal('foo', cli.send(:options)[:pidfile])
98
- end
99
-
100
- test 'writes pid to pidfile when given' do
101
- mock_pidfile = mock('pidfile')
102
- mock_pidfile.expects(:puts)
103
- File.expects(:open).with('derp.pid', 'w').yields(mock_pidfile)
104
- cli = new_cli(%w(--pidfile derp.pid))
105
- cli.pre_run
106
- end
107
-
108
- test 'initializes dynamic from the env' do
109
- cli = new_cli([], { 'DYNAMIC_SCHEDULE' => '1' })
110
- assert_equal('flurb', cli.send(:options)[:dynamic])
111
- end
112
-
113
- test 'defaults to nil dynamic' do
114
- assert_equal(nil, new_cli.send(:options)[:dynamic])
115
- end
116
-
117
- test 'accepts dynamic via -D' do
118
- cli = new_cli(%w(-D))
119
- cli.parse_options
120
- assert_equal(true, cli.send(:options)[:dynamic])
121
- end
122
-
123
- test 'accepts dynamic via --dynamic-schedule' do
124
- cli = new_cli(%w(--dynamic-schedule))
125
- cli.parse_options
126
- assert_equal(true, cli.send(:options)[:dynamic])
127
- end
128
-
129
- test 'initializes env from the env' do
130
- cli = new_cli([], { 'RAILS_ENV' => 'flurb' })
131
- assert_equal('flurb', cli.send(:options)[:env])
132
- end
133
-
134
- test 'defaults to nil env' do
135
- assert_equal(nil, new_cli.send(:options)[:env])
136
- end
137
-
138
- test 'accepts env via -E' do
139
- cli = new_cli(%w(-E bork))
140
- cli.parse_options
141
- assert_equal('bork', cli.send(:options)[:env])
142
- end
143
-
144
- test 'accepts env via --environment' do
145
- cli = new_cli(%w(--environment hork))
146
- cli.parse_options
147
- assert_equal('hork', cli.send(:options)[:env])
148
- end
149
-
150
- test 'initializes initializer_path from the env' do
151
- cli = new_cli([], { 'INITIALIZER_PATH' => 'herp.rb' })
152
- assert_equal('herp.rb', cli.send(:options)[:initializer_path])
153
- end
154
-
155
- test 'defaults to nil initializer_path' do
156
- assert_equal(nil, new_cli.send(:options)[:initializer_path])
157
- end
158
-
159
- test 'accepts initializer_path via -I' do
160
- cli = new_cli(%w(-I hambone.rb))
161
- cli.parse_options
162
- assert_equal('hambone.rb', cli.send(:options)[:initializer_path])
163
- end
164
-
165
- test 'accepts initializer_path via --initalizer-path' do
166
- cli = new_cli(%w(--initializer-path cookies.rb))
167
- cli.parse_options
168
- assert_equal('cookies.rb', cli.send(:options)[:initializer_path])
169
- end
170
-
171
- test 'loads given initilalizer_path' do
172
- cli = new_cli(%w(--initializer-path fuzzbert.rb))
173
- cli.expects(:load).with('fuzzbert.rb')
174
- cli.pre_run
175
- end
176
-
177
- test 'initializes mute/quiet from the env' do
178
- cli = new_cli([], { 'QUIET' => '1' })
179
- assert_equal('1', cli.send(:options)[:mute])
180
- end
181
-
182
- test 'defaults to unmuted' do
183
- assert_equal(false, !!new_cli.send(:options)[:mute])
184
- end
185
-
186
- test 'accepts mute/quiet via -q' do
187
- cli = new_cli(%w(-q))
188
- cli.parse_options
189
- assert_equal(true, cli.send(:options)[:mute])
190
- end
191
-
192
- test 'accepts mute via --quiet' do
193
- cli = new_cli(%w(--quiet))
194
- cli.parse_options
195
- assert_equal(true, cli.send(:options)[:mute])
196
- end
197
-
198
- test 'initializes logfile from the env' do
199
- cli = new_cli([], { 'LOGFILE' => 'derp.log' })
200
- assert_equal('derp.log', cli.send(:options)[:logfile])
201
- end
202
-
203
- test 'defaults to nil logfile' do
204
- assert_equal(nil, new_cli.send(:options)[:logfile])
205
- end
206
-
207
- test 'accepts logfile via -l' do
208
- cli = new_cli(%w(-l hurm.out))
209
- cli.parse_options
210
- assert_equal('hurm.out', cli.send(:options)[:logfile])
211
- end
212
-
213
- test 'accepts logfile via --logfile' do
214
- cli = new_cli(%w(--logfile flam.log))
215
- cli.parse_options
216
- assert_equal('flam.log', cli.send(:options)[:logfile])
217
- end
218
-
219
- test 'initializes logformat from the env' do
220
- cli = new_cli([], { 'LOGFORMAT' => 'fancy' })
221
- assert_equal('fancy', cli.send(:options)[:logformat])
222
- end
223
-
224
- test 'defaults to nil logformat' do
225
- assert_equal(nil, new_cli.send(:options)[:logformat])
226
- end
227
-
228
- test 'accepts logformat via -F' do
229
- cli = new_cli(%w(-F silly))
230
- cli.parse_options
231
- assert_equal('silly', cli.send(:options)[:logformat])
232
- end
233
-
234
- test 'accepts logformat via --logformat' do
235
- cli = new_cli(%w(--logformat flimsy))
236
- cli.parse_options
237
- assert_equal('flimsy', cli.send(:options)[:logformat])
238
- end
239
-
240
- test 'initializes dynamic from the env' do
241
- cli = new_cli([], { 'DYNAMIC_SCHEDULE' => '1' })
242
- assert_equal('1', cli.send(:options)[:dynamic])
243
- end
244
-
245
- test 'defaults to dynamic=false' do
246
- assert_equal(false, !!new_cli.send(:options)[:dynamic])
247
- end
248
-
249
- test 'accepts dynamic via -D' do
250
- cli = new_cli(%w(-D))
251
- cli.parse_options
252
- assert_equal(true, cli.send(:options)[:dynamic])
253
- end
254
-
255
- test 'accepts dynamic via --dynamic-schedule' do
256
- cli = new_cli(%w(--dynamic-schedule))
257
- cli.parse_options
258
- assert_equal(true, cli.send(:options)[:dynamic])
259
- end
260
-
261
- test 'initializes app_name from the env' do
262
- cli = new_cli([], { 'APP_NAME' => 'sprocket' })
263
- assert_equal('sprocket', cli.send(:options)[:app_name])
264
- end
265
-
266
- test 'defaults to nil app_name' do
267
- assert_equal(nil, new_cli.send(:options)[:app_name])
268
- end
269
-
270
- test 'accepts app_name via -n' do
271
- cli = new_cli(%w(-n hambone))
272
- cli.parse_options
273
- assert_equal('hambone', cli.send(:options)[:app_name])
274
- end
275
-
276
- test 'accepts app_name via --app-name' do
277
- cli = new_cli(%w(--app-name flimsy))
278
- cli.parse_options
279
- assert_equal('flimsy', cli.send(:options)[:app_name])
280
- end
281
-
282
- test 'runs Resque::Scheduler' do
283
- Resque::Scheduler.expects(:run)
284
- ResqueScheduler::Cli.run!([], {})
285
- end
286
- end