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)


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.



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