Class: Gouda::Scheduler::Entry

Inherits:
Struct
  • Object
show all
Defined in:
lib/gouda/scheduler.rb

Overview

A timer entry is either a Cron pattern or an interval duration, and configures which job needs to be scheduled and when

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argsObject

Returns the value of attribute args

Returns:

  • (Object)

    the current value of args



9
10
11
# File 'lib/gouda/scheduler.rb', line 9

def args
  @args
end

#cronObject

Returns the value of attribute cron

Returns:

  • (Object)

    the current value of cron



9
10
11
# File 'lib/gouda/scheduler.rb', line 9

def cron
  @cron
end

#interval_secondsObject

Returns the value of attribute interval_seconds

Returns:

  • (Object)

    the current value of interval_seconds



9
10
11
# File 'lib/gouda/scheduler.rb', line 9

def interval_seconds
  @interval_seconds
end

#job_classObject

Returns the value of attribute job_class

Returns:

  • (Object)

    the current value of job_class



9
10
11
# File 'lib/gouda/scheduler.rb', line 9

def job_class
  @job_class
end

#kwargsObject

Returns the value of attribute kwargs

Returns:

  • (Object)

    the current value of kwargs



9
10
11
# File 'lib/gouda/scheduler.rb', line 9

def kwargs
  @kwargs
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



9
10
11
# File 'lib/gouda/scheduler.rb', line 9

def name
  @name
end

#setObject

Returns the value of attribute set

Returns:

  • (Object)

    the current value of set



9
10
11
# File 'lib/gouda/scheduler.rb', line 9

def set
  @set
end

Instance Method Details

#build_active_jobActiveJob::Base

Builds the ActiveJob which can be enqueued for this entry

Returns:

  • (ActiveJob::Base)


31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gouda/scheduler.rb', line 31

def build_active_job
  next_at = self.next_at
  return unless next_at

  job_class = self.job_class.constantize

  active_job = kwargs_value.present? ? job_class.new(*args_value, **kwargs_value) : job_class.new(*args_value) # This method supports ruby2_keywords
  active_job.scheduled_at = next_at
  active_job.scheduler_key = scheduler_key

  set_value.present? ? active_job.set(set_value) : active_job
end

#next_atTime

Tells when this particular task should next run

Returns:

  • (Time)


17
18
19
20
21
22
23
24
25
26
# File 'lib/gouda/scheduler.rb', line 17

def next_at
  if interval_seconds
    first_existing = Gouda::Workload.where(scheduler_key: scheduler_key).where("scheduled_at > NOW()").order("scheduled_at DESC").pluck(:scheduled_at).first
    (first_existing || Time.now.utc) + interval_seconds
  elsif cron
    fugit = Fugit::Cron.parse(cron)
    raise ArgumentError, "Unable to parse cron pattern #{cron.inspect}" unless fugit
    Time.at(fugit.next_time.to_i).utc
  end
end

#scheduler_keyObject



10
11
12
# File 'lib/gouda/scheduler.rb', line 10

def scheduler_key
  [name, interval_seconds, cron, job_class].compact.join("_")
end