Sha256: be604d7f2771cd9175ec84decc0200316b5dc9601fd945b949188425ae4ec765

Contents?: true

Size: 1.31 KB

Versions: 16

Compression:

Stored size: 1.31 KB

Contents

module CronoTrigger
  module Models
    class Signal < ActiveRecord::Base
      self.table_name = "crono_trigger_signals"

      IGNORE_THRESHOLD = 300

      enum signal: {TERM: "TERM", USR1: "USR1", CONT: "CONT", TSTP: "TSTP"}

      scope :sent_to_me, proc {
        raise "Must set worker_id" unless CronoTrigger.config.worker_id

        where(arel_table[:sent_at].gteq(Time.current - IGNORE_THRESHOLD))
          .where(worker_id: CronoTrigger.config.worker_id)
          .where(received_at: nil)
          .order(:sent_at)
      }

      class << self
        def send_signal(signal, worker_id)
          create!(signal: signal, worker_id: worker_id, sent_at: Time.current)
        end

        def send_term(worker_id)
          send_signal("TERM", worker_id)
        end

        def send_usr1(worker_id)
          send_signal("USR1", worker_id)
        end

        def send_cont(worker_id)
          send_signal("CONT", worker_id)
        end

        def send_tstp(worker_id)
          send_signal("TSTP", worker_id)
        end
      end

      def kill_me(to_supervisor: true)
        if update(received_at: Time.current)
          if to_supervisor && Process.ppid != 1
            Process.kill(signal, Process.ppid)
          else
            Process.kill(signal, Process.pid)
          end
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
crono_trigger-0.8.1 lib/crono_trigger/models/signal.rb
crono_trigger-0.8.0 lib/crono_trigger/models/signal.rb
crono_trigger-0.7.1 lib/crono_trigger/models/signal.rb
crono_trigger-0.7.0 lib/crono_trigger/models/signal.rb
crono_trigger-0.6.4 lib/crono_trigger/models/signal.rb
crono_trigger-0.6.3 lib/crono_trigger/models/signal.rb
crono_trigger-0.6.2 lib/crono_trigger/models/signal.rb
crono_trigger-0.6.1 lib/crono_trigger/models/signal.rb
crono_trigger-0.6.0 lib/crono_trigger/models/signal.rb
crono_trigger-0.5.4 lib/crono_trigger/models/signal.rb
crono_trigger-0.5.3 lib/crono_trigger/models/signal.rb
crono_trigger-0.5.2 lib/crono_trigger/models/signal.rb
crono_trigger-0.5.1 lib/crono_trigger/models/signal.rb
crono_trigger-0.5.0 lib/crono_trigger/models/signal.rb
crono_trigger-0.4.0 lib/crono_trigger/models/signal.rb
crono_trigger-0.3.4 lib/crono_trigger/models/signal.rb