Class: Bricolage::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/bricolage/application.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

Returns a new instance of Application.



32
33
34
35
# File 'lib/bricolage/application.rb', line 32

def initialize
  @hooks = Bricolage
  @start_time = Time.now
end

Class Method Details

.install_signal_handlersObject



22
23
24
25
# File 'lib/bricolage/application.rb', line 22

def Application.install_signal_handlers
  Signal.trap('PIPE', 'IGNORE')
  PostgresConnection.install_signal_handlers
end

.mainObject



27
28
29
30
# File 'lib/bricolage/application.rb', line 27

def Application.main
  install_signal_handlers
  new.main
end

Instance Method Details

#build_log_locator(job) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/bricolage/application.rb', line 93

def build_log_locator(job)
  @log_locator_builder.build(
    job_ref: JobNet::JobRef.new(job.subsystem, job.id, '-'),
    jobnet_id: "#{job.subsystem}/#{job.id}",
    job_start_time: @start_time,
    jobnet_start_time: @start_time
  )
end

#error_exit(msg) ⇒ Object



161
162
163
164
# File 'lib/bricolage/application.rb', line 161

def error_exit(msg)
  print_error msg
  exit 1
end

#list_declarations(decls) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/bricolage/application.rb', line 145

def list_declarations(decls)
  decls.each do |decl|
    if decl.have_default_value?
      puts "#{decl.name}\t= #{decl.default_value.inspect}"
    else
      puts decl.name
    end
  end
end

#list_variables(vars) ⇒ Object



139
140
141
142
143
# File 'lib/bricolage/application.rb', line 139

def list_variables(vars)
  vars.each_variable do |var|
    puts "#{var.name}=#{var.value.inspect}"
  end
end

#load_job(ctx, opts) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/bricolage/application.rb', line 102

def load_job(ctx, opts)
  if opts.file_mode?
    Job.load_file(opts.job_file, ctx)
  else
    usage_exit "no job class given", opts.help if ARGV.empty?
    job_class_id = ARGV.shift
    Job.instantiate(nil, job_class_id, ctx)
  end
rescue ParameterError => ex
  raise if $DEBUG
  usage_exit ex.message, opts.help
end

#mainObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/bricolage/application.rb', line 37

def main
  opts = GlobalOptions.new(self)
  @hooks.run_before_option_parsing_hooks(opts)
  opts.parse!(ARGV)

  @ctx = Context.for_application(opts.home, opts.job_file, environment: opts.environment, global_variables: opts.global_variables)
  opts.merge_saved_options(@ctx.load_system_options)

  if opts.dump_options?
    opts.option_pairs.each do |name, value|
      puts "#{name}=#{value.inspect}"
    end
    exit 0
  end
  if opts.list_global_variables?
    list_variables @ctx.global_variables.resolve
    exit 0
  end

  job = load_job(@ctx, opts)
  process_job_options(job, opts)
  job.compile

  if opts.list_declarations?
    list_declarations job.declarations
    exit 0
  end
  if opts.list_variables?
    list_variables job.variables
    exit 0
  end
  if opts.dry_run?
    puts job.script_source
    exit 0
  end
  if opts.explain?
    job.explain
    exit 0
  end

  @log_locator_builder = LogLocatorBuilder.for_options(@ctx, opts.log_path_format, opts.log_s3_ds, opts.log_s3_key_format)

  @hooks.run_before_all_jobs_hooks(BeforeAllJobsEvent.new(job.id, [job]))
  @hooks.run_before_job_hooks(BeforeJobEvent.new(job))
  result = job.execute(log_locator: build_log_locator(job))
  @hooks.run_after_job_hooks(AfterJobEvent.new(result))
  @hooks.run_after_all_jobs_hooks(AfterAllJobsEvent.new(result.success?, [job]))
  exit result.status
rescue OptionError => ex
  raise if $DEBUG
  usage_exit ex.message, opts.help
rescue ApplicationError => ex
  raise if $DEBUG
  error_exit ex.message
end


166
167
168
# File 'lib/bricolage/application.rb', line 166

def print_error(msg)
  $stderr.puts "#{program_name}: error: #{msg}"
end

#process_job_options(job, opts) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/bricolage/application.rb', line 115

def process_job_options(job, opts)
  parser = OptionParser.new
  parser.banner = "Usage: #{program_name} #{job.class_id} [job_class_options]"
  job.parsing_options {|job_opt_defs|
    job_opt_defs.define_options parser
    parser.on_tail('--help', 'Shows this message and quit.') {
      puts parser.help
      exit 0
    }
    parser.on_tail('--version', 'Shows program version and quit.') {
      puts "#{APPLICATION_NAME} version #{VERSION}"
      exit 0
    }
    parser.parse!
  }
  unless ARGV.empty?
    msg = opts.file_mode? ? "--job-file and job class argument is exclusive" : "bad argument: #{ARGV.first}"
    usage_exit msg, parser.help
  end
rescue OptionError => ex
  raise if $DEBUG
  usage_exit ex.message, parser.help
end

#program_nameObject



170
171
172
# File 'lib/bricolage/application.rb', line 170

def program_name
  File.basename($PROGRAM_NAME, '.*')
end

#usage_exit(msg, usage) ⇒ Object



155
156
157
158
159
# File 'lib/bricolage/application.rb', line 155

def usage_exit(msg, usage)
  print_error msg
  $stderr.puts usage
  exit 1
end