Project

General

Profile

« Previous | Next » 

Revision 5da1960c

Added by Marc Dequènes almost 14 years ago

  • ID 5da1960cb46be0feaa153f365256dbf0dd7967ef

[evol] Task DSL: rework to use the same Task objet for the same task (in order to have a proper count of tasks and be able to transport information easily through steps)

View differences:

bin/test_client
include BotNet
def start_work
# to test the closure through the inner instance_eval calls
toto = true
task "compare stuff" do
ask "MapMaker", :info1, "/_cyborg_"
ask "Librarian", :info2, "/_cyborg_"
......
puts "PLOUF"
pp errors
pp results
stop_bot :at_once
end
on_success do
puts "OK"
......
on_success do
puts "OK compare stuff"
end
end
on_error do
stop_bot :at_once
on_error do
stop_bot :at_once
end
end
end
lib/cyborghood/cyborg/dsl.rb
module CyborgHood
module DSL
class BaseDSL < ActiveSupport::BasicObject
def initialize(&block)
_load_block(&block)
_start_dsl
end
reveal :class
reveal :logger
protected
def _load_block(&block)
self.instance_eval(&block)
end
def _start_dsl
end
end
class Task < BaseDSL
......
@@task_wip == 0
end
def initialize(bot, name = nil, previous_step_data = {}, &block)
def initialize(bot, name = nil, &block)
@bot = bot
@name = name
@@task_wip += 1
@errors = (previous_step_data[:errors] || {}).freeze
@results = (previous_step_data[:results] || {}).freeze
@dsl_runing = false
@subtasks = []
@cancel_on_start_error = false
@cancel_run = false
@error_cb = nil
@success_cb = nil
@errors = {}.freeze
@results = {}.freeze
@notification_name = "task/#{@name}"
begin
super(&block)
rescue
logger.error "Task '#{@name}': error in DSL: " + $!.to_s
logger.debug $!.backtrace.join("\n")
@cancel_run = true
end
_start_dsl &block
end
def run_dsl
end
# may return a Hash of results
......
# avoid race: no subtask will be run in this task now
@dsl_runing = false
next_step_data = {
:errors => {},
:results => {}
}
# compute step result
@errors = {}
@results = {}
@subtasks.each do |subtask|
next_step_data[:errors][subtask.name] = subtask.errors unless subtask.errors.empty?
next_step_data[:results].merge!(subtask.results)
@errors[subtask.name] = subtask.errors unless subtask.errors.empty?
@results.merge!(subtask.results)
end
@errors.freeze
@result.freeze
cb = next_step_data[:errors].empty? ? @success_cb : @error_cb
# next step
cb = @errors.empty? ? @success_cb : @error_cb
if cb
self.class.new(@bot, @name, next_step_data, &cb)
_start_dsl(&cb)
else
logger.debug "Task '#{@name}': finished"
@@task_wip -= 1
logger.debug "Task '#{@name}': finished (#{@@task_wip} remaining)"
end
@@task_wip -= 1
end
def _notification_criterias_match(msg, criterias)
......
true
end
def _start_dsl
return false if @cancel_run
def _start_dsl(&block)
@dsl_runing = false
@subtasks = []
@cancel_on_start_error = false
@error_cb = nil
@success_cb = nil
begin
instance_eval(&block)
rescue
logger.error "Task '#{@name}': error in DSL: " + $!.to_s
logger.debug $!.backtrace.join("\n")
return
end
logger.debug "Task '#{@name}': begining step"

Also available in: Unified diff