Class: ChoresKit::Chore

Inherits:
Object
  • Object
show all
Defined in:
lib/chores_kit/chore.rb

Constant Summary collapse

DEFAULT_NOTIFICATIONS =
[:failed, :successful].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Chore

Returns a new instance of Chore.



12
13
14
15
16
17
18
19
20
# File 'lib/chores_kit/chore.rb', line 12

def initialize(name)
  @name = name
   = {}

  @dag = DAG.new
  @tasks = @dag.vertices

  @notifications = {}
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/chores_kit/chore.rb', line 8

def name
  @name
end

#tasksObject (readonly)

Returns the value of attribute tasks.



8
9
10
# File 'lib/chores_kit/chore.rb', line 8

def tasks
  @tasks
end

Instance Method Details

#description(string) ⇒ Object

Metadata



23
24
25
# File 'lib/chores_kit/chore.rb', line 23

def description(string)
  [:description] = string
end

#notify(conditions, &block) ⇒ Object

After-run callbacks



65
66
67
68
69
70
71
72
# File 'lib/chores_kit/chore.rb', line 65

def notify(conditions, &block)
  conditions.each do |condition|
    notification = Notification.new(condition)
    notification.instance_eval(&block)

    @notifications[condition] = notification
  end
end

#retry_failed(options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/chores_kit/chore.rb', line 40

def retry_failed(options)
  raise "Couldn't parse retry interval from attributes" unless options[:wait].nil? || options[:wait].is_a?(AS::Duration)

  wait = options[:wait] || 1.second
  retries = options[:retries] || 1

  [:retry_failed] = {
    wait: wait,
    retries: retries
  }
end

#run(task, options) ⇒ Object



59
60
61
62
# File 'lib/chores_kit/chore.rb', line 59

def run(task, options)
  from = options[:triggered_by] || options[:upstream] || task
  to = options[:triggers] || options[:downstream] || task
end

#schedule(options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/chores_kit/chore.rb', line 27

def schedule(options)
  raise "Couldn't parse start time from attributes" if options[:at].nil?
  raise "Couldn't parse interval from attributes" unless options[:every].nil? || options[:every].is_a?(AS::Duration)

  at_ltz = Time.parse(options[:at]) || Time.now
  at_utc = Time.utc(*at_ltz) || Date.today.to_time.utc

  [:schedule] = {
    at:    at_utc,
    every: options[:every]
  }
end

#task(options, &block) ⇒ Object

Tasks and dependencies



53
54
55
56
57
# File 'lib/chores_kit/chore.rb', line 53

def task(options, &block)
  name, params = *options

  @dag.add_vertex(task: Task.new(name, params, &block))
end