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.



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

def initialize(id, job_class, context)
  @id = id
  @job_class = job_class
  @context = context
  @global_variables = nil
  @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

#global_variablesObject (readonly)

valid after #init_global_variables



70
71
72
# File 'lib/bricolage/job.rb', line 70

def global_variables
  @global_variables
end

#idObject (readonly)

Returns the value of attribute id.



51
52
53
# File 'lib/bricolage/job.rb', line 51

def id
  @id
end

#paramsObject (readonly)

Returns the value of attribute params.



69
70
71
# File 'lib/bricolage/job.rb', line 69

def params
  @params
end

#scriptObject (readonly)

valid after #compile



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

def script
  @script
end

#variablesObject (readonly)

valid after #compile



71
72
73
# File 'lib/bricolage/job.rb', line 71

def variables
  @variables
end

Class Method Details

.instantiate(id, class_id, ctx) ⇒ Object

For standalone job (command line mode)



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

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)



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

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



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

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



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

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

#class_idObject



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

def class_id
  @job_class.id
end

#compileObject



84
85
86
87
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
# File 'lib/bricolage/job.rb', line 84

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

  # 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_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,
    @global_variables,
    @params.variables,   # Like $dest_table
    job_file_rest_vars,
    job_v_opt_vars
    #          v High precedence
  )
  @variables = vars.resolve
  @variables.bind_declarations declarations

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

#declarationsObject



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

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

#execute(log_locator: LogLocator.empty) ⇒ Object



143
144
145
146
147
# File 'lib/bricolage/job.rb', line 143

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

#execute_in_process(log_locator:) ⇒ Object



149
150
151
152
153
154
155
156
157
158
# File 'lib/bricolage/job.rb', line 149

def execute_in_process(log_locator:)
  # ??? FIXME: status_path should be independent from log_path.
  # Also, status_path should be defined regardless of log_path.
  status_path = log_locator.path ? "#{log_locator.path}.status" : nil
  isolate_process(status_path) {
    log_locator.redirect_stdouts {
      do_execute
    }
  }
end

#explainObject



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

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

#init_global_variablesObject



61
62
63
64
65
66
67
# File 'lib/bricolage/job.rb', line 61

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



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

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

#provide_default(name, value) ⇒ Object



120
121
122
# File 'lib/bricolage/job.rb', line 120

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

#provide_sql_file_by_job_idObject

Called from jobclasses (parameters_filter)



125
126
127
# File 'lib/bricolage/job.rb', line 125

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

#script_sourceObject



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

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

#subsystemObject



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

def subsystem
  @context.subsystem_name
end