rocketjob 3.5.1 → 3.5.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cb87d22625bed26c75bd82fd284f95bfd73d3e70c1aac45ef8ef9a49c6c94fae
4
- data.tar.gz: 51878e58e9a051dcf3f284c1a33e4eb7e3e4e0f87c19f607459e2d1107127f87
3
+ metadata.gz: bd3505de7a318efb53880ef4c0f506a970d1ee30277f531bef7e2a9f257cf500
4
+ data.tar.gz: '085e0371c60662b51ba53c4e5e6e1b2e016cdc2e37de1b51a437e5477f907a22'
5
5
  SHA512:
6
- metadata.gz: 96adc5a3bcd6ad53d86a8bd01b78546318690329de4c0230a7288d233b24a7b0eac47668b880cdd6c724dc861e2c27830dd34452d938e5da4e205b5982615b5c
7
- data.tar.gz: d0c947bef27a6b1bc238ff8f04133709cf8339d2be34c951ae9c3a5c3f41cc4110584d78aee2ce44de988372c571942b1d40423f916b4bd57be2d3e809a9c96d
6
+ metadata.gz: a9a9441957a5405ed1d3d98bbe5bf0a45febbbdf8283c09de967f19abd1e5a7470cc871d66b804f8569ad90b48732fcd4d649e3d0c282b60be2b000bdd60adc8
7
+ data.tar.gz: d47b9c8a4eba0fd88798b76926da8c4073b0c26c6743ae3b54883535f278ea3317d6fb7901d57caf8506f710b5fdb7151b9acae65e7e2427bf2607a0c926231a
@@ -74,23 +74,16 @@ module RocketJob
74
74
  include RocketJob::Plugins::Cron
75
75
  include RocketJob::Plugins::Retry
76
76
 
77
- self.priority = 90
78
- self.description = 'Generalized Job'
77
+ self.description = 'On Demand Job'
79
78
  self.destroy_on_complete = false
80
79
  self.retry_limit = 0
81
80
 
82
81
  # Be sure to store key names only as Strings, not Symbols
83
- field :data, type: Hash, default: {}
84
- field :code, type: String
82
+ field :data, type: Hash, default: {}, copy_on_restart: true
83
+ field :code, type: String, copy_on_restart: true
85
84
 
86
85
  validates :code, presence: true
87
- validates_each :code do |job, attr, _value|
88
- begin
89
- job.send(:load_code)
90
- rescue Exception => exc
91
- job.errors.add(attr, "Failed to parse :code, #{exc.inspect}")
92
- end
93
- end
86
+ validate :validate_code
94
87
 
95
88
  before_perform :load_code
96
89
 
@@ -99,6 +92,12 @@ module RocketJob
99
92
  def load_code
100
93
  instance_eval("def perform\n#{code}\nend", __FILE__, __LINE__)
101
94
  end
95
+
96
+ def validate_code
97
+ load_code
98
+ rescue Exception => exc
99
+ errors.add(:code, "Failed to parse :code, #{exc.inspect}")
100
+ end
102
101
  end
103
102
  end
104
103
  end
@@ -63,6 +63,8 @@ module RocketJob
63
63
  if original_file_name && defined?(IOStreams)
64
64
  streams = IOStreams.streams_for_file_name(original_file_name)
65
65
  job.upload(upload_file_name, streams: streams)
66
+ # job.upload sets the archived filename, we want it to be the original file name.
67
+ job.upload_file_name = original_file_name
66
68
  else
67
69
  job.upload(upload_file_name)
68
70
  end
@@ -2,7 +2,13 @@ require 'active_support/concern'
2
2
 
3
3
  module RocketJob
4
4
  module Plugins
5
- # Schedule jobs to run at set intervals.
5
+ # Allow jobs to run on a predefined schedule, much like a crontab.
6
+ #
7
+ # Notes:
8
+ # - No single point of failure since their is no centralized scheduler.
9
+ # - `cron_schedule` can be edited at any time via the web interface.
10
+ # - A scheduled job can be run at any time by calling `#run_now!` or
11
+ # by clicking `Run Now` in the web interface.
6
12
  module Cron
7
13
  extend ActiveSupport::Concern
8
14
 
@@ -11,15 +17,8 @@ module RocketJob
11
17
 
12
18
  field :cron_schedule, type: String, class_attribute: true, user_editable: true, copy_on_restart: true
13
19
 
14
- before_save :rocket_job_set_run_at
15
-
16
- validates_each :cron_schedule do |record, attr, value|
17
- begin
18
- RocketJob::Plugins::Rufus::CronLine.new(value) if value
19
- rescue ArgumentError => exc
20
- record.errors.add(attr, exc.message)
21
- end
22
- end
20
+ validate :rocket_job_cron_valid
21
+ before_save :rocket_job_cron_set_run_at
23
22
 
24
23
  private
25
24
 
@@ -41,25 +40,6 @@ module RocketJob
41
40
  end
42
41
  end
43
42
 
44
- # Returns [Time] at which this job was intended to run at.
45
- #
46
- # Takes into account any delays that could occur.
47
- # Recommended to use this Time instead of Time.now in the `#perform` since the job could run outside its
48
- # intended window. Especially if a failed job is only retried quite sometime later.
49
- #
50
- # Notes:
51
- # * When `cron_schedule` is set, this would be the `run_at` time, otherwise it is the `created_at` time
52
- # since that would be the intended time for which this job is running.
53
- def scheduled_at
54
- run_at || created_at
55
- end
56
-
57
- # Make this job run now, regardless of the cron schedule.
58
- # Upon completion the job will automatically reschedule itself.
59
- def run_now!
60
- update_attributes(run_at: nil) if cron_schedule
61
- end
62
-
63
43
  # Returns [Time] the next time this job will be scheduled to run at.
