Class: ChoresKit::Chore
- Inherits:
-
Object
- Object
- ChoresKit::Chore
- Defined in:
- lib/chores_kit/chore.rb
Constant Summary collapse
- DEFAULT_NOTIFICATIONS =
[:failed, :successful].freeze
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#tasks ⇒ Object
readonly
Returns the value of attribute tasks.
Instance Method Summary collapse
-
#description(string) ⇒ Object
Metadata.
-
#initialize(name) ⇒ Chore
constructor
A new instance of Chore.
-
#notify(*options, &block) ⇒ Object
After-run callbacks.
- #retry_failed(options) ⇒ Object
- #run(task, options = {}) ⇒ Object
- #schedule(options) ⇒ Object
-
#task(options, &block) ⇒ Object
Tasks and dependencies.
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/chores_kit/chore.rb', line 8 def name @name end |
#tasks ⇒ Object (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(*options, &block) ⇒ Object
After-run callbacks
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/chores_kit/chore.rb', line 87 def notify(*, &block) raise "Couldn't create notifications without a block" unless block_given? conditions = * conditions = DEFAULT_NOTIFICATIONS if .empty? 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() raise "Couldn't parse retry interval from attributes" unless [:wait].nil? || [:wait].is_a?(AS::Duration) wait = [:wait] || 1.second retries = [:retries] || 1 [:retry_failed] = { wait: wait, retries: retries } end |
#run(task, options = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/chores_kit/chore.rb', line 62 def run(task, = {}) from = [:triggered_by] || [:upstream] || task to = [:triggers] || [:downstream] || task tasks = @tasks.map(&:name) direction = [:upstream] || [:triggered_by] ? 'upstream' : 'downstream' # Throw an error if either up- or downstream task doesn't exist non_existing_tasks = ([from, to] - tasks).uniq raise "Couldn't set #{direction} dependency for non-existing task #{non_existing_tasks.first}" if non_existing_tasks.any? # Throw an error if unsupported dependencies are set raise "Multiple upstream tasks aren't supported" if from.is_a?(Array) raise "Multiple downstream tasks aren't supported" if to.is_a?(Array) # Skip any processing if there is just one task return if tasks.one? v1 = @dag.vertices.detect { |vertex| vertex[:name] == from } v2 = @dag.vertices.detect { |vertex| vertex[:name] == to } @dag.add_edge(from: v1, to: v2) 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() raise "Couldn't parse start time from attributes" if [:at].nil? raise "Couldn't parse interval from attributes" unless [:every].nil? || [:every].is_a?(AS::Duration) at_ltz = Time.parse([:at]) || Time.now at_utc = Time.utc(*at_ltz) || Date.today.to_time.utc [:schedule] = { at: at_utc, every: [:every] } end |
#task(options, &block) ⇒ Object
Tasks and dependencies
53 54 55 56 57 58 59 60 |
# File 'lib/chores_kit/chore.rb', line 53 def task(, &block) name, params = * raise "Couldn't create task without a name" if name.nil? raise "Couldn't create task without a block" unless block_given? @dag.add_vertex(name: name, task: Task.new(name, params, &block)) end |