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.



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

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

Instance Attribute Details

#tasksHash<String, Task> (readonly)

Returns:

  • (Hash<String, Task>)


25
26
27
# File 'lib/karafka/pro/recurring_tasks/schedule.rb', line 25

def tasks
  @tasks
end

#versionString (readonly)

Returns:

  • (String)


22
23
24
# File 'lib/karafka/pro/recurring_tasks/schedule.rb', line 22

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:



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

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

#each(&block) ⇒ Object

Iterates over tasks yielding them one after another

Parameters:

  • block (Proc)

    block that will be executed with each task



44
45
46
# File 'lib/karafka/pro/recurring_tasks/schedule.rb', line 44

def each(&block)
  @tasks.each_value(&block)
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



50
51
52
# File 'lib/karafka/pro/recurring_tasks/schedule.rb', line 50

def find(id)
  @tasks[id]
end

#schedule(**args, &block) ⇒ Object

Allows us to have a nice DSL for defining schedules

Parameters:

  • args (Array)

    attributes accepted by the task initializer

  • block (Proc)

    block to execute



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

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