Class: Rjob::Scripts::EnqueueScheduledJobs

Inherits:
RedisScript
  • Object
show all
Defined in:
lib/rjob/scripts/enqueue_scheduled_jobs.rb

Instance Attribute Summary

Attributes inherited from RedisScript

#sha1

Instance Method Summary collapse

Instance Method Details

#arg_paramsObject



4
5
6
# File 'lib/rjob/scripts/enqueue_scheduled_jobs.rb', line 4

def arg_params
  %i(time_now job_limit bucket_no)
end

#key_paramsObject



8
9
10
# File 'lib/rjob/scripts/enqueue_scheduled_jobs.rb', line 8

def key_params
  %i(scheduled_key dest_key jobs_key)
end

#lua_scriptObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rjob/scripts/enqueue_scheduled_jobs.rb', line 12

def lua_script
  <<~LUA
    local r = redis
    local time_now = ARGV[1]
    local job_limit = ARGV[2]
    local bucket_no = ARGV[3]

    local scheduled_key = KEYS[1]
    local dest_key = KEYS[2]
    local jobs_key = KEYS[3]

    local jobs = r.call('zrangebyscore', scheduled_key, 0, time_now, 'limit', 0, job_limit)
    if #jobs == 0 then
      return 0
    end

    local i
    for i=1, #jobs do
      r.call('lpush', dest_key, jobs[i])
    end
    r.call('zrem', scheduled_key, unpack(jobs))
    r.call('publish', jobs_key, bucket_no)

    return #jobs
  LUA
end