Module: Cloudtasker::Worker::ClassMethods

Defined in:
lib/cloudtasker/worker.rb,
lib/cloudtasker/testing.rb

Overview

Module class methods

Instance Method Summary collapse

Instance Method Details

#cache_key(val = nil) ⇒ String

Return a namespaced cache key.

Parameters:

  • val (Any, Array<Any>, nil) (defaults to: nil)

    The key to namespace

Returns:

  • (String)

    The namespaced key(s).



94
95
96
# File 'lib/cloudtasker/worker.rb', line 94

def cache_key(val = nil)
  [to_s.underscore, val].flatten.compact.map(&:to_s).join('/')
end

#cloudtasker_options(opts = {}) ⇒ Hash

Set the worker runtime options.

Parameters:

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

    The worker options.

Returns:

  • (Hash)

    The options set.



73
74
75
76
# File 'lib/cloudtasker/worker.rb', line 73

def cloudtasker_options(opts = {})
  opt_list = opts&.map { |k, v| [k.to_sym, v] } || [] # symbolize
  @cloudtasker_options_hash = opt_list.to_h
end

#cloudtasker_options_hashHash

Return the worker runtime options.

Returns:

  • (Hash)

    The worker runtime options.



83
84
85
# File 'lib/cloudtasker/worker.rb', line 83

def cloudtasker_options_hash
  @cloudtasker_options_hash || {}
end

#drainArray<any>

Run all jobs related to this worker class.

Returns:

  • (Array<any>)

    The return values of the workers perform method.



128
129
130
# File 'lib/cloudtasker/testing.rb', line 128

def drain
  Backend::MemoryTask.drain(to_s)
end

#jobsArray<Cloudtasker::Backend::MemoryTask>

Return all jobs related to this worker class.

Returns:



119
120
121
# File 'lib/cloudtasker/testing.rb', line 119

def jobs
  Backend::MemoryTask.all(to_s)
end

#max_retriesInteger

Return the numbeer of times this worker will be retried.

Returns:

  • (Integer)

    The number of retries.



152
153
154
# File 'lib/cloudtasker/worker.rb', line 152

def max_retries
  cloudtasker_options_hash[:max_retries] || Cloudtasker.config.max_retries
end

#perform_async(*args) ⇒ Cloudtasker::CloudTask

Enqueue worker in the backgroundf.

Parameters:

  • *args (Array<any>)

    List of worker arguments

Returns:



105
106
107
# File 'lib/cloudtasker/worker.rb', line 105

def perform_async(*args)
  schedule(args: args)
end

#perform_at(time_at, *args) ⇒ Cloudtasker::CloudTask

Enqueue worker and delay processing.

Parameters:

  • time_at (Time, Integer)

    The time at which the job should run.

  • *args (Array<any>)

    List of worker arguments

Returns:



129
130
131
# File 'lib/cloudtasker/worker.rb', line 129

def perform_at(time_at, *args)
  schedule(args: args, time_at: time_at)
end

#perform_in(interval, *args) ⇒ Cloudtasker::CloudTask

Enqueue worker and delay processing.

Parameters:

  • interval (Integer, nil)

    The delay in seconds.

  • *args (Array<any>)

    List of worker arguments.

Returns:



117
118
119
# File 'lib/cloudtasker/worker.rb', line 117

def perform_in(interval, *args)
  schedule(args: args, time_in: interval)
end

#redisCloudtasker::RedisClient

Return the Cloudtasker redis client

Returns:



62
63
64
# File 'lib/cloudtasker/worker.rb', line 62

def redis
  @redis ||= RedisClient.new
end

#schedule(args: nil, time_in: nil, time_at: nil, queue: nil) ⇒ Cloudtasker::CloudTask

Enqueue a worker with explicity options.

Parameters:

  • args (Array<any>) (defaults to: nil)

    The job arguments.

  • time_in (Time, Integer) (defaults to: nil)

    The delay in seconds.

  • time_at (Time, Integer) (defaults to: nil)

    The time at which the job should run.

  • queue (String, Symbol) (defaults to: nil)

    The queue on which the worker should run.

Returns:



143
144
145
# File 'lib/cloudtasker/worker.rb', line 143

def schedule(args: nil, time_in: nil, time_at: nil, queue: nil)
  new(job_args: args, job_queue: queue).schedule(**{ interval: time_in, time_at: time_at }.compact)
end