Class: MultiBackgroundJob::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/multi_background_job/worker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker_class, **options) ⇒ Worker

Returns a new instance of Worker.



11
12
13
14
15
16
# File 'lib/multi_background_job/worker.rb', line 11

def initialize(worker_class, **options)
  @worker_class = worker_class
  @options = options
  @payload = {}
  unique(@options.delete(:uniq)) if @options.key?(:uniq)
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



9
10
11
# File 'lib/multi_background_job/worker.rb', line 9

def arguments
  @arguments
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/multi_background_job/worker.rb', line 7

def options
  @options
end

#payloadObject (readonly)

Returns the value of attribute payload.



7
8
9
# File 'lib/multi_background_job/worker.rb', line 7

def payload
  @payload
end

#unique_jobObject (readonly)

Returns the value of attribute unique_job.



7
8
9
# File 'lib/multi_background_job/worker.rb', line 7

def unique_job
  @unique_job
end

#worker_classObject (readonly)

Returns the value of attribute worker_class.



7
8
9
# File 'lib/multi_background_job/worker.rb', line 7

def worker_class
  @worker_class
end

Class Method Details

.coerce(service:, payload:, **opts) ⇒ Object



18
19
20
# File 'lib/multi_background_job/worker.rb', line 18

def self.coerce(service:, payload:, **opts)
  SERVICES.fetch(service).coerce_to_worker(payload, **opts)
end

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
# File 'lib/multi_background_job/worker.rb', line 100

def eql?(other)
  return false unless other.is_a?(self.class)

  worker_class == other.worker_class && \
    payload == other.payload &&
    options == other.options &&
    unique_job == other.unique_job
end

#in(timestamp) ⇒ Object Also known as: at

Schedule the time when a job will be executed. Jobs which are scheduled in the past are enqueued for immediate execution.

Parameters:

  • timestamp (Numeric)

    timestamp, numeric or something that acts numeric.

Returns:

  • self



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/multi_background_job/worker.rb', line 48

def in(timestamp)
  now = Time.now.to_f
  timestamp = Time.parse(timestamp) if timestamp.is_a?(String)
  int = timestamp.respond_to?(:strftime) ? timestamp.to_f : now + timestamp.to_f
  return self if int <= now

  @payload['at'] = int
  @payload['created_at'] = now

  self
end

#push(to: nil) ⇒ Object

Returns Response of service.

Parameters:

  • :to (Symbol)

    Adapter key

Returns:

  • Response of service

See Also:

  • for more details


87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/multi_background_job/worker.rb', line 87

def push(to: nil)
  to ||= options[:service]
  unless SERVICES.include?(to)
    raise Error, format('Service %<to>p is not implemented. Please use one of %<list>p', to: to, list: SERVICES.keys)
  end

  @payload['created_at'] ||= Time.now.to_f
  worker_to_push = with_job_jid
  MultiBackgroundJob.config.middleware.invoke(worker_to_push, to) do
    SERVICES[to].push(worker_to_push)
  end
end

#unique(value) ⇒ Object

Wrap uniq options

Parameters:

  • value (Hash)

    Unique configurations with ‘across`, `timeout` and `unlock_policy`

Returns:

  • self



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/multi_background_job/worker.rb', line 65

def unique(value)
  value = {} if value == true
  @unique_job = \
    case value
    when Hash then UniqueJob.coerce(value)
    when UniqueJob then value
    else
      nil
    end

  self
end

#unique_job?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/multi_background_job/worker.rb', line 110

def unique_job?
  unique_job.is_a?(UniqueJob)
end

#with_args(*args) ⇒ Object

Adds arguments to the job

Returns:

  • self



39
40
41
42
43
# File 'lib/multi_background_job/worker.rb', line 39

def with_args(*args)
  @payload['args'] = args

  self
end

#with_job_jid(jid = nil) ⇒ Object



78
79
80
81
82
# File 'lib/multi_background_job/worker.rb', line 78

def with_job_jid(jid = nil)
  @payload['jid'] ||= jid || MultiBackgroundJob.jid

  self
end