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.



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bricolage/job.rb', line 36

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



67
68
69
# File 'lib/bricolage/job.rb', line 67

def global_variables
  @global_variables
end

#idObject (readonly)

Returns the value of attribute id.



48
49
50
# File 'lib/bricolage/job.rb', line 48

def id
  @id
end

#paramsObject (readonly)

Returns the value of attribute params.



66
67
68
# File 'lib/bricolage/job.rb', line 66

def params
  @params
end

#scriptObject (readonly)

valid after #compile



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

def script
  @script
end

#variablesObject (readonly)

valid after #compile



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

def variables
  @variables
end

Class Method Details

.instantiate(id, class_id, ctx) ⇒ Object

For standalone job (command line mode)



30
31
32
33
34
# File 'lib/bricolage/job.rb', line 30

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
# 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
  }
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



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

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

#class_idObject



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

def class_id
  @job_class.id
end

#compileObject



81
82
83
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
# File 'lib/bricolage/job.rb', line 81

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



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

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

#execute(log_locator: LogLocator.empty) ⇒ Object



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

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

#execute_in_process(log_locator:) ⇒ Object



146
147
148
149
150
151
152
153
154
155
# File 'lib/bricolage/job.rb', line 146

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



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

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

#init_global_variablesObject



58
59
60
61
62
63
64
# File 'lib/bricolage/job.rb', line 58

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



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

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

#provide_default(name, value) ⇒ Object



117
118
119
# File 'lib/bricolage/job.rb', line 117

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

#provide_sql_file_by_job_idObject

Called from jobclasses (parameters_filter)



122
123
124
# File 'lib/bricolage/job.rb', line 122

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

#script_sourceObject



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

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

#subsystemObject



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

def subsystem
  @context.subsystem_name
end