Class: DistributedJob::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/distributed_job/client.rb

Overview

A ‘DistributedJob::Client` allows to easily manage distributed jobs. The main purpose of the client object is to configure settings to all distributed jobs or a group of distributed jobs like e.g. the redis connection and an optional namespace to be used to prefix all redis keys.

Examples:

DistributedJobClient = DistributedJob::Client.new(redis: Redis.new)

distributed_job = DistributedJobClient.build(token: SecureRandom.hex)

# Add job parts and queue background jobs
distributed_job.push_each(Date.parse('2021-01-01')..Date.today) do |date, part|
  SomeBackgroundJob.perform_async(date, distributed_job.token, part)
end

distributed_job.token # can be used to query the status of the distributed job

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis:, namespace: nil, default_ttl: 86_400) ⇒ Client

Creates a new ‘DistributedJob::Client`.

Examples:

DistributedJobClient = DistributedJob::Client.new(redis: Redis.new)

Parameters:

  • redis (Redis)

    The redis connection instance

  • namespace (String) (defaults to: nil)

    An optional namespace used to prefix redis keys

  • default_ttl (Integer) (defaults to: 86_400)

    The default number of seconds the jobs will stay available in redis. This value is used to automatically expire and clean up the jobs in redis. Default is 86400, i.e. one day. The ttl is used everytime the job is modified in redis.



36
37
38
39
40
# File 'lib/distributed_job/client.rb', line 36

def initialize(redis:, namespace: nil, default_ttl: 86_400)
  @redis = redis
  @namespace = namespace
  @default_ttl = default_ttl
end

Instance Attribute Details

#default_ttlObject (readonly)

Returns the value of attribute default_ttl.



22
23
24
# File 'lib/distributed_job/client.rb', line 22

def default_ttl
  @default_ttl
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



22
23
24
# File 'lib/distributed_job/client.rb', line 22

def namespace
  @namespace
end

#redisObject (readonly)

Returns the value of attribute redis.



22
23
24
# File 'lib/distributed_job/client.rb', line 22

def redis
  @redis
end

Instance Method Details

#build(token:, ttl: default_ttl) ⇒ Object

Builds a new ‘DistributedJob::Job` instance.

Parameters:

  • token (String)

    Some token to be used to identify the job. You can e.g. use SecureRandom.hex to generate one.

  • ttl (Integer) (defaults to: default_ttl)

    The number of seconds the job will stay available in redis. This value is used to automatically expire and clean up the job in redis. Default is ‘default_ttl`, i.e. one day. The ttl is used everytime the job is modified in redis.



51
52
53
# File 'lib/distributed_job/client.rb', line 51

def build(token:, ttl: default_ttl)
  Job.new(client: self, token: token, ttl: ttl)
end