Class: Rjob::RecurringJob

Inherits:
Object
  • Object
show all
Defined in:
lib/rjob/recurring_job.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, cron, job_class_name, job_arguments, unique_id = nil) ⇒ RecurringJob

Returns a new instance of RecurringJob.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rjob/recurring_job.rb', line 10

def initialize(context, cron, job_class_name, job_arguments, unique_id=nil)
  @context = context
  @cron = cron
  @job_class_name = job_class_name
  @job_class = nil
  @job_arguments = job_arguments

  @unique_id = unique_id

  generate_unique_id! unless @unique_id
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#cronObject (readonly)

Returns the value of attribute cron.



5
6
7
# File 'lib/rjob/recurring_job.rb', line 5

def cron
  @cron
end

#job_argumentsObject (readonly)

Returns the value of attribute job_arguments.



7
8
9
# File 'lib/rjob/recurring_job.rb', line 7

def job_arguments
  @job_arguments
end

#job_class_nameObject (readonly)

Returns the value of attribute job_class_name.



6
7
8
# File 'lib/rjob/recurring_job.rb', line 6

def job_class_name
  @job_class_name
end

#unique_idObject (readonly)

Returns the value of attribute unique_id.



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

def unique_id
  @unique_id
end

Class Method Details

.from_definition(context, defn) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/rjob/recurring_job.rb', line 43

def self.from_definition(context, defn)
  new(
    context,
    Fugit.parse(defn[:cron]),
    defn[:job_class].to_s,
    defn[:arguments],
    defn[:unique_id]
  )
end

Instance Method Details

#job_classObject



39
40
41
# File 'lib/rjob/recurring_job.rb', line 39

def job_class
  @job_class ||= @context.demodularize_class(@job_class_name)
end

#maybe_enqueue(redis) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rjob/recurring_job.rb', line 22

def maybe_enqueue(redis)
  key_name = "#{@context.prefix}:recurring:1:#{@unique_id}:lastrun".b
  current_time = Time.now

  last_run_str = redis.get(key_name)
  last_run = last_run_str ? Time.parse(last_run_str) : (current_time - 1)

  next_run_on = @cron.next_time(last_run)
  should_run = (current_time >= next_run_on.to_t)

  @context.enqueue_job_with_redis(job_class, job_arguments, redis) if should_run

  if should_run || last_run_str == nil
    redis.set(key_name, current_time.utc.to_s, ex: @cron.rough_frequency * 2)
  end
end