# encoding: UTF-8 # frozen_string_literal: true # Refinements # ======================================================================= require 'nrser/refinements/types' using NRSER::Types # Definitions # ======================================================================= # A user-defined agent that runs periodically to perform a task. # class Locd::Agent::Job < Locd::Agent # Modules # ============================================================================ module Types def self.start_calendar_interval t.and( t.shape( minute: t.maybe( t.non_neg_int ), hour: t.maybe( t.non_neg_int ), day: t.maybe( t.and( t.int, 0..31 ) ), weekday: t.maybe( t.and( t.int, 0..7 ) ), month: t.maybe( t.and( t.int, 0..11 ) ), ), # All values can't be `nil` t.not( t.hash_( values: nil ) ), ) end def self.start_calendar_intervals t.array start_calendar_interval end end # Class Methods # ====================================================================== # @todo Document plist? method. # # @param [type] arg_name # @todo Add name param description. # # @return [return_type] # @todo Document return value. # def self.plist? plist super( plist ) && ( plist.key?( 'StartInterval' ) || plist.key?( 'StartCalendarInterval' ) ) end # .plist? # @!group Creating Servers # -------------------------------------------------------------------------- # Create the `launchd` property list data for a new {Locd::Agent::Site}, which # has an additional `port:` keyword versus {Locd::Agent.create_plist_data}. # # @param cmd_template (see Locd::Agent.create_plist_data) # @param label (see Locd::Agent.create_plist_data) # @param workdir (see Locd::Agent.create_plist_data) # @param log_path (see Locd::Agent.create_plist_data) # @param keep_alive (see Locd::Agent.create_plist_data) # @param run_at_load (see Locd::Agent.create_plist_data) # # @param [Fixnum | Hash] start_interval: # How often to start the job: # # 1. {Fixnum} - Run about every X number of seconds (positive integer). # 2. {Hash} # # @return (see Locd::Agent.create_plist_data) # # @example Run a job about every minute # Locd::Agent::Job.create_plist_date \ # cmd_template: 'myexe do-some-job', # label: 'myexe.do-some-job', # workdir: '~', # start_interval: 60 # # @example Run a job every hour, on the hour # Locd::Agent::Job.create_plist_date \ # cmd_template: 'myexe do-some-job', # label: 'myexe.do-some-job', # workdir: '~', # start_interval: { # minute: 0, # } # def self.create_plist_data cmd_template:, label:, workdir:, start_interval:, log_path: nil, keep_alive: false, run_at_load: false, **extras plist_data = super cmd_template: cmd_template, label: label, workdir: workdir, log_path: log_path, keep_alive: keep_alive, run_at_load: run_at_load, **extras start_interval_data = t.match start_interval, t.pos_int, { "StartInterval" => start_interval }, Types.start_calendar_interval, ->( interval ) { { 'StartCalendarInterval' => interval.map { |key, value| [key.to_s.capitalize, value] }.to_h } }, Types.start_calendar_intervals, ->( intervals ) { intervals.map { |interval| interval.map { |key, value| [key.to_s.capitalize, value] }.to_h } } plist_data.merge start_interval_data end # Attributes # ====================================================================== # Instance Methods # ====================================================================== end # class Locd::Agent::Job