# encoding: UTF-8 # frozen_string_literal: true # Refinements # ======================================================================= using NRSER 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 # end protected public # Write Commands # ---------------------------------------------------------------------------- desc "add 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 logger.debug "#{ self.class.name }##{ __method__ }", options: options, cmd_template: cmd_template, shared: self.class.shared_method_options if options[:every] && options[:at] logger.error "Don't supply both `every` and `at` options", options: options exit 1 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 job = agent_class.add \ cmd_template: cmd_template, start_interval: start_interval, **option_kwds( groups: :write ) logger.info "`#{ job.label }` job added." job.load if options[:load] respond job end end # class Locd::CLI::Command::Job