Module: Scheduler::Schedulable

Included in:
Examples::SchedulableModel
Defined in:
lib/scheduler/schedulable.rb

Defined Under Namespace

Classes: Delayed

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

Class Method Details

.included(base) ⇒ Object



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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/scheduler/schedulable.rb', line 35

def self.included(base)
  base.class_eval do
    include Mongoid::Document

    field :executable_class,        type: String
    field :args,                    type: Array,      default: []
    field :run_at,                  type: DateTime,   default: Time.now
    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) }

    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 schedules the job.
      #
      # @param [String] executable_class the class of the job to run.
      # @param [Array] *job_args job arguments
      #
      # @return [Object] the created job.
      def schedule(executable_class, *job_args)
        self.create(executable_class: executable_class, args: job_args).schedule
      end

      ##
      # Creates an instance of this class and schedules the job after the amount of given time.
      #
      # @param [Integer] time the amount of time to wait.
      #
      # @return [Object] the created job.
      def in(time)
        Scheduler::Schedulable::Delayed.new(self, time)
      end

      ##
      # Creates an instance of this class and performs the job.
      #
      # @param [String] executable_class the class of the job to run.
      # @param [Array] *job_args job arguments
      #
      # @return [Object] the created job.
      def perform(executable_class, *job_args)
        self.create(executable_class: executable_class, args: job_args).perform
      end

    end

    ##
    # Gets executor job class.
    #
    # @return [Class] the executor job class.
    def executable_class
      Object.const_get self[:executable_class]
    end

    ##
    # Resets the job data.
    #
    # @return [Object] itself.
    def reset
      self.scheduled_at = Time.current
      self.logs = []
      self.progress = 0.0
      self.unset(:error)
      self.unset(:backtrace)
      self.unset(:completed_at)
      self.unset(:executed_at)
      self.save

      self
    end

    ##
    # Schedules the job.
    #
    # @return [Object] itself.
    def schedule
      self.status = :queued
      self.reset

      yield self if block_given?
      self.perform if Scheduler.perform_jobs_in_test_or_development?

      self
    end

    ##
    # Performs the job.
    #
    # @param [Integer] pid the executing pid.
    # @param [Boolean] reset whether to reset job data before performing.
    #
    # @return [Object] the instanced executable_class.
    def perform(pid = nil, reset = false)
      self.reset if reset

      job = self.executable_class.new(self)
      raise Scheduler::Error.new "#{self.executable_class} does not implement method 'call'. Please make "\
        "sure to implement it before performing the job." unless job.respond_to? :call
      self.status!(:running)
      self.update(pid: pid) if pid.present?
      begin
        catch :error do
          if self.args.last.is_a? Hash
            job.call(*self.args.first(self.args.size - 1), **self.args.last.symbolize_keys)
          else
            job.call(*self.args)
          end
        end
      rescue StandardError => error
        self.status!(:error)
        self.log(:error, error.message)
        self.log(:error, error.backtrace.join("\n"))
      end
      self.completed_at = Time.current
      if self.status == :running
        self.progress!(100)
        self.status!(:completed)
      end

      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

    ##
    # Immediately stops job execution and logs the error.
    #
    # @param [String] message the error message.
    def error!(message)
      self.log :error, message
      self.status! :error
      throw :error
    end

    ##
    # Registers a log message with the given level.
    def log(level, message)
      raise Scheduler::Error.new "The given log level '#{level}' is not valid. "\
        "Valid log levels are: #{LOG_LEVELS.join(', ')}" unless level.in? LOG_LEVELS
      Scheduler.logger.send level, Rainbow("[#{self.class}:#{self.id}] #{message}").send(self.class.log_color level)
      self.update(logs: logs.push([level, message]))
    end
  end
end