Sha256: b2095c390f090e2e1f2367b8cb3335ff27c57b1f601196d4146f66f73296dcad
Contents?: true
Size: 1.34 KB
Versions: 23
Compression:
Stored size: 1.34 KB
Contents
# frozen_string_literal: true # # Mixin for objects that act as servers # module ServerProcessAble extend ActiveSupport::Concern def self.included(base) base.class_eval do # # Fields # field :host_name, type: String field :pid, type: Integer field :running, type: Mongoid::Boolean, default: false field :last_check_in_at, type: Time # # Validations # validates :host_name, presence: true validates :pid, presence: true end base.extend ClassMethods end # # Methods for the concrete class this concern is attached too, ways to find or create or just find the # associated server. # module ClassMethods # # Find a record for this server # def find_or_create_server find_or_create_by!(host_name: Socket.gethostname, pid: Process.pid) end # # Find a worker, return nil if not found # def find_server find_by(host_name: Socket.gethostname, pid: Process.pid) rescue StandardError nil end end # # Perform a check in for the server # def check_in set(last_check_in_at: Time.now.utc) end # # Stop the worker # def stop set(running: false, last_check_in_at: Time.now.utc) end # # Start the worker # def start set(running: true, last_check_in_at: Time.now.utc) end end
Version data entries
23 entries across 23 versions & 1 rubygems