Project

General

Profile

« Previous | Next » 

Revision 09b29d2c

Added by Marc Dequènes almost 14 years ago

  • ID 09b29d2ca895315082be96521c37365f9c52e7e9

[evol] Task: implement loading step from file and a store to help communicate between files

View differences:

lib/cyborghood/cyborg/task.rb
module CyborgHood
module TaskAspect
def task(name, &block)
def task(name, options = {}, &block)
the_bot = self.is_a?(Cyborg) ? self : self.bot
DSL::Task.new(the_bot, name, &block)
DSL::Task.new(the_bot, name, options, &block)
end
def schedule_task(callback = nil, &task)
......
module DSL
class TaskBase
attr_reader :bot, :name, :errors, :results, :preferred_locales, :locale, :user
attr_reader :bot, :name, :errors, :results, :preferred_locales, :locale, :user, :store
include I18nTranslation
......
end
# the name MUST be unique
def initialize(bot, name, &block)
def initialize(bot, name, options = {}, &block)
@bot = bot
@name = name
......
@locale = nil
@user = nil
@store = OpenStruct.new
_setup
_start_dsl &block
step = {:options => options, :cb => block}
step_code = get_step_code(step)
_start_dsl(step_code)
end
def set_preferred_locales(prefs)
......
@cancel_on_start_error = true
end
def on_error(&cb)
@error_cb = cb
def on_error(options = {}, &cb)
@error_step = {:options => options, :cb => cb}
end
def on_success(&cb)
@success_cb = cb
def on_success(options = {}, &cb)
@success_step = {:options => options, :cb => cb}
end
def stop_bot(condition)
......
subtasks_running.empty?
end
def get_step_code(step)
return if step.nil?
cb = step[:cb]
return cb unless cb.nil?
file = step[:options][:file]
if file.nil?
logger.error "Task '#{@name}': error in DSL: neither block nor file provided for next step definition"
return
end
begin
step_code = File.read(File.join(self.base_lpath, file + ".rb"))
rescue
logger.error "Task '#{@name}': error in DSL: cannot open step definition file: " + $!.message
return
end
return step_code
end
def _check_finished
return unless _subtasks_finished?
......
@result.freeze
# next step
cb = @errors.empty? ? @success_cb : @error_cb
if cb
step = @errors.empty? ? @success_step : @error_step
step_code = get_step_code(step)
if step_code
logger.debug "Task '#{@name}': step result: " + (@errors.empty? ? "success" : "error")
_start_dsl(&cb)
_start_dsl(step_code)
else
_finished
end
......
logger.debug "Task '#{@name}': created"
end
def _start_dsl(&block)
def base_lpath
File.join(Config::LIB_DIR, 'cyborghood-' + @bot.model.downcase, 'tasks')
end
def _start_dsl(step_code)
@dsl_runing = false
@subtasks = []
@cancel_on_start_error = false
@error_cb = nil
@success_cb = nil
@error_step = nil
@success_step = nil
begin
instance_eval(&block)
if step_code.is_a? String
instance_eval step_code
else
instance_eval &step_code
end
rescue
logger.error "Task '#{@name}': error in DSL: " + $!.to_s
logger.debug $!.backtrace.join("\n")

Also available in: Unified diff