Class: Bricolage::Job

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, job_class, context) ⇒ Job

Returns a new instance of Job.



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bricolage/job.rb', line 40

def initialize(id, job_class, context)
  @id = id
  @job_class = job_class
  @context = context
  @global_variables = nil
  @option_variables = @context.option_variables
  @param_decls = @job_class.get_parameters
  @param_vals = nil      # Parameters::IntermediateValues by *.job
  @param_vals_opt = nil  # Parameters::IntermediateValues by options
  @params = nil
  @variables = nil
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



55
56
57
# File 'lib/bricolage/job.rb', line 55

def context
  @context
end

#global_variablesObject (readonly)

valid after #init_global_variables



74
75
76
# File 'lib/bricolage/job.rb', line 74

def global_variables
  @global_variables
end

#idObject (readonly)

Returns the value of attribute id.



53
54
55
# File 'lib/bricolage/job.rb', line 53

def id
  @id
end

#job_classObject (readonly)

Returns the value of attribute job_class.



54
55
56
# File 'lib/bricolage/job.rb', line 54

def job_class
  @job_class
end

#paramsObject (readonly)

Returns the value of attribute params.



73
74
75
# File 'lib/bricolage/job.rb', line 73

def params
  @params
end

#scriptObject (readonly)

valid after #compile



76
77
78
# File 'lib/bricolage/job.rb', line 76

def script
  @script
end

#variablesObject (readonly)

valid after #compile



75
76
77
# File 'lib/bricolage/job.rb', line 75

def variables
  @variables
end

Class Method Details

.instantiate(id, class_id, ctx) ⇒ Object

For standalone job (command line mode)



34
35
36
37
38
# File 'lib/bricolage/job.rb', line 34

def Job.instantiate(id, class_id, ctx)
  new(id, JobClass.get(class_id), ctx).tap {|job|
    job.init_global_variables
  }
end

.load_file(path, ctx) ⇒ Object

For standalone job (.job file mode)



23
24
25
26
27
28
29
30
31
# File 'lib/bricolage/job.rb', line 23

def Job.load_file(path, ctx)
  f = JobFile.load(ctx, path)
  instantiate(f.job_id, f.class_id, ctx).tap {|job|
    job.bind_parameters f.values
    f.global_variables.each do |name, value|
      job.global_variables[name.to_s] = value
    end
  }
end

.load_ref(ref, jobnet_context) ⇒ Object

For JobNetRunner



16
17
18
19
20
# File 'lib/bricolage/job.rb', line 16

def Job.load_ref(ref, jobnet_context)
  ctx = jobnet_context.subsystem(ref.subsystem)
  path = ctx.job_file(ref.name)
  load_file(path, ctx)
end

Instance Method Details

#bind_parameters(values) ⇒ Object

For job file



79
80
81
# File 'lib/bricolage/job.rb', line 79

def bind_parameters(values)
  @param_vals = @param_decls.parse_direct_values(values)
end

#class_idObject



57
58
59
# File 'lib/bricolage/job.rb', line 57

def class_id
  @job_class.id
end

#compileObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/bricolage/job.rb', line 88

def compile
  param_vals_default = @param_decls.parse_default_values(@global_variables.get_force('defaults'))
  @job_class.invoke_parameters_filter(self)

  job_file_rest_vars = @param_vals ? @param_vals.variables : Variables.new
  job_v_opt_vars = @param_vals_opt ? @param_vals_opt.variables : Variables.new
  cmd_v_opt_vars = @option_variables ? @option_variables : Variables.new

  # We use different variable set for paramter expansion and
  # SQL variable expansion.  Parameter expansion uses global
  # variables and "-v" option variables (both of global and job).
  base_vars = Variables.union(
    #          ^ Low precedence
    @global_variables,
    job_file_rest_vars,
    cmd_v_opt_vars,
    job_v_opt_vars
    #          v High precedence
  )
  pvals = @param_decls.union_intermediate_values(*[param_vals_default, @param_vals, @param_vals_opt].compact)
  @params = pvals.resolve(@context, base_vars.resolve)

  # Then, expand SQL variables and check with declarations.
  vars = Variables.union(
    #          ^ Low precedence
    declarations.default_variables, # default value written in *.sql
    @global_variables,   # from yaml file
    @params.variables,   # Like $dest_table in job file
    job_file_rest_vars,  # custom variable at header of job file
    cmd_v_opt_vars,      # -v option for bricolage/bricolage-jobnet command
    job_v_opt_vars       # -v option for bricolage command using jobclass
    #          v High precedence
  )
  @variables = vars.resolve
  @variables.bind_declarations declarations

  @script = @job_class.get_script(@params)
  @script.bind @context, @variables
end

#declarationsObject



137
138
139
# File 'lib/bricolage/job.rb', line 137

def declarations
  @declarations ||= @job_class.get_declarations(@params)
end

#execute(log_locator: LogLocator.empty) ⇒ Object



151
152
153
154
155
# File 'lib/bricolage/job.rb', line 151

def execute(log_locator: LogLocator.empty)
  log_locator.redirect_stdouts {
    do_execute
  }
end

#execute_in_process(log_locator:) ⇒ Object



157
158
159
160
161
162
163
164
# File 'lib/bricolage/job.rb', line 157

def execute_in_process(log_locator:)
  status_path = "#{Dir.tmpdir}/bricolage.#{$$}.status.#{"%010x" % rand(1000000000000)}"
  isolate_process(status_path) {
    log_locator.redirect_stdouts {
      do_execute
    }
  }
end

#explainObject



146
147
148
149
# File 'lib/bricolage/job.rb', line 146

def explain
  raise 'Job#explain called before #compile' unless @script
  @script.run_explain
end

#init_global_variablesObject



65
66
67
68
69
70
71
# File 'lib/bricolage/job.rb', line 65

def init_global_variables
  # Context#global_variables loads file on each call,
  # updating @global_variables is multi-thread safe.
  @global_variables = @context.global_variables
  @global_variables['bricolage_cwd'] = Dir.pwd
  @global_variables['bricolage_job_dir'] = @context.job_dir.to_s
end

#parsing_options(&block) ⇒ Object

For command line options



84
85
86
# File 'lib/bricolage/job.rb', line 84

def parsing_options(&block)
  @param_vals_opt = @param_decls.parsing_options(&block)
end

#provide_default(name, value) ⇒ Object



128
129
130
# File 'lib/bricolage/job.rb', line 128

def provide_default(name, value)
  @param_vals[name] ||= value if @param_vals
end

#provide_sql_file_by_job_idObject

Called from jobclasses (parameters_filter)



133
134
135
# File 'lib/bricolage/job.rb', line 133

def provide_sql_file_by_job_id
  provide_default 'sql-file', @id if @id
end

#script_sourceObject



141
142
143
144
# File 'lib/bricolage/job.rb', line 141

def script_source
  raise 'Job#script_source called before #compile' unless @script
  @script.source
end

#subsystemObject



61
62
63
# File 'lib/bricolage/job.rb', line 61

def subsystem
  @context.subsystem_name
end