Class: Sqewer::ExecutionContext

Inherits:
Object
  • Object
show all
Defined in:
lib/sqewer/execution_context.rb

Overview

Is passed to each Job when executing (is the argument for the #run method of the job). The job can use this object to submit extra jobs, or to get at the things specific for the execution context (database/key-value store connections, error handling transaction and so on).

Instance Method Summary collapse

Constructor Details

#initialize(submitter, extra_variables = {}) ⇒ ExecutionContext

Create a new ExecutionContext with an environment hash.

Parameters:

  • submitter (Sqewer::Submitter)

    the object to submit new jobs through. Used when jobs want to submit jobs

  • extra_variables (Hash) (defaults to: {})

    any extra data to pass around to each Job



12
13
14
15
16
# File 'lib/sqewer/execution_context.rb', line 12

def initialize(submitter, extra_variables={})
  @submitter = submitter
  @params = {}
  extra_variables.each_pair{|k, v| self[k]  = v }
end

Instance Method Details

#[](key) ⇒ Object

Returns a key of the execution environment by name

Parameters:

  • key (#to_s)

    the key to get



36
37
38
# File 'lib/sqewer/execution_context.rb', line 36

def [](key)
  @params[key.to_s]
end

#[]=(key, value) ⇒ Object

Sets a key in the execution environment

Parameters:

  • key (#to_s)

    the key to set

  • value

    the value to set



29
30
31
# File 'lib/sqewer/execution_context.rb', line 29

def []=(key, value)
  @params[key.to_s] = value
end

#fetch(key, &blk) ⇒ Object

Returns a key of the execution environment, or executes the given block if the key is not set

Parameters:

  • key (#to_s)

    the key to get

  • blk

    the block to execute if no such key is present



45
46
47
# File 'lib/sqewer/execution_context.rb', line 45

def fetch(key, &blk)
  @params.fetch(key.to_s, &blk)
end

#loggerLogger

Returns the logger set in the execution environment, or the NullLogger if no logger is set. Can be used to supply a logger prefixed with job parameters per job.

Returns:

  • (Logger)

    the logger to send messages to.



54
55
56
# File 'lib/sqewer/execution_context.rb', line 54

def logger
  @params.fetch('logger') { Sqewer::NullLogger }
end

#submit!(job, **execution_options) ⇒ Object

Submits one or more jobs to the queue

See Also:

  • Sqewer::ExecutionContext.{Sqewer{Sqewer::Submitter{Sqewer::Submitter#submit!}


21
22
23
# File 'lib/sqewer/execution_context.rb', line 21

def submit!(job, **execution_options)
  @submitter.submit!(job, **execution_options)
end