# encoding: UTF-8 # frozen_string_literal: true # Refinements # ======================================================================= require 'nrser/refinements/types' using NRSER::Types # Definitions # ======================================================================= # TODO Doc me pls # class Locd::CLI::Command::Job < Locd::CLI::Command::Agent # Helpers # ============================================================================ # def self.agent_class Locd::Agent::Job end def self.parse_every_option value case value when /\A\d+\z/, /\A\d+s\z/ value.to_i when /\A\d+m\z/ value[0...-1].to_i * 60 when /\A\d+h\z/ value[0...-1].to_i * 60 * 60 when /\A\d+d\z/ value[0...-1].to_i * 60 * 60 * 24 else raise ArgumentError, "Can't parse `every` option value: #{ value.inspect }" end end protected # ======================================================================== def agent_table agents Locd::CLI::Table.build do |t| t.col "PID", &:pid t.col "LEC", desc: "Last Exit Code", &:last_exit_code t.col "File" do |agent| agent_file agent end t.rows agents end end def get_start_interval if options[:every] && options[:at] raise Thor::ConflictingArgumentError, "Don't supply both `every` and `at` options" end start_interval = if options[:every] self.class.parse_every_option options[:every] elsif options[:at] options[:at].map { |key, value| [key.to_sym, value.to_i] }.to_h else raise Thor::RequiredArgumentMissingError, "Must provide `every` or `at` option" end logger.debug "Parsed start interval", start_interval: start_interval start_interval end # end protected public # Write Commands # ---------------------------------------------------------------------------- desc "add [OPTIONS] -- CMD_TEMPLATE...", "Add job that runs in the current directory" include_options groups: [:write, :add, :respond_with_agents] option :every, desc: "How often to start the job", type: :string option :at, desc: "Day/time to start the job", type: :hash def add *cmd_template, **kwds logger.trace "#{ self.class.name }##{ __method__ }", options: options, cmd_template: cmd_template, kwds: kwds, shared: self.class.shared_method_options kwds[:start_interval] ||= get_start_interval super *cmd_template, **kwds # job = agent_class.add \ # cmd_template: cmd_template, # start_interval: get_start_interval, # **option_kwds( groups: [:write, :add] ) # # logger.info "`#{ job.label }` job added." # # job.reload if options[:load] # # respond job end end # class Locd::CLI::Command::Job