Class: Karafka::Pro::RecurringTasks::Task

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

Overview

Represents a single recurring task that can be executed when the time comes. Tasks should be lightweight. Anything heavy should be executed by scheduling appropriate jobs here.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, cron:, previous_time: 0, enabled: true, &block) ⇒ Task

Returns a new instance of Task.

Parameters:

  • id (String)

    unique id. If re-used between versions, will replace older occurrence.

  • cron (String)

    cron expression matching this task expected execution times.

  • previous_time (Integer, Time) (defaults to: 0)

    previous time this task was executed. 0 if never.

  • enabled (Boolean) (defaults to: true)

    should this task be enabled. Users may disable given task temporarily, this is why we need to know that.

  • block (Proc)

    code to execute.



57
58
59
60
61
62
63
64
65
66
# File 'lib/karafka/pro/recurring_tasks/task.rb', line 57

def initialize(id:, cron:, previous_time: 0, enabled: true, &block)
  @id = id
  @cron = Fugit::Cron.do_parse(cron)
  @previous_time = previous_time
  @start_time = Time.now
  @executable = block
  @enabled = enabled
  @trigger = false
  @changed = false
end

Instance Attribute Details

#cronFugit::Cron (readonly)

Returns cron from parsing the raw cron expression.

Returns:

  • (Fugit::Cron)

    cron from parsing the raw cron expression



46
47
48
# File 'lib/karafka/pro/recurring_tasks/task.rb', line 46

def cron
  @cron
end

#idString (readonly)

Returns this task id.

Returns:

  • (String)

    this task id



43
44
45
# File 'lib/karafka/pro/recurring_tasks/task.rb', line 43

def id
  @id
end

#previous_timeObject

Allows for update of previous time when restoring the materialized state



49
50
51
# File 'lib/karafka/pro/recurring_tasks/task.rb', line 49

def previous_time
  @previous_time
end

Instance Method Details

#callObject

Executes the given task and publishes appropriate notification bus events.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/karafka/pro/recurring_tasks/task.rb', line 112

def call
  monitor.instrument(
    "recurring_tasks.task.executed",
    task: self
  ) do
    # We check for presence of the `@executable` because user can define cron schedule
    # without the code block
    return unless @executable

    execute
  end
rescue => e
  monitor.instrument(
    "error.occurred",
    caller: self,
    error: e,
    task: self,
    type: "recurring_tasks.task.execute.error"
  )
ensure
  @trigger = false
  @previous_time = Time.now
end

#call?Boolean

Returns should we execute this task at this moment in time.

Returns:

  • (Boolean)

    should we execute this task at this moment in time



102
103
104
105
106
107
108
109
# File 'lib/karafka/pro/recurring_tasks/task.rb', line 102

def call?
  return true if @trigger
  return false unless enabled?

  # Ensure the job is only due if current_time is strictly after the next_time
  # Please note that we can only compare eorbi against time and not the other way around
  next_time <= Time.now
end

#changed?Boolean

Returns true if anything in the task has changed and we should persist it.

Returns:

  • (Boolean)

    true if anything in the task has changed and we should persist it



69
70
71
# File 'lib/karafka/pro/recurring_tasks/task.rb', line 69

def changed?
  @changed
end

#clearObject

Removes the changes indicator flag



143
144
145
# File 'lib/karafka/pro/recurring_tasks/task.rb', line 143

def clear
  @changed = false
end

#disableObject

Disables this task execution indefinitely



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

def disable
  touch
  @enabled = false
end

#enableObject

Enables back this task



80
81
82
83
# File 'lib/karafka/pro/recurring_tasks/task.rb', line 80

def enable
  touch
  @enabled = true
end

#enabled?Boolean

Returns is this an executable task.

Returns:

  • (Boolean)

    is this an executable task



86
87
88
# File 'lib/karafka/pro/recurring_tasks/task.rb', line 86

def enabled?
  @enabled
end

#executeObject

Runs the executable block without any instrumentation or error handling. Useful for debugging and testing



138
139
140
# File 'lib/karafka/pro/recurring_tasks/task.rb', line 138

def execute
  @executable.call
end

#next_timeEtOrbi::EoTime

Returns next execution time.

Returns:

  • (EtOrbi::EoTime)

    next execution time



97
98
99
# File 'lib/karafka/pro/recurring_tasks/task.rb', line 97

def next_time
  @cron.next_time(@previous_time.to_i.zero? ? @start_time : @previous_time)
end

#to_hHash

Returns hash version of the task. Used for contract validation.

Returns:

  • (Hash)

    hash version of the task. Used for contract validation.



148
149
150
151
152
153
154
155
156
157
# File 'lib/karafka/pro/recurring_tasks/task.rb', line 148

def to_h
  {
    id: id,
    cron: @cron.original,
    previous_time: previous_time,
    next_time: next_time,
    changed: changed?,
    enabled: enabled?
  }
end

#triggerObject

Triggers the execution of this task at the earliest opportunity



91
92
93
94
# File 'lib/karafka/pro/recurring_tasks/task.rb', line 91

def trigger
  touch
  @trigger = true
end