Sha256: f5dd4605f00e5080cf472fb08b6104f51204d36dab20b3aef1b9beeef0d509c1

Contents?: true

Size: 1.48 KB

Versions: 13

Compression:

Stored size: 1.48 KB

Contents

require 'active_support/concern'

module RocketJob
  class Server
    # State machine for sliced jobs
    module StateMachine
      extend ActiveSupport::Concern

      included do
        # States
        #   :starting -> :running -> :paused
        #                         -> :stopping
        aasm column: :state, whiny_persistence: true do
          state :starting, initial: true
          state :running
          state :paused
          state :stopping

          event :started do
            transitions from: :starting, to: :running
            before do
              self.started_at = Time.now
              build_heartbeat(updated_at: Time.now, workers: 0)
            end
          end

          event :pause do
            transitions from: :running, to: :paused
          end

          event :resume do
            transitions from: :paused, to: :running
          end

          event :stop do
            transitions from: :running, to: :stopping
            transitions from: :paused, to: :stopping
            transitions from: :starting, to: :stopping
          end
        end

        # Stop all running, paused, or starting servers
        def self.stop_all
          where(:state.in => %i[running paused starting]).each(&:stop!)
        end

        # Pause all running servers
        def self.pause_all
          running.each(&:pause!)
        end

        # Resume all paused servers
        def self.resume_all
          paused.each(&:resume!)
        end
      end

    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
rocketjob-5.1.1 lib/rocket_job/server/state_machine.rb
rocketjob-5.1.0 lib/rocket_job/server/state_machine.rb
rocketjob-5.0.0 lib/rocket_job/server/state_machine.rb
rocketjob-5.0.0.rc1 lib/rocket_job/server/state_machine.rb
rocketjob-5.0.0.beta4 lib/rocket_job/server/state_machine.rb
rocketjob-5.0.0.beta3 lib/rocket_job/server/state_machine.rb
rocketjob-5.0.0.beta2 lib/rocket_job/server/state_machine.rb
rocketjob-5.0.0.beta lib/rocket_job/server/state_machine.rb
rocketjob-4.3.0.beta2 lib/rocket_job/server/state_machine.rb
rocketjob-4.3.0.beta lib/rocket_job/server/state_machine.rb
rocketjob-4.2.0 lib/rocket_job/server/state_machine.rb
rocketjob-4.1.1 lib/rocket_job/server/state_machine.rb
rocketjob-4.1.0 lib/rocket_job/server/state_machine.rb