Class: Aias::JobBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/aias/job_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(shell: ENV.fetch("SHELL", "/bin/bash"), aia_path: nil, env_file: nil, config_file: nil) ⇒ JobBuilder

Returns a new instance of JobBuilder.



5
6
7
8
9
10
# File 'lib/aias/job_builder.rb', line 5

def initialize(shell: ENV.fetch("SHELL", "/bin/bash"), aia_path: nil, env_file: nil, config_file: nil)
  @shell       = shell
  @aia_path    = aia_path || 'aia'
  @env_file    = env_file || Paths::SCHEDULE_ENV
  @config_file = config_file
end

Instance Method Details

#build(scanner_result, prompts_dir: nil) ⇒ Object

Returns a single cron line string for the given scanner result, e.g.:

0 8 * * * /bin/bash -c 'source ~/.config/aia/schedule/env.sh && /path/to/aia --prompts-dir /path --config ~/.config/aia/schedule/aia.yml daily_digest > ~/.config/aia/schedule/logs/daily_digest.log 2>&1'

env.sh is sourced first to set PATH, API keys, etc. All flags come before the prompt ID. config_file selects the schedule-specific AIA config. prompts_dir sets the directory AIA searches for the prompt file.



18
19
20
21
22
23
24
25
26
# File 'lib/aias/job_builder.rb', line 18

def build(scanner_result, prompts_dir: nil)
  cron_expr    = resolved_cron(scanner_result.schedule)
  prompt_id    = scanner_result.prompt_id
  log          = log_path_for(prompt_id)
  prompts_flag = prompts_dir ? %( --prompts-dir "#{File.expand_path(prompts_dir)}") : ""
  config_flag  = @config_file ? %( --config "#{@config_file}") : ""
  cmd          = %(source "#{@env_file}" && #{@aia_path}#{prompts_flag}#{config_flag} #{prompt_id} > "#{log}" 2>&1)
  "#{cron_expr} #{shell_binary} -c '#{cmd}'"
end

#log_path_for(prompt_id) ⇒ Object

Returns the log file path for a given prompt_id. Mirrors the subdirectory structure of the prompt_id. e.g. "reports/weekly" → "~/.config/aia/schedule/logs/reports/weekly.log"



31
32
33
# File 'lib/aias/job_builder.rb', line 31

def log_path_for(prompt_id)
  File.join(Paths::SCHEDULE_LOG, "#{prompt_id}.log")
end