Class: Chronicle::ETL::CLI::Jobs

Inherits:
SubcommandBase show all
Defined in:
lib/chronicle/etl/cli/jobs.rb

Overview

CLI commands for working with ETL jobs

Instance Method Summary collapse

Methods inherited from SubcommandBase

banner, help, subcommand_prefix

Instance Method Details

#edit(name = nil) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/chronicle/etl/cli/jobs.rb', line 142

def edit(name = nil)
  cli_fail(message: "Job '#{name}' does not exist") if name && !Chronicle::ETL::Config.exists?('jobs', name)

  filename = Chronicle::ETL::Config.path('jobs', name)
  system "${VISUAL:-${EDITOR:-vi}} \"#{filename}\""

  definition = Chronicle::ETL::JobDefinition.new
  definition.add_config(load_job_config(name))
  definition.validate!

  cli_exit(message: "Job '#{name}' saved")
rescue Chronicle::ETL::JobDefinitionError => e
  cli_fail(message: 'Job definition error', exception: e)
end

#listObject

List available ETL jobs



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/chronicle/etl/cli/jobs.rb', line 159

def list
  jobs = Chronicle::ETL::Config.available_jobs

  job_details = jobs.map do |job|
    r = Chronicle::ETL::Config.load('jobs', job)

    extractor = r[:extractor][:name] if r[:extractor]
    transformer = r[:transformer][:name] if r[:transformer]
    loader = r[:loader][:name] if r[:loader]

    [job, extractor, transformer, loader]
  end

  headers = %w[name extractor transformer loader].map { |h| h.upcase.bold }

  puts 'Available jobs:'
  table = TTY::Table.new(headers, job_details)
  puts table.render(indent: 0, padding: [0, 2])
rescue Chronicle::ETL::ConfigError => e
  cli_fail(message: "Config error. #{e.message}", exception: e)
end

#save(name) ⇒ Object

Create an ETL job



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/chronicle/etl/cli/jobs.rb', line 104

def save(name)
  write_config = true
  job_definition = build_job_definition(name, options)
  job_definition.validate!

  if Chronicle::ETL::Config.exists?('jobs', name) && !options[:'skip-confirmation']
    prompt = TTY::Prompt.new
    write_config = false
    message = "Job '#{name}' exists already. Ovewrite it?"
    begin
      write_config = prompt.yes?(message)
    rescue TTY::Reader::InputInterrupt
    end
  end

  if write_config
    Chronicle::ETL::Config.write('jobs', name, job_definition.definition)
    cli_exit(message: "Job saved. Run it with `$ chronicle-etl jobs:run #{name}`")
  else
    cli_fail(message: "\nJob not saved")
  end
rescue Chronicle::ETL::JobDefinitionError => e
  cli_fail(message: 'Job definition error', exception: e)
end

#show(name = nil) ⇒ Object

Show an ETL job



131
132
133
134
135
136
137
138
139
# File 'lib/chronicle/etl/cli/jobs.rb', line 131

def show(name = nil)
  cli_fail(message: "Job '#{name}' does not exist") if name && !Chronicle::ETL::Config.exists?('jobs', name)

  job_definition = build_job_definition(name, options)
  job_definition.validate!
  puts Chronicle::ETL::Job.new(job_definition)
rescue Chronicle::ETL::JobDefinitionError => e
  cli_fail(message: 'Job definition error', exception: e)
end

#start(*args) ⇒ Object

Run an ETL job



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/chronicle/etl/cli/jobs.rb', line 68

def start(*args)
  name = args.first

  # If someone runs `$ chronicle-etl` with no arguments, show help menu.
  # TODO: decide if we should check that there's nothing in stdin pipe
  # in case user wants to actually run this sort of job stdin->null->stdout
  if name.nil? && options[:extractor].nil?
    m = Chronicle::ETL::CLI::Main.new
    m.help
    cli_exit
  end

  cli_fail(message: "Job '#{name}' does not exist") if name && !Chronicle::ETL::Config.exists?('jobs', name)

  job_definition = build_job_definition(name, options)

  if job_definition.plugins_missing?
    missing_plugins = job_definition.errors[:plugins]
      .select { |error| error.is_a?(Chronicle::ETL::PluginNotInstalledError) }
      .map(&:name)
      .uniq
    install_missing_plugins(missing_plugins)
  end

  run_job(job_definition)
rescue Chronicle::ETL::JobDefinitionError => e
  message = ''
  job_definition.errors.each_pair do |category, errors|
    message << "Problem with #{category}:\n  - #{errors.map(&:to_s).join("\n  - ")}"
  end
  cli_fail(message: "Error running job.\n#{message}", exception: e)
end