64
44
  #
65
45
  # Parameters
@@ -72,10 +52,17 @@ module RocketJob
72
52
 
73
53
  private
74
54
 
75
- def rocket_job_set_run_at
55
+ def rocket_job_cron_set_run_at
76
56
  return unless cron_schedule
77
57
  self.run_at = rocket_job_cron_next_time if cron_schedule_changed? && !run_at_changed?
78
58
  end
59
+
60
+ def rocket_job_cron_valid
61
+ return unless cron_schedule
62
+ RocketJob::Plugins::Rufus::CronLine.new(cron_schedule)
63
+ rescue ArgumentError => exc
64
+ errors.add(:cron_schedule, exc.message)
65
+ end
79
66
  end
80
67
  end
81
68
  end
@@ -259,6 +259,20 @@ module RocketJob
259
259
  running? && worker_name.present? ? [worker_name] : []
260
260
  end
261
261
 
262
+ # Clear `run_at` so that this job will run now.
263
+ def run_now!
264
+ update_attributes(run_at: nil) if run_at
265
+ end
266
+
267
+ # Returns [Time] at which this job was intended to run at.
268
+ #
269
+ # Takes into account any delays that could occur.
270
+ # Recommended to use this Time instead of Time.now in the `#perform` since the job could run outside its
271
+ # intended window. Especially if a failed job is only retried quite sometime later.
272
+ def scheduled_at
273
+ run_at || created_at
274
+ end
275
+
262
276
  # Returns [Hash] status of this job
263
277
  def as_json
264
278
  attrs = serializable_hash(methods: %i[seconds duration])
@@ -114,7 +114,7 @@ module RocketJob
114
114
  end
115
115
  count += 1
116
116
  end
117
- logger.warn("New job instance not started: #{job.errors.messages.inspect}")
117
+ logger.error("New job instance not started: #{job.errors.messages.inspect}")
118
118
  false
119
119
  end
120
120
  end
@@ -2,23 +2,28 @@ require 'active_support/concern'
2
2
 
3
3
  module RocketJob
4
4
  module Plugins
5
- # Prevent more than one instance of this job class from running at a time
5
+ # Prevent this job from being saved if another is running, queued, or paused.
6
6
  module Singleton
7
7
  extend ActiveSupport::Concern
8
8
 
9
9
  included do
10
- # Validation prevents a new job from being saved while one is already running
11
- validates_each :state do |record, attr, _value|
12
- if (record.running? || record.queued? || record.paused?) && record.rocket_job_singleton_active?
13
- record.errors.add(attr, "Another instance of #{record.class.name} is already queued or running")
14
- end
15
- end
10
+ validate :rocket_job_singleton_check
11
+ end
12
+
13
+ # Returns [true|false] whether another instance of this job is already active
14
+ def rocket_job_singleton_active?
15
+ self.class.where(:state.in => %i[running queued], :id.ne => id).exists?
16
+ end
16
17
 
17
- # Returns [true|false] whether another instance of this job is already active
18
- def rocket_job_singleton_active?
19
- self.class.where(:state.in => %i[running queued], :id.ne => id).exists?
20
- end
18
+ private
19
+
20
+ # Validation prevents a new job from being saved while one is already running
21
+ def rocket_job_singleton_check
22
+ return unless (running? || queued? || paused?) && rocket_job_singleton_active?
23
+
24
+ errors.add(:state, "Another instance of #{self.class.name} is already running, queued, or paused")
21
25
  end
26
+
22
27
  end
23
28
  end
24
29
  end
@@ -1,3 +1,3 @@
1
1
  module RocketJob
2
- VERSION = '3.5.1'.freeze
2
+ VERSION = '3.5.2'.freeze
3
3
  end
@@ -88,10 +88,18 @@ class UploadFileJobTest < Minitest::Test
88
88
 
89
89
  it 'calls upload with original_file_name' do
90
90
  job.job_class_name = BatchTestJob.name
91
- job.original_file_name = 'file.zip'
92
91
  job.perform_now
93
92
  assert created_job = UploadFileJobTest::BatchTestJob.first
94
93
  assert_equal __FILE__, created_job.upload_file_name
94
+ assert_nil created_job.saved_streams
95
+ end
96
+
97
+ it 'job retains the original_file_name when present' do
98
+ job.job_class_name = BatchTestJob.name
99
+ job.original_file_name = 'file.zip'
100
+ job.perform_now
101
+ assert created_job = UploadFileJobTest::BatchTestJob.first
102
+ assert_equal 'file.zip', created_job.upload_file_name
95
103
  assert_equal %i[file zip], created_job.saved_streams
96
104
  end
97
105
  end
@@ -75,7 +75,7 @@ module Plugins
75
75
  @job.start!
76
76
  job2 = SingletonJob.new
77
77
  refute job2.valid?
78
- assert_equal ['Another instance of Plugins::SingletonTest::SingletonJob is already queued or running'], job2.errors.messages[:state]
78
+ assert_equal ['Another instance of Plugins::SingletonTest::SingletonJob is already running, queued, or paused'], job2.errors.messages[:state]
79
79
  end
80
80
 
81
81
  it 'passes if another job is active, but this job is not' do
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rocketjob
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.5.1
4
+ version: 3.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Reid Morrison
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-10 00:00:00.000000000 Z
11
+ date: 2018-05-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aasm