Class: Karafka::Pro::RecurringTasks::Schedule

Inherits:
Object
  • Object
show all
Defined in:
lib/karafka/pro/recurring_tasks/schedule.rb

Overview

Represents the current code-context schedule with defined tasks and their cron execution details. Single schedule includes all the information about all the tasks that we have defined and to be executed in a given moment in time.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version:) ⇒ Schedule

Returns a new instance of Schedule.

Parameters:

  • schedule version. In case of usage of versioning it is used to ensure, that older still active processes do not intercept the assignment to run older version of the scheduler. It is important to make sure, that this string is comparable.



39
40
41
42
# File 'lib/karafka/pro/recurring_tasks/schedule.rb', line 39

def initialize(version:)
  @version = version
  @tasks = {}
end

Instance Attribute Details

#tasksHash{String => Task} (readonly)

Returns:



34
35
36
# File 'lib/karafka/pro/recurring_tasks/schedule.rb', line 34

def tasks
  @tasks
end

#versionString (readonly)

Returns:



31
32
33
# File 'lib/karafka/pro/recurring_tasks/schedule.rb', line 31

def version
  @version
end

Instance Method Details

#<<(task) ⇒ Object

Note:

In case of multiple tasks with the same id, it will overwrite

Adds task into the tasks accumulator

Parameters:



47
48
49
# File 'lib/karafka/pro/recurring_tasks/schedule.rb', line 47

def <<(task)
  @tasks[task.id] = task
end

#eachObject

Iterates over tasks yielding them one after another



52
53
54
# File 'lib/karafka/pro/recurring_tasks/schedule.rb', line 52

def each(&)
  @tasks.each_value(&)
end

#find(id) ⇒ Task?

Returns task with a given id or nil if not found.

Parameters:

  • id of a particular recurring task

Returns:

  • task with a given id or nil if not found



58
59
60
# File 'lib/karafka/pro/recurring_tasks/schedule.rb', line 58

def find(id)
  @tasks[id]
end

#schedule(**args) ⇒ Object

Allows us to have a nice DSL for defining schedules

Options Hash (**args):

  • :id (String)

    unique task identifier

  • :cron (String)

    cron expression for task scheduling

  • :previous_time (Proc)

    optional lambda returning previous execution time

Parameters:

  • attributes accepted by the task initializer



67
68
69
# File 'lib/karafka/pro/recurring_tasks/schedule.rb', line 67

def schedule(**args, &)
  self << Task.new(**args, &)
end