Class: Jiggler::Job::Enqueuer

Inherits:
Object
  • Object
show all
Defined in:
lib/jiggler/job.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, options = {}) ⇒ Enqueuer

Returns a new instance of Enqueuer.



43
44
45
46
# File 'lib/jiggler/job.rb', line 43

def initialize(klass, options = {})
  @options = options
  @klass = klass
end

Instance Method Details

#configObject



95
96
97
# File 'lib/jiggler/job.rb', line 95

def config
  @config ||= Jiggler.config
end

#enqueue(*args) ⇒ Object



53
54
55
56
57
# File 'lib/jiggler/job.rb', line 53

def enqueue(*args)
  Jiggler.config.client_redis_pool.acquire do |conn|
    conn.call('LPUSH', list_name, job_args(args))
  end
end

#enqueue_bulk(args_arr) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/jiggler/job.rb', line 59

def enqueue_bulk(args_arr)
  Jiggler.config.client_redis_pool.acquire do |conn|
    conn.pipelined do |pipeline|
      args_arr.each do |args|
        pipeline.call('LPUSH', list_name, job_args(args))
      end
    end
  end
end

#enqueue_in(seconds, *args) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/jiggler/job.rb', line 69

def enqueue_in(seconds, *args)
  timestamp = Time.now.to_f + seconds
  Jiggler.config.client_redis_pool.acquire do |conn|
    conn.call(
      'ZADD',
      config.scheduled_set, 
      timestamp, 
      job_args(args)
    )
  end
end

#job_args(raw_args) ⇒ Object



85
86
87
# File 'lib/jiggler/job.rb', line 85

def job_args(raw_args)
  Oj.dump({ name: @klass.name, args: raw_args, **job_options }, mode: :compat)
end

#job_optionsObject



89
90
91
92
93
# File 'lib/jiggler/job.rb', line 89

def job_options
  retries = @options.fetch(:retries, @klass.retries)
  jid = @options.fetch(:jid, SecureRandom.hex(8))
  { retries: retries, jid: jid }
end

#list_nameObject



81
82
83
# File 'lib/jiggler/job.rb', line 81

def list_name
  "#{config.queue_prefix}#{@options.fetch(:queue, @klass.queue)}"
end

#with_options(options) ⇒ Object



48
49
50
51
# File 'lib/jiggler/job.rb', line 48

def with_options(options)
  @options.merge(options)
  self
end