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:

  • version (String)

    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.



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

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

Instance Attribute Details

#tasksHash{String => Task} (readonly)

Returns:

  • (Hash{String => Task})


42
43
44
# File 'lib/karafka/pro/recurring_tasks/schedule.rb', line 42

def tasks
  @tasks
end

#versionString (readonly)

Returns:

  • (String)


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

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:



55
56
57
# File 'lib/karafka/pro/recurring_tasks/schedule.rb', line 55

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

#eachObject

Iterates over tasks yielding them one after another



60
61
62
# File 'lib/karafka/pro/recurring_tasks/schedule.rb', line 60

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

#find(id) ⇒ Task?

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

Parameters:

  • id (String)

    id of a particular recurring task

Returns:

  • (Task, nil)

    task with a given id or nil if not found



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

def find(id)
  @tasks[id]
end

#schedule(**args) ⇒ Object

Allows us to have a nice DSL for defining schedules

Parameters:

  • args (Hash)

    attributes accepted by the task initializer

Options Hash (**args):

  • :id (String)

    unique task identifier

  • :cron (String)

    cron expression for task scheduling

  • :previous_time (Proc)

    optional lambda returning previous execution time



75
76
77
# File 'lib/karafka/pro/recurring_tasks/schedule.rb', line 75

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