Module: Scheduler::Schedulable
- Included in:
- ExampleSchedulableModel
- Defined in:
- app/models/scheduler/schedulable.rb
Constant Summary collapse
- STATUSES =
Possible schedulable statuses.
[ :queued, :running, :completed, :warning, :error, :locked ]
- LOG_LEVELS =
Possible log levels.
[ :debug, :info, :warn, :error ]
Class Method Summary collapse
Instance Method Summary collapse
-
#broadcast ⇒ nil
Broadcasts a job updating event.
-
#job_class ⇒ Class
Gets ActiveJob’s job class.
-
#log(level, message) ⇒ Object
Registers a log message with the given level.
-
#perform_later ⇒ Object
Performs job when queue is available.
-
#perform_now ⇒ Object
Performs job when queue is available.
-
#progress!(amount) ⇒ Object
Immediately increases progress to the given amount.
-
#progress_by!(amount) ⇒ Object
Immediately increases progress by the given amount.
-
#schedule ⇒ Object
Schedules the job.
-
#status!(status) ⇒ nil
Immediately update the status to the given one.
Class Method Details
.included(base) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'app/models/scheduler/schedulable.rb', line 12 def self.included(base) base.class_eval do include Mongoid::Document field :class_name, type: String field :args, type: Array, default: [] field :scheduled_at, type: DateTime field :executed_at, type: DateTime field :completed_at, type: DateTime field :pid, type: Integer field :status, type: Symbol, default: :queued field :logs, type: Array, default: [] field :progress, type: Float, default: 0.0 field :error, type: String field :backtrace, type: String scope :queued, -> { where(status: :queued) } scope :running, -> { where(status: :running) } scope :completed, -> { where(status: :completed) } scope :to_check, -> { where(status: :warning) } scope :in_error, -> { where(status: :error) } scope :locked, -> { where(status: :locked) } validates_presence_of :class_name after_save :broadcast class << self ## # Returns possible statuses. # # @return [Array<Symbol>] possible statuses. def statuses Scheduler::Schedulable::STATUSES end ## # Returns possible log levels. # # @return [Array<Symbol>] possible log levels. def log_levels Scheduler::Schedulable::LOG_LEVELS end ## # Returns the corresponding log color if this level. # # @param [Symbol] level log level. # # @return [Symbol] the color. def log_color(level) case level when :debug then :green when :info then :cyan when :warn then :yellow when :error then :red end end ## # Creates an instance of this class and calls :perform_later. # # @param [String] job_class the class of the job to run. # @param [Array] *job_args job arguments # # @return [Object] the created job. def perform_later(job_class, *job_args) self.create(class_name: job_class, args: job_args).perform_later end ## # Creates an instance of this class and calls :perform_now. # # @param [String] job_class the class of the job to run. # @param [Array] *job_args job arguments # # @return [Object] the created job. def perform_now(job_class, *job_args) self.create(class_name: job_class, args: job_args).perform_now end end end ## # Gets ActiveJob's job class. # # @return [Class] the ActiveJob's job class. def job_class self.class_name.constantize end ## # Schedules the job. # # @return [Object] itself. def schedule self.scheduled_at = Time.current self.status = :queued self.logs = [] self.progress = 0.0 self.unset(:error) self.unset(:backtrace) self.unset(:completed_at) self.unset(:executed_at) self.save if block_given? yield self else self end end ## # Performs job when queue is available. # On test or development env, the job is performed by ActiveJob queue, # if configured on the Scheduler configuration. # On production env, the job is performed only with a Scheduler::MainProcess. # # @return [Object] the job class. def perform_later self.schedule if Rails.env.development? or Rails.env.test? if Scheduler.configuration.perform_jobs_in_test_or_development job_class.set(wait: 5.seconds).perform_later(self.class.name, self.id.to_s, *self.args) end end self end ## # Performs job when queue is available. # On test or development env, the job is performed with ActiveJob. # On production env, the job is performed with Scheduler. # # @return [Object] the job class. def perform_now self.schedule job_class.perform_now(self.class.name, self.id.to_s, *self.args) self end ## # Immediately update the status to the given one. # # @param [Symbol] status the status to update. # # @return [nil] def status!(status) self.update(status: status) end ## # Immediately increases progress to the given amount. # # @param [Float] amount the given progress amount. def progress!(amount) self.update(progress: amount.to_f) if amount.numeric? end ## # Immediately increases progress by the given amount. # # @param [Float] amount the given progress amount. def progress_by!(amount) self.update(progress: progress + amount.to_f) if amount.numeric? end ## # Registers a log message with the given level. def log(level, ) raise ArgumentError.new("The given log level '#{level}' is not valid. "\ "Valid log levels are: #{LOG_LEVELS.join(', ')}") unless level.in? LOG_LEVELS Scheduler.configuration.logger.send level, "[#{self.class}:#{self.id}] #{}".send(self.class.log_color level) self.update(logs: logs.push([level, ])) end ## # Broadcasts a job updating event. # # @return [nil] def broadcast { status: status, logs: logs } end end |
Instance Method Details
#broadcast ⇒ nil
Broadcasts a job updating event.
195 196 197 |
# File 'app/models/scheduler/schedulable.rb', line 195 def broadcast { status: status, logs: logs } end |
#job_class ⇒ Class
Gets ActiveJob’s job class.
103 104 105 |
# File 'app/models/scheduler/schedulable.rb', line 103 def job_class self.class_name.constantize end |
#log(level, message) ⇒ Object
Registers a log message with the given level.
183 184 185 186 187 188 189 |
# File 'app/models/scheduler/schedulable.rb', line 183 def log(level, ) raise ArgumentError.new("The given log level '#{level}' is not valid. "\ "Valid log levels are: #{LOG_LEVELS.join(', ')}") unless level.in? LOG_LEVELS Scheduler.configuration.logger.send level, "[#{self.class}:#{self.id}] #{}".send(self.class.log_color level) self.update(logs: logs.push([level, ])) end |
#perform_later ⇒ Object
Performs job when queue is available. On test or development env, the job is performed by ActiveJob queue, if configured on the Scheduler configuration. On production env, the job is performed only with a Scheduler::MainProcess.
133 134 135 136 137 138 139 140 141 |
# File 'app/models/scheduler/schedulable.rb', line 133 def perform_later self.schedule if Rails.env.development? or Rails.env.test? if Scheduler.configuration.perform_jobs_in_test_or_development job_class.set(wait: 5.seconds).perform_later(self.class.name, self.id.to_s, *self.args) end end self end |
#perform_now ⇒ Object
Performs job when queue is available. On test or development env, the job is performed with ActiveJob. On production env, the job is performed with Scheduler.
149 150 151 152 153 |
# File 'app/models/scheduler/schedulable.rb', line 149 def perform_now self.schedule job_class.perform_now(self.class.name, self.id.to_s, *self.args) self end |
#progress!(amount) ⇒ Object
Immediately increases progress to the given amount.
169 170 171 |
# File 'app/models/scheduler/schedulable.rb', line 169 def progress!(amount) self.update(progress: amount.to_f) if amount.numeric? end |
#progress_by!(amount) ⇒ Object
Immediately increases progress by the given amount.
177 178 179 |
# File 'app/models/scheduler/schedulable.rb', line 177 def progress_by!(amount) self.update(progress: progress + amount.to_f) if amount.numeric? end |
#schedule ⇒ Object
Schedules the job.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'app/models/scheduler/schedulable.rb', line 111 def schedule self.scheduled_at = Time.current self.status = :queued self.logs = [] self.progress = 0.0 self.unset(:error) self.unset(:backtrace) self.unset(:completed_at) self.unset(:executed_at) self.save if block_given? yield self else self end end |
#status!(status) ⇒ nil
Immediately update the status to the given one.
161 162 163 |
# File 'app/models/scheduler/schedulable.rb', line 161 def status!(status) self.update(status: status) end |