Class: Roby::Application
- Extended by:
- Logger::Forward, Logger::Hierarchy
- Defined in:
- lib/roby/app.rb
Overview
There is one and only one Application object, which holds mainly the system-wide configuration and takes care of file loading and system-wide setup (#setup). A Roby application can be started in multiple modes. The first and most important mode is the runtime mode (scripts/run). Other modes are the testing mode (#testing? returns true, entered through scripts/test) and the shell mode (#shell? returns true, entered through scripts/shell). Usually, user code does not have to take the modes into account, but it is sometime useful.
Finally, in both testing and runtime mode, the code can be started in simulation or live setups (see #simulation?). Specific plugins can for instance start and set up a simulation system in simulation mode, and as well set up some simulation-specific configuration for the functional layer of the architecture.
Configuration files
In all modes, a specific set of configuration files are loaded. The files that are actually loaded are defined by the robot name and type, as specified to #robot. The loaded files are, in order, the following:
- config/app.yml
-
the application configuration as a YAML file. See the comments in that file for more details.
- config/init.rb
-
Ruby code for the common configuration of all robots
- config/ROBOT_NAME.rb or config/ROBOT_TYPE.rb
-
Ruby code for the configuration of either all robots of the same type, or a specific robot. It is one or the other. If a given robot needs to inherit the configuration of its type, explicitely require the ROBOT_TYPE.rb file in config/ROBOT_NAME.rb.
Runtime mode (scripts/run)
Then, in runtime mode the robot controller controller/ROBOT_NAME.rb or controller/ROBOT_TYPE.rb is loaded. The same rules than for the configuration file config/ROBOT_NAME.rb apply.
Testing mode (scripts/test)
This mode is used to run test suites in the test directory. See Roby::Test::TestCase for a description of Roby-specific tests.
Plugin Integration
Plugins are integrated by providing methods that get called during setup and teardown of the application. It is therefore important to understand the order in which methods get called, and where the plugins can ‘plug-in’ this process.
On setup, the following methods are called:
-
load base configuration files. app.yml and init.rb
-
load_base_config hook
-
set up directories (log dir, …) and loggers
-
set up singletons
-
base_setup hook
-
setup hook. The difference is that the setup hook is called only if #setup is called. base_setup is always called.
-
load models in models/tasks
-
require_models hook
-
load models in models/planners and models/actions
-
require_planners hook
-
load additional model files
-
finalize_model_loading hook
-
load config file config/ROBOT.rb
-
require_config hook
-
setup main planner
-
setup testing if in testing mode
-
setup shell interface
Defined Under Namespace
Classes: ActionResolutionError, InvalidLoggerName, InvalidRobyAppDirEnv, LogDirNotInitialized, NoCurrentLog, NoSuchRobot, NotInCurrentApp, PluginsDisabled
Constant Summary collapse
- DEFAULT_OPTIONS =
{ 'log' => Hash['events' => true, 'server' => true, 'levels' => Hash.new, 'filter_backtraces' => true], 'discovery' => Hash.new, 'engine' => Hash.new }
Instance Attribute Summary collapse
-
#action_handlers ⇒ Array<#call>
readonly
List of blocks that should be executed once the application is started.
-
#additional_model_files ⇒ Array<String>
readonly
List of paths to files not in models/ that contain some models.
-
#app_dir ⇒ String?
Returns the application base directory.
-
#app_extra_metadata ⇒ Object
readonly
Additional metadata saved in log_dir/info.yml by the app.
-
#app_name ⇒ Object
Returns the name of the application.
-
#argv_set ⇒ Object
readonly
The –set options passed on the command line.
-
#auto_load_models ⇒ Boolean
writeonly
Controls whether Roby should load the available the model files automatically in #require_models.
-
#available_plugins ⇒ Object
readonly
A [name, dir, file, module] array of available plugins, where ‘name’ is the plugin name, ‘dir’ the directory in which it is installed, ‘file’ the file which should be required to load the plugin and ‘module’ the Application-compatible module for configuration of the plug-in.
-
#cleanup_handlers ⇒ Array<#call>
readonly
List of objects called when the app cleans up (it is the opposite of setup).
-
#clear_models_handlers ⇒ Array<#call>
readonly
List of objects called when the app is doing #clear_models.
-
#controllers ⇒ Array<#call>
readonly
List of blocks that should be executed once the application is started.
-
#created_log_base_dirs ⇒ Array<String>
readonly
The list of directories created by this app in the paths to #created_log_dirs.
-
#created_log_dirs ⇒ Array<String>
readonly
The list of log directories created by this app.
-
#filter_out_patterns ⇒ Object
readonly
Array of regular expressions used to filter out backtraces.
-
#init_handlers ⇒ Array<#call>
readonly
List of objects called when the app gets initialized (i.e. just after init.rb is loaded).
-
#log_server_pid ⇒ Integer?
readonly
The PID of the server that gives access to the log file.
-
#log_server_port ⇒ Integer?
readonly
The port on which the log server is started.
-
#module_name ⇒ Object
Returns the name of this app’s toplevel module.
-
#notification_listeners ⇒ #call
readonly
The blocks that listen to notifications.
-
#options ⇒ Object
readonly
Applicatio configuration information is stored in a YAML file config/app.yml.
-
#plan ⇒ ExecutablePlan
readonly
The main plan on which this application acts.
-
#planners ⇒ Array
readonly
A set of planners declared in this application.
-
#plugins ⇒ Object
readonly
An [name, module] array of the loaded plugins.
-
#registered_exceptions ⇒ Array<(Exception,String)>
readonly
A set of exceptions that have been encountered by the application The associated string, if given, is a hint about in which context this exception got raised.
-
#require_handlers ⇒ Array<#call>
readonly
List of objects called when the app gets to require its models (i.e. after #require_models).
-
#rest_interface_host ⇒ String
The host to which the REST interface server should bind.
-
#rest_interface_port ⇒ Integer
The port on which the REST interface server should be.
-
#search_path ⇒ Array<String>
The list of paths in which the application should be looking for files.
-
#setup_handlers ⇒ Array<#call>
readonly
List of objects called when the app gets initialized (i.e. in #setup after #base_setup).
-
#shell_interface ⇒ Interface
readonly
The Interface bound to this app.
-
#shell_interface_host ⇒ String
The host to which the shell interface server should bind.
-
#shell_interface_port ⇒ Integer
The port on which the shell interface server should be.
-
#ui_event_listeners ⇒ #call
readonly
The blocks that listen to ui events.
Class Method Summary collapse
-
.attr_config(config_key) ⇒ Object
Allows to attribute configuration keys to override configuration parameters stored in config/app.yml.
-
.common_optparse_setup(parser) ⇒ Object
Defines common configuration options valid for all Roby-oriented scripts.
- .find_data(*name) ⇒ Object
-
.guess_app_dir ⇒ String?
Guess the app directory based on the current directory.
-
.host_options(parser, options) ⇒ Object
Sets up provided option parser to add the –host and –vagrant option.
-
.is_app_dir?(test_dir) ⇒ Boolean
Tests if the given directory looks like the root of a Roby app.
-
.overridable_configuration(config_set, config_key, options = Hash.new) ⇒ Object
Defines accessors for a configuration parameter stored in #options.
-
.read_current_dir(current_path) ⇒ Object
private
Read and validate the ‘current’ dir by means of the ‘current’ symlink that Roby maintains in its log base directory.
- .register_plugin(name, mod, &init) ⇒ Object
-
.unique_dirname(base_dir, path_spec, date_tag = nil) ⇒ Object
Returns a unique directory name as a subdirectory of
base_dir, based onpath_spec.
Instance Method Summary collapse
-
#abort_on_application_exception=(flag) ⇒ Object
Controls whether the Roby app should quit if an application (i.e. non-plan) exception is received.
-
#abort_on_application_exception? ⇒ Object
Controls whether the Roby app should quit if an application (i.e. non-plan) exception is received.
-
#abort_on_exception=(flag) ⇒ Object
Controls whether the application should quit if an unhandled plan exception is received.
-
#abort_on_exception? ⇒ Object
Controls whether the application should quit if an unhandled plan exception is received.
-
#action_from_model(model) ⇒ Actions::Models::Action
Find an action on the planning interface that can generate the given task model.
-
#action_from_name(name) ⇒ Actions::Models::Action
Finds the action matching the given name.
-
#actions(&block) ⇒ Object
Declares that the following block should be used to setup the main action interface.
-
#add_app_metadata(metadata) ⇒ Object
Add some metadata to #app_metadata, and save it to the log dir’s info.yml if it is already created.
- #add_plugin(name, mod) ⇒ Object
-
#app_file?(path) ⇒ Boolean
Returns true if the given path points to a file in the Roby app.
-
#app_metadata ⇒ Object
Metadata used to describe the app.
-
#app_module ⇒ Object
Returns this app’s toplevel module.
- #app_path ⇒ Object
-
#apply_config(config) ⇒ Object
private
Sets relevant configuration values from a configuration hash.
-
#apply_config_interface(host_port) ⇒ Object
private
Parses and applies the ‘interface’ value from a configuration hash.
-
#auto_load_all=(flag) ⇒ Boolean
Controls whether Roby’s auto-load feature should load all models in #search_path or only the ones in #app_dir.
-
#auto_load_all? ⇒ Boolean
Controls whether Roby’s auto-load feature should load all models in #search_path or only the ones in #app_dir.
-
#auto_load_models? ⇒ Boolean
Controls whether Roby should load the available the model files automatically in #require_models.
-
#auto_load_search_path ⇒ Array<String>
The search path for the auto-load feature.
- #auto_require_models ⇒ Object
- #auto_require_planners ⇒ Object
-
#autodiscover_tests_in?(path) ⇒ Boolean
Hook for the plugins to filter out some paths that should not be auto-loaded by #each_test_file_in_app.
-
#automatic_testing=(flag) ⇒ Object
True if user interaction is disabled during tests.
-
#automatic_testing? ⇒ Object
True if user interaction is disabled during tests.
-
#backward_compatible_naming=(flag) ⇒ Object
If set to true, the app will enable backward-compatible behaviour related to naming schemes, file placements and so on.
-
#backward_compatible_naming? ⇒ Object
If set to true, the app will enable backward-compatible behaviour related to naming schemes, file placements and so on.
-
#base_cleanup ⇒ Object
The inverse of #base_setup.
- #base_setup ⇒ Object
-
#call_plugins(method, *args, deprecated: nil) ⇒ Object
Call
methodon each loaded extension module which define it, with argumentsargs. -
#cleanup ⇒ Object
The inverse of #setup.
- #clear_config ⇒ Object
- #clear_exceptions ⇒ Object
-
#clear_model?(m) ⇒ Boolean
Whether this model should be cleared in #clear_models.
-
#clear_models ⇒ Object
Clear all models for which #clear_model? returns true.
-
#controller(&block) ⇒ Object
Declares that the following block should be used as the robot controller.
-
#created_log_dir? ⇒ Boolean
Test whether this app already created its log directory.
- #define_actions_module ⇒ Object
- #define_main_planner_if_needed ⇒ Object
-
#defined_plugin?(name) ⇒ Boolean
True if
nameis a plugin known to us. -
#definition_file_for(model) ⇒ Object
Returns the downmost app file that was involved in the given model’s definition.
-
#development_mode? ⇒ Object
Whether the app should run in development mode.
-
#discover_test_files(all: true, only_self: false) ⇒ Array<String>
Discover which tests should be run, and require them.
-
#each_model(root_model = nil) {|| ... } ⇒ Object
Enumerate all models registered in this app.
-
#each_notification_listener {|the| ... } ⇒ Object
Enumerates the listeners currently registered through #on_notification.
-
#each_plugin(on_available = false) ⇒ Object
Enumerates all available plugins, yielding only the plugin module (i.e. the plugin object itself).
-
#each_responding_plugin(method, on_available = false) ⇒ Object
Yields each plugin object that respond to
method. -
#each_test_file_for_loaded_models {|path, models| ... } ⇒ Object
Enumerate the test files that should be run to test the current app configuration.
-
#each_test_file_in_app ⇒ Object
Enumerate all the test files in this app and for this robot configuration.
-
#each_ui_event_listener {|the| ... } ⇒ Object
Enumerates the listeners currently registered through #on_ui_event.
-
#execution_engine ⇒ ExecutionEngine?
The engine associated with #plan.
-
#find_action_from_name(name) ⇒ (ActionInterface,Actions::Models::Action)?
Find an action with the given name on the action interfaces registered on #planners.
-
#find_and_create_log_dir(time_tag = self.time_tag) ⇒ String
Create a log directory for the given time tag, and make it this app’s log directory.
-
#find_base_path_for(path) ⇒ nil, String
Returns the path in search_path that contains the given file or path.
- #find_data(*name) ⇒ Object
-
#find_dir(*args) ⇒ Object
Returns the first match from #find_dirs, or nil if nothing matches.
-
#find_files_in_dirs(*path, options) ⇒ Array<String>
Enumerates the subdirectories of paths in #search_path matching the given path.
-
#find_file(*args) ⇒ Object
Returns the first match from #find_files, or nil if nothing matches.
-
#find_files(*path, options) ⇒ Array<String>
Enumerates files based on their relative paths in #search_path.
-
#find_files_in_dirs(*path, options) ⇒ Array<String>
Enumerates the files that are present in subdirectories of paths in #search_path.
-
#framework_file?(path) ⇒ Boolean
Tests whether a path is within a framework library.
-
#guess_app_dir ⇒ String
Guess the app directory based on the current directory, and sets #app_dir.
-
#has_app? ⇒ Boolean
Whether there is a supporting app directory.
-
#ignore_all_load_errors=(flag) ⇒ Object
If set to true, files that generate errors while loading will be ignored.
-
#ignore_all_load_errors? ⇒ Object
If set to true, files that generate errors while loading will be ignored.
-
#initialize ⇒ Application
constructor
A new instance of Application.
- #isolate_load_errors(message, logger = Application, level = :warn) ⇒ Object
- #join ⇒ Object
- #load_all_model_files_in(prefix_name, ignored_exceptions: Array.new) ⇒ Object
-
#load_base_config ⇒ Object
Loads the base configuration.
- #load_config_yaml ⇒ Object
-
#load_default_models ⇒ Object
Helper to the robot config files to load the root files in models/ (e.g. models/tasks.rb).
-
#load_plugin_file(appfile) ⇒ Object
Load the given Roby plugin file.
-
#load_plugins_from_prefix(dir) ⇒ Object
Looks into subdirectories of
dirfor files called app.rb and registers them as Roby plugins. -
#loaded_plugin?(name) ⇒ Boolean
Returns true if
nameis a loaded plugin. -
#log ⇒ Object
:method: log_server=.
-
#log_base_dir ⇒ Object
The base directory in which logs should be saved.
-
#log_base_dir=(dir) ⇒ Object
Sets the directory under which logs should be created.
-
#log_create_current=(flag) ⇒ Object
If set to true, this Roby application will create a ‘current’ entry in #log_base_dir that points to the latest log directory.
-
#log_create_current? ⇒ Object
If set to true, this Roby application will create a ‘current’ entry in #log_base_dir that points to the latest log directory.
-
#log_current_dir ⇒ Object
The path to the current log directory.
-
#log_current_file ⇒ Object
The path to the current log file.
-
#log_dir ⇒ Object
The directory in which logs are to be saved Defaults to app_dir/data/$time_tag.
-
#log_dir=(dir) ⇒ Object
Explicitely set the log directory.
-
#log_read_metadata ⇒ Object
Read the time tag from the current log directory.
- #log_read_time_tag ⇒ Object
-
#log_save_metadata(append: true) ⇒ Object
Save #app_metadata in the log directory.
-
#log_setup(mod_path, level, file = nil) ⇒ Object
Configures a text logger in the system.
-
#main_action_interface ⇒ Object
Returns this app’s main action interface.
-
#make_path_relative(path) ⇒ Object
Transforms
pathinto a path relative to an entry insearch_path(usually the application root directory). -
#model_defined_in_app?(model) ⇒ Boolean
Tests whether a model class has been defined in this app’s code.
- #modelling_only ⇒ Object
-
#needs_to_be_in_current_app(allowed_outside: true) ⇒ Object
Call to check whether the current directory is within #app_dir.
-
#notify(source, level, message) ⇒ Object
Sends a message to all notification listeners.
-
#on_cleanup(&block) ⇒ Object
Declares that the following block should be called when #clear_models is called.
-
#on_clear_models(&block) ⇒ Object
Declares that the following block should be called when #clear_models is called.
-
#on_config(&block) ⇒ Object
deprecated
Deprecated.
use #on_setup instead
-
#on_init(&block) ⇒ Object
Declares a block that should be executed when the Roby app gets initialized (i.e. just after init.rb gets loaded).
-
#on_notification {|source, level, message| ... } ⇒ Object
Registers a block to be called when a message needs to be dispatched from #notify.
-
#on_require(&block) ⇒ Object
Declares a block that should be executed when the Roby app loads models (i.e. in #require_models).
-
#on_setup(&block) ⇒ Object
Declares a block that should be executed when the Roby app is begin setup.
-
#on_ui_event {|name, args| ... } ⇒ Object
Registers a block to be called when a message needs to be dispatched from #ui_event.
-
#plugin_definition(name) ⇒ Object
Returns the [name, dir, file, module] array definition of the plugin
name, or nil ifnameis not a known plugin. -
#plugins_enabled=(flag) ⇒ Object
True if plugins should be discovered, registered and loaded (true by default).
-
#plugins_enabled? ⇒ Object
True if plugins should be discovered, registered and loaded (true by default).
-
#prepare ⇒ Object
Prepares the environment to actually run.
-
#prepare_action(name, mission: false, **arguments) ⇒ Object
Generate the plan pattern that will call the required action on the planning interface, with the given arguments.
- #prepare_event_log ⇒ Object private
-
#public_logs=(flag) ⇒ Object
If set to true, this Roby application will make its logs public, i.e.
-
#public_logs? ⇒ Object
If set to true, this Roby application will make its logs public, i.e.
-
#public_rest_interface=(flag) ⇒ Object
If set to true, this Roby application will publish a Interface::REST::API object.
-
#public_rest_interface? ⇒ Object
If set to true, this Roby application will publish a Interface::REST::API object.
-
#public_shell_interface=(flag) ⇒ Object
If set to true, this Roby application will publish a Interface::Interface object as a TCP server.
-
#public_shell_interface? ⇒ Object
If set to true, this Roby application will publish a Interface::Interface object as a TCP server.
- #register_exception(e, reason = nil) ⇒ Object
- #register_plugins(force: false) ⇒ Object
-
#register_server(name, port) ⇒ Object
Register a server port that can be discovered later.
-
#reload_actions ⇒ Object
Reload action models defined in models/actions/.
-
#reload_config ⇒ Object
Reload files in config/.
-
#reload_models ⇒ Object
Reload model files in models/.
- #reload_planners ⇒ Object
-
#remove_notification_listener(listener) ⇒ Object
Removes a notification listener added with #on_notification.
-
#remove_ui_event_listener(listener) ⇒ Object
Removes a notification listener added with #on_ui_event.
- #require(absolute_path) ⇒ Object
-
#require_app_dir(needs_current: false, allowed_outside: true) ⇒ Object
Call to require this roby application to be in a Roby application.
-
#require_models ⇒ Object
Loads the models, based on the given robot name and robot type.
-
#require_planners ⇒ Object
Loads the planner models.
- #require_robot_file ⇒ Object
-
#reset_log_dir ⇒ Object
Reset the current log dir so that #setup picks a new one.
-
#reset_plan(plan = ExecutablePlan.new) ⇒ Object
Reset the plan to a new Plan object.
-
#restart(*cmdline) ⇒ Object
Quits this app and replaces with a new one after a proper cleanup.
-
#restarting? ⇒ Boolean
Whether #run should exec a new process on quit or not.
-
#robot(name, type = nil) ⇒ Object
Sets up the name and type of the robot.
-
#robot_name ⇒ String?
The robot name.
-
#robot_name?(name) ⇒ Boolean
Test if the given name is a valid robot name.
-
#robot_type ⇒ String?
The robot type.
-
#robots ⇒ App::RobotNames
The robot names configuration.
-
#root_models ⇒ Array<#each_submodel>
The list of model classes that allow to discover all models in this app.
- #run(thread_priority: 0, &block) ⇒ Object
-
#run_plugins(mods, &block) ⇒ Object
Helper for Application#run to call the plugin’s run or start methods while guaranteeing the system’s cleanup.
-
#running? ⇒ Boolean
Whether we’re inside #run.
-
#self_file?(path) ⇒ Boolean
Returns true if the given path points to a file under #app_dir.
-
#setup ⇒ Object
Does basic setup of the Roby environment.
- #setup_for_minimal_tooling ⇒ Object
-
#setup_loggers(ignore_missing: false, redirections: true) ⇒ Object
Sets up all the default loggers.
-
#setup_rest_interface ⇒ Object
Publishes a REST API.
- #setup_robot_names_from_config_dir ⇒ Object
-
#setup_shell_interface ⇒ Object
Publishes a shell interface.
- #shell ⇒ Object
-
#shutdown ⇒ Object
The inverse of #prepare.
- #simulation ⇒ Object
- #single ⇒ Object
- #start_log_server(logfile, options = Hash.new) ⇒ Object
- #stop ⇒ Object
- #stop_log_server ⇒ Object
-
#stop_rest_interface(join: false) ⇒ Object
Stops a running REST interface.
-
#stop_shell_interface ⇒ Object
Stops a running shell interface.
-
#test_files_for(model) ⇒ Object
Given a model class, returns the full path of an existing test file that is meant to verify this model.
- #testing ⇒ Object
-
#time_tag ⇒ Object
The time tag.
-
#ui_event(name, *args) ⇒ Object
Sends a message to all UI event listeners.
-
#unload_features(*pattern) ⇒ Object
Ensure tha require’d files that match the given pattern can be re-required.
- #update_load_path ⇒ Object
-
#using(*names, force: false) ⇒ Object
Loads the plugins whose name are listed in
names.
Constructor Details
#initialize ⇒ Application
Returns a new instance of Application.
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 |
# File 'lib/roby/app.rb', line 660 def initialize @plan = ExecutablePlan.new @argv_set = Array.new @auto_load_all = false @default_auto_load = true @auto_load_models = nil @app_name = nil @module_name = nil @app_dir = nil @backward_compatible_naming = true @development_mode = true @search_path = nil @plugins = Array.new @plugins_enabled = true @available_plugins = Array.new = DEFAULT_OPTIONS.dup @public_logs = false @log_create_current = true @created_log_dirs = [] @created_log_base_dirs = [] @additional_model_files = [] @restarting = false @shell_interface = nil @shell_interface_host = nil @shell_interface_port = Interface::DEFAULT_PORT @shell_abort_on_exception = true @rest_interface = nil @rest_interface_host = nil @rest_interface_port = Interface::DEFAULT_REST_PORT @automatic_testing = true @registered_exceptions = [] = Hash.new @filter_out_patterns = [Roby::RX_IN_FRAMEWORK, Roby::RX_IN_METARUBY, Roby::RX_IN_UTILRB, Roby::RX_REQUIRE] self.abort_on_application_exception = true @planners = [] @notification_listeners = Array.new @ui_event_listeners = Array.new @init_handlers = Array.new @setup_handlers = Array.new @require_handlers = Array.new @clear_models_handlers = Array.new @cleanup_handlers = Array.new @controllers = Array.new @action_handlers = Array.new end |
Instance Attribute Details
#action_handlers ⇒ Array<#call> (readonly)
Returns list of blocks that should be executed once the application is started.
493 494 495 |
# File 'lib/roby/app.rb', line 493 def action_handlers @action_handlers end |
#additional_model_files ⇒ Array<String> (readonly)
Returns list of paths to files not in models/ that contain some models. This is mainly used by the command-line tools so that the user can load separate “model-based scripts” files.
465 466 467 |
# File 'lib/roby/app.rb', line 465 def additional_model_files @additional_model_files end |
#app_dir ⇒ String?
Returns the application base directory
276 277 278 279 280 281 282 |
# File 'lib/roby/app.rb', line 276 def app_dir if defined?(APP_DIR) APP_DIR elsif @app_dir @app_dir end end |
#app_extra_metadata ⇒ Object (readonly)
Additional metadata saved in log_dir/info.yml by the app
Do not modify directly, use #add_app_metadata instead
515 516 517 |
# File 'lib/roby/app.rb', line 515 def end |
#app_name ⇒ Object
Returns the name of the application
237 238 239 240 241 242 243 244 |
# File 'lib/roby/app.rb', line 237 def app_name if @app_name @app_name elsif app_dir @app_name = File.basename(app_dir).gsub(/[^\w]/, '_') else 'default' end end |
#argv_set ⇒ Object (readonly)
The –set options passed on the command line
137 138 139 |
# File 'lib/roby/app.rb', line 137 def argv_set @argv_set end |
#auto_load_models=(flag) ⇒ Boolean (writeonly)
Controls whether Roby should load the available the model files automatically in #require_models
2370 2371 2372 |
# File 'lib/roby/app.rb', line 2370 def auto_load_models=(value) @auto_load_models = value end |
#available_plugins ⇒ Object (readonly)
A [name, dir, file, module] array of available plugins, where ‘name’ is the plugin name, ‘dir’ the directory in which it is installed, ‘file’ the file which should be required to load the plugin and ‘module’ the Application-compatible module for configuration of the plug-in
424 425 426 |
# File 'lib/roby/app.rb', line 424 def available_plugins @available_plugins end |
#cleanup_handlers ⇒ Array<#call> (readonly)
Returns list of objects called when the app cleans up (it is the opposite of setup).
485 486 487 |
# File 'lib/roby/app.rb', line 485 def cleanup_handlers @cleanup_handlers end |
#clear_models_handlers ⇒ Array<#call> (readonly)
Returns list of objects called when the app is doing #clear_models.
481 482 483 |
# File 'lib/roby/app.rb', line 481 def clear_models_handlers @clear_models_handlers end |
#controllers ⇒ Array<#call> (readonly)
Returns list of blocks that should be executed once the application is started.
489 490 491 |
# File 'lib/roby/app.rb', line 489 def controllers @controllers end |
#created_log_base_dirs ⇒ Array<String> (readonly)
The list of directories created by this app in the paths to #created_log_dirs
They are deleted on cleanup if #public_logs? is false. Unlike with #created_log_dirs, they are not deleted if they are not empty.
510 511 512 |
# File 'lib/roby/app.rb', line 510 def created_log_base_dirs @created_log_base_dirs end |
#created_log_dirs ⇒ Array<String> (readonly)
The list of log directories created by this app
They are deleted on cleanup if #public_logs? is false. Unlike with #created_log_base_dirs, they are deleted even if they are not empty.
501 502 503 |
# File 'lib/roby/app.rb', line 501 def created_log_dirs @created_log_dirs end |
#filter_out_patterns ⇒ Object (readonly)
Array of regular expressions used to filter out backtraces
576 577 578 |
# File 'lib/roby/app.rb', line 576 def filter_out_patterns @filter_out_patterns end |
#init_handlers ⇒ Array<#call> (readonly)
Returns list of objects called when the app gets initialized (i.e. just after init.rb is loaded).
469 470 471 |
# File 'lib/roby/app.rb', line 469 def init_handlers @init_handlers end |
#log_server_pid ⇒ Integer? (readonly)
The PID of the server that gives access to the log file
Its port is allocated automatically, and must be discovered through the Roby interface
294 295 296 |
# File 'lib/roby/app.rb', line 294 def log_server_pid @log_server_pid end |
#log_server_port ⇒ Integer? (readonly)
The port on which the log server is started
It is by default started on an ephemeral port, that needs to be discovered by clients through the Roby interface’s Interface#log_server_port
303 304 305 |
# File 'lib/roby/app.rb', line 303 def log_server_port @log_server_port end |
#module_name ⇒ Object
Returns the name of this app’s toplevel module
256 257 258 |
# File 'lib/roby/app.rb', line 256 def module_name @module_name || app_name.camelcase(:upper) end |
#notification_listeners ⇒ #call (readonly)
Returns the blocks that listen to notifications. They are added with #on_notification and removed with #remove_notification_listener.
2717 2718 2719 |
# File 'lib/roby/app.rb', line 2717 def notification_listeners @notification_listeners end |
#options ⇒ Object (readonly)
Applicatio configuration information is stored in a YAML file config/app.yml. The options are saved in a hash.
This attribute contains the raw hash as read from the file. It is overlaid
118 119 120 |
# File 'lib/roby/app.rb', line 118 def end |
#plan ⇒ ExecutablePlan (readonly)
The main plan on which this application acts
101 102 103 |
# File 'lib/roby/app.rb', line 101 def plan @plan end |
#planners ⇒ Array (readonly)
A set of planners declared in this application
111 112 113 |
# File 'lib/roby/app.rb', line 111 def planners @planners end |
#plugins ⇒ Object (readonly)
An [name, module] array of the loaded plugins
426 427 428 |
# File 'lib/roby/app.rb', line 426 def plugins @plugins end |
#registered_exceptions ⇒ Array<(Exception,String)> (readonly)
A set of exceptions that have been encountered by the application The associated string, if given, is a hint about in which context this exception got raised
126 127 128 |
# File 'lib/roby/app.rb', line 126 def registered_exceptions @registered_exceptions end |
#require_handlers ⇒ Array<#call> (readonly)
Returns list of objects called when the app gets to require its models (i.e. after #require_models).
477 478 479 |
# File 'lib/roby/app.rb', line 477 def require_handlers @require_handlers end |
#rest_interface_host ⇒ String
The host to which the REST interface server should bind
637 638 639 |
# File 'lib/roby/app.rb', line 637 def rest_interface_host @rest_interface_host end |
#rest_interface_port ⇒ Integer
The port on which the REST interface server should be
641 642 643 |
# File 'lib/roby/app.rb', line 641 def rest_interface_port @rest_interface_port end |
#search_path ⇒ Array<String>
The list of paths in which the application should be looking for files
393 394 395 396 397 398 399 400 401 402 |
# File 'lib/roby/app.rb', line 393 def search_path if !@search_path if app_dir [app_dir] else [] end else @search_path end end |
#setup_handlers ⇒ Array<#call> (readonly)
Returns list of objects called when the app gets initialized (i.e. in #setup after #base_setup).
473 474 475 |
# File 'lib/roby/app.rb', line 473 def setup_handlers @setup_handlers end |
#shell_interface ⇒ Interface (readonly)
The Interface bound to this app
306 307 308 |
# File 'lib/roby/app.rb', line 306 def shell_interface @shell_interface end |
#shell_interface_host ⇒ String
The host to which the shell interface server should bind
646 647 648 |
# File 'lib/roby/app.rb', line 646 def shell_interface_host @shell_interface_host end |
#shell_interface_port ⇒ Integer
The port on which the shell interface server should be
650 651 652 |
# File 'lib/roby/app.rb', line 650 def shell_interface_port @shell_interface_port end |
#ui_event_listeners ⇒ #call (readonly)
Returns the blocks that listen to ui events. They are added with #on_ui_event and removed with #remove_ui_event.
2674 2675 2676 |
# File 'lib/roby/app.rb', line 2674 def ui_event_listeners @ui_event_listeners end |
Class Method Details
.attr_config(config_key) ⇒ Object
Allows to attribute configuration keys to override configuration parameters stored in config/app.yml
For instance,
attr_config 'log'
creates a log_overrides attribute, which contains a hash. Any value stored in this hash will override those stored in the config file. The final value can be accessed by accessing the generated #config_key method:
E.g.,
in config/app.yml:
log:
dir: test
in config/init.rb:
Roby.app.log_overrides['dir'] = 'bla'
Then, Roby.app.log will return { ‘dir’ => ‘bla }
This override mechanism is not meant to be used directly by the user, but as a tool for the rest of the framework
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/roby/app.rb', line 166 def self.attr_config(config_key) config_key = config_key.to_s # Ignore if already done return if method_defined?("#{config_key}_overrides") attribute("#{config_key}_overrides") { Hash.new } define_method(config_key) do plain = self.[config_key] || Hash.new overrides = instance_variable_get "@#{config_key}_overrides" if overrides plain.recursive_merge(overrides) else plain end end end |
.common_optparse_setup(parser) ⇒ Object
Defines common configuration options valid for all Roby-oriented scripts
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 |
# File 'lib/roby/app.rb', line 519 def self.common_optparse_setup(parser) Roby.app.load_config_yaml parser.on("--set=KEY=VALUE", String, "set a value on the Conf object") do |value| Roby.app.argv_set << value key, value = value.split('=') path = key.split('.') base_conf = path[0..-2].inject(Conf) { |c, name| c.send(name) } base_conf.send("#{path[-1]}=", YAML.load(value)) end parser.on("--log=SPEC", String, "configuration specification for text loggers. SPEC is of the form path/to/a/module:LEVEL[:FILE][,path/to/another]") do |log_spec| log_spec.split(',').each do |spec| mod, level, file = spec.split(':') Roby.app.log_setup(mod, level, file) end end parser.on('-r NAME', '--robot=NAME[,TYPE]', String, 'the robot name and type') do |name| robot_name, robot_type = name.split(',') Roby.app.setup_robot_names_from_config_dir Roby.app.robot(robot_name, robot_type) end parser.on('--debug', 'run in debug mode') do Roby.app.public_logs = true Roby.app.filter_backtraces = false require 'roby/app/debug' end parser.on_tail('-h', '--help', 'this help message') do STDERR.puts parser exit end end |
.find_data(*name) ⇒ Object
2393 2394 2395 2396 2397 2398 2399 2400 |
# File 'lib/roby/app.rb', line 2393 def self.find_data(*name) name = File.join(*name) Roby::Conf.datadirs.each do |dir| path = File.join(dir, name) return path if File.exists?(path) end raise Errno::ENOENT, "no file #{name} found in #{Roby::Conf.datadirs.join(":")}" end |
.guess_app_dir ⇒ String?
Guess the app directory based on the current directory
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/roby/app.rb', line 324 def self.guess_app_dir if test_dir = ENV['ROBY_APP_DIR'] if !Application.is_app_dir?(test_dir) raise InvalidRobyAppDirEnv, "the ROBY_APP_DIR envvar is set to #{test_dir}, but this is not a valid Roby application path" end return test_dir end path = Pathname.new(Dir.pwd).find_matching_parent do |test_dir| Application.is_app_dir?(test_dir.to_s) end if path path.to_s end end |
.host_options(parser, options) ⇒ Object
Sets up provided option parser to add the –host and –vagrant option
When added, a :host entry will be added to the provided options hash
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 |
# File 'lib/roby/app.rb', line 553 def self.(parser, ) [:host] ||= Roby.app.shell_interface_host || 'localhost' [:port] ||= Roby.app.shell_interface_port || Interface::DEFAULT_PORT parser.on('--host URL', String, "sets the host to connect to as hostname[:PORT]") do |url| if url =~ /(.*):(\d+)$/ [:host] = $1 [:port] = Integer($2) else [:host] = url end end parser.on('--vagrant NAME[:PORT]', String, "connect to a vagrant VM") do |vagrant_name| require 'roby/app/vagrant' if vagrant_name =~ /(.*):(\d+)$/ vagrant_name, port = $1, Integer($2) end [:host] = Roby::App::Vagrant.resolve_ip(vagrant_name) [:port] = port end end |
.is_app_dir?(test_dir) ⇒ Boolean
Tests if the given directory looks like the root of a Roby app
311 312 313 314 315 316 |
# File 'lib/roby/app.rb', line 311 def self.is_app_dir?(test_dir) File.file?(File.join(test_dir, 'config', 'app.yml')) || File.directory?(File.join(test_dir, 'models')) || File.directory?(File.join(test_dir, 'scripts', 'controllers')) || File.directory?(File.join(test_dir, 'config', 'robots')) end |
.overridable_configuration(config_set, config_key, options = Hash.new) ⇒ Object
Defines accessors for a configuration parameter stored in #options
This method allows to define a getter and a setter for a parameter stored in #options that should be user-overridable. It builds upon #attr_config.
For instance:
overridable_configuration 'log', 'filter_backtraces'
will create a #filter_backtraces getter and a #filter_backtraces= setter which allow to respectively access the log/filter_backtraces configuration value, and override it from its value in config/app.yml
The :predicate option allows to make the setter look like a predicate:
overridable_configuration 'log', 'filter_backtraces', predicate: true
will define #filter_backtraces? instead of #filter_backtraces
203 204 205 206 207 208 209 210 211 212 |
# File 'lib/roby/app.rb', line 203 def self.overridable_configuration(config_set, config_key, = Hash.new) = Kernel. , predicate: false, attr_name: config_key attr_config(config_set) define_method("#{options[:attr_name]}#{"?" if options[:predicate]}") do send(config_set)[config_key] end define_method("#{options[:attr_name]}=") do |new_value| send("#{config_set}_overrides")[config_key] = new_value end end |
.read_current_dir(current_path) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Read and validate the ‘current’ dir by means of the ‘current’ symlink that Roby maintains in its log base directory
1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 |
# File 'lib/roby/app.rb', line 1386 def self.read_current_dir(current_path) if !File.symlink?(current_path) raise ArgumentError, "#{current_path} does not exist or is not a symbolic link" end resolved_path = File.readlink(current_path) if !File.exist?(resolved_path) raise ArgumentError, "#{current_path} points to #{resolved_path}, which does not exist" elsif !File.directory?(resolved_path) raise ArgumentError, "#{current_path} points to #{resolved_path}, which is not a directory" end resolved_path end |
.register_plugin(name, mod, &init) ⇒ Object
2402 2403 2404 2405 2406 2407 |
# File 'lib/roby/app.rb', line 2402 def self.register_plugin(name, mod, &init) caller(1)[0] =~ /^([^:]+):\d/ dir = File.(File.dirname($1)) Roby.app.available_plugins.delete_if { |n| n == name } Roby.app.available_plugins << [name, dir, mod, init] end |
.unique_dirname(base_dir, path_spec, date_tag = nil) ⇒ Object
Returns a unique directory name as a subdirectory of base_dir, based on path_spec. The generated name is of the form
<base_dir>/a/b/c/YYYYMMDD-HHMM-basename
if path_spec = "a/b/c/basename". A .<number> suffix is appended if the path already exists.
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 |
# File 'lib/roby/app.rb', line 1409 def self.unique_dirname(base_dir, path_spec, date_tag = nil) if path_spec =~ /\/$/ basename = "" dirname = path_spec else basename = File.basename(path_spec) dirname = File.dirname(path_spec) end date_tag ||= Time.now.strftime('%Y%m%d-%H%M') if basename && !basename.empty? basename = date_tag + "-" + basename else basename = date_tag end # Check if +basename+ already exists, and if it is the case add a # .x suffix to it full_path = File.(File.join(dirname, basename), base_dir) base_dir = File.dirname(full_path) final_path, i = full_path, 0 while File.exists?(final_path) i += 1 final_path = full_path + ".#{i}" end final_path end |
Instance Method Details
#abort_on_application_exception=(flag) ⇒ Object
Controls whether the Roby app should quit if an application (i.e. non-plan) exception is received
The default is true
447 |
# File 'lib/roby/app.rb', line 447 attr_predicate :abort_on_application_exception, true |
#abort_on_application_exception? ⇒ Object
Controls whether the Roby app should quit if an application (i.e. non-plan) exception is received
The default is true
447 |
# File 'lib/roby/app.rb', line 447 attr_predicate :abort_on_application_exception, true |
#abort_on_exception=(flag) ⇒ Object
Controls whether the application should quit if an unhandled plan exception is received
The default is false
438 |
# File 'lib/roby/app.rb', line 438 attr_predicate :abort_on_exception, true |
#abort_on_exception? ⇒ Object
Controls whether the application should quit if an unhandled plan exception is received
The default is false
438 |
# File 'lib/roby/app.rb', line 438 attr_predicate :abort_on_exception, true |
#action_from_model(model) ⇒ Actions::Models::Action
Find an action on the planning interface that can generate the given task model
2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 |
# File 'lib/roby/app.rb', line 2587 def action_from_model(model) candidates = [] planners.each do |planner_model| planner_model.find_all_actions_by_type(model).each do |action| candidates << [planner_model, action] end end candidates = candidates.uniq if candidates.empty? raise ActionResolutionError, "cannot find an action to produce #{model}" elsif candidates.size > 1 raise ActionResolutionError, "more than one actions available produce #{model}: #{candidates.map { |pl, m| "#{pl}.#{m.name}" }.sort.join(", ")}" else candidates.first end end |
#action_from_name(name) ⇒ Actions::Models::Action
Finds the action matching the given name
Unlike #find_action_from_name, it raises if no matching action has been found
2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 |
# File 'lib/roby/app.rb', line 2634 def action_from_name(name) action = find_action_from_name(name) if !action available_actions = planners.map do |planner_model| planner_model.each_action.map(&:name) end.flatten if available_actions.empty? raise ActionResolutionError, "cannot find an action named #{name}, there are no actions defined" else raise ActionResolutionError, "cannot find an action named #{name}, available actions are: #{available_actions.sort.join(", ")}" end end action end |
#actions(&block) ⇒ Object
Declares that the following block should be used to setup the main action interface
978 979 980 |
# File 'lib/roby/app.rb', line 978 def actions(&block) action_handlers << block end |
#add_app_metadata(metadata) ⇒ Object
Add some metadata to #app_metadata, and save it to the log dir’s info.yml if it is already created
1281 1282 1283 1284 1285 1286 |
# File 'lib/roby/app.rb', line 1281 def () .merge!() if created_log_dir? end end |
#add_plugin(name, mod) ⇒ Object
1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 |
# File 'lib/roby/app.rb', line 1133 def add_plugin(name, mod) plugins << [name, mod] extend mod # If +load+ has already been called, call it on the module if mod.respond_to?(:load) && mod.load(self, ) end # Refresh the relation sets in #plan to include relations # possibly added by the plugin plan.refresh_relations mod end |
#app_file?(path) ⇒ Boolean
Returns true if the given path points to a file in the Roby app
2436 2437 2438 |
# File 'lib/roby/app.rb', line 2436 def app_file?(path) !!find_base_path_for(path) end |
#app_metadata ⇒ Object
Metadata used to describe the app
It is saved in the app’s log directory under info.yml
1293 1294 1295 1296 1297 |
# File 'lib/roby/app.rb', line 1293 def Hash['time' => time_tag, 'cmdline' => "#{$0} #{ARGV.join(" ")}", 'robot_name' => robot_name, 'robot_type' => robot_type, 'app_name' => app_name, 'app_dir' => app_dir].merge() end |
#app_module ⇒ Object
Returns this app’s toplevel module
261 262 263 |
# File 'lib/roby/app.rb', line 261 def app_module constant("::#{module_name}") end |
#app_path ⇒ Object
284 285 286 |
# File 'lib/roby/app.rb', line 284 def app_path @app_path ||= Pathname.new(app_dir) end |
#apply_config(config) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sets relevant configuration values from a configuration hash
1750 1751 1752 1753 1754 1755 1756 1757 |
# File 'lib/roby/app.rb', line 1750 def apply_config(config) if host_port = config['interface'] apply_config_interface(host_port) elsif host_port = config.fetch('droby', Hash.new)['host'] Roby.warn_deprecated 'the droby.host configuration parameter in config/app.yml is deprecated, use "interface" at the toplevel instead' apply_config_interface(host_port) end end |
#apply_config_interface(host_port) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Parses and applies the ‘interface’ value from a configuration hash
It is a helper for #apply_config
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 |
# File 'lib/roby/app.rb', line 1764 def apply_config_interface(host_port) if host_port !~ /:\d+$/ host_port += ":#{Interface::DEFAULT_PORT}" end match = /(.*):(\d+)$/.match(host_port) host = match[1] @shell_interface_host = if !host.empty? host end @shell_interface_port = Integer(match[2]) end |
#auto_load_all=(flag) ⇒ Boolean
Controls whether Roby’s auto-load feature should load all models in #search_path or only the ones in #app_dir. It influences the return value of #auto_load_search_path
2361 |
# File 'lib/roby/app.rb', line 2361 attr_predicate :auto_load_all?, true |
#auto_load_all? ⇒ Boolean
Controls whether Roby’s auto-load feature should load all models in #search_path or only the ones in #app_dir. It influences the return value of #auto_load_search_path
2361 |
# File 'lib/roby/app.rb', line 2361 attr_predicate :auto_load_all?, true |
#auto_load_models? ⇒ Boolean
Controls whether Roby should load the available the model files automatically in #require_models
2370 |
# File 'lib/roby/app.rb', line 2370 attr_writer :auto_load_models |
#auto_load_search_path ⇒ Array<String>
Returns the search path for the auto-load feature. It depends on the value of #auto_load_all?.
2382 2383 2384 2385 2386 2387 |
# File 'lib/roby/app.rb', line 2382 def auto_load_search_path if auto_load_all? then search_path elsif app_dir then [app_dir] else [] end end |
#auto_require_models ⇒ Object
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 |
# File 'lib/roby/app.rb', line 1609 def auto_require_models # Require all common task models and the task models specific to # this robot if auto_load_models? load_all_model_files_in('tasks') if backward_compatible_naming? search_path = self.auto_load_search_path all_files = find_files_in_dirs('tasks', 'ROBOT', path: search_path, all: true, order: :specific_last, pattern: /\.rb$/) all_files.each do |p| require(p) end end call_plugins(:auto_require_models, self) end end |
#auto_require_planners ⇒ Object
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 |
# File 'lib/roby/app.rb', line 1705 def auto_require_planners search_path = self.auto_load_search_path prefixes = ['actions'] if backward_compatible_naming? prefixes << 'planners' end prefixes.each do |prefix| load_all_model_files_in(prefix) end if backward_compatible_naming? main_files = find_files('planners', 'ROBOT', 'main.rb', all: true, order: :specific_first) main_files.each do |path| require path end planner_files = find_files_in_dirs('planners', 'ROBOT', all: true, order: :specific_first, pattern: /\.rb$/) planner_files.each do |path| require path end end call_plugins(:require_planners, self) end |
#autodiscover_tests_in?(path) ⇒ Boolean
Hook for the plugins to filter out some paths that should not be auto-loaded by #each_test_file_in_app. It does not affect #each_test_file_for_loaded_models.
2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 |
# File 'lib/roby/app.rb', line 2789 def autodiscover_tests_in?(path) suffix = File.basename(path) if robots.has_robot?(suffix) && ![robot_name, robot_type].include?(suffix) false elsif defined? super super else true end end |
#automatic_testing=(flag) ⇒ Object
True if user interaction is disabled during tests
453 |
# File 'lib/roby/app.rb', line 453 attr_predicate :automatic_testing?, true |
#automatic_testing? ⇒ Object
True if user interaction is disabled during tests
453 |
# File 'lib/roby/app.rb', line 453 attr_predicate :automatic_testing?, true |
#backward_compatible_naming=(flag) ⇒ Object
If set to true, the app will enable backward-compatible behaviour related to naming schemes, file placements and so on
The default is true
234 |
# File 'lib/roby/app.rb', line 234 attr_predicate :backward_compatible_naming?, true |
#backward_compatible_naming? ⇒ Object
If set to true, the app will enable backward-compatible behaviour related to naming schemes, file placements and so on
The default is true
234 |
# File 'lib/roby/app.rb', line 234 attr_predicate :backward_compatible_naming?, true |
#base_cleanup ⇒ Object
The inverse of #base_setup
791 792 793 794 795 796 797 798 799 800 801 802 803 |
# File 'lib/roby/app.rb', line 791 def base_cleanup if !public_logs? created_log_dirs.delete_if do |dir| FileUtils.rm_rf dir true end created_log_base_dirs.sort_by(&:length).reverse_each do |dir| # .rmdir will ignore nonempty / nonexistent directories FileUtils.rmdir(dir) created_log_base_dirs.delete(dir) end end end |
#base_setup ⇒ Object
777 778 779 780 781 782 783 784 785 786 787 788 |
# File 'lib/roby/app.rb', line 777 def base_setup STDOUT.sync = true load_base_config if !@log_dir find_and_create_log_dir end setup_loggers(redirections: true) # Set up the loaded plugins call_plugins(:base_setup, self) end |
#call_plugins(method, *args, deprecated: nil) ⇒ Object
Call method on each loaded extension module which define it, with arguments args
1069 1070 1071 1072 1073 1074 1075 1076 |
# File 'lib/roby/app.rb', line 1069 def call_plugins(method, *args, deprecated: nil) each_responding_plugin(method) do |config_extension| if deprecated Roby.warn "#{config_extension} uses the deprecated .#{method} hook during setup and teardown, #{deprecated}" end config_extension.send(method, *args) end end |
#cleanup ⇒ Object
The inverse of #setup. It gets called at the end of #run
846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 |
# File 'lib/roby/app.rb', line 846 def cleanup # Run the cleanup handlers first, we want the plugins to still be # active cleanup_handlers.each(&:call) call_plugins(:cleanup, self) # Deprecated version of #cleanup call_plugins(:reset, self, deprecated: "define 'cleanup' instead") planners.clear plan.execution_engine.gather_propagation do plan.clear end clear_models clear_config stop_shell_interface base_cleanup end |
#clear_config ⇒ Object
2462 2463 2464 2465 2466 2467 |
# File 'lib/roby/app.rb', line 2462 def clear_config Conf.clear call_plugins(:clear_config, self) # Deprecated name for clear_config call_plugins(:reload_config, self) end |
#clear_exceptions ⇒ Object
1506 1507 1508 |
# File 'lib/roby/app.rb', line 1506 def clear_exceptions registered_exceptions.clear end |
#clear_model?(m) ⇒ Boolean
Whether this model should be cleared in #clear_models
The default implementation returns true for the models that are not registered as constants (more precisely, for which MetaRuby’s #permanent_model? returns false) and for the models defined in this app.
2529 2530 2531 2532 |
# File 'lib/roby/app.rb', line 2529 def clear_model?(m) !m.permanent_model? || (!testing? && model_defined_in_app?(m)) end |
#clear_models ⇒ Object
Clear all models for which #clear_model? returns true
2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 |
# File 'lib/roby/app.rb', line 2535 def clear_models root_models.each do |root_model| submodels = root_model.each_submodel.to_a.dup submodels.each do |m| if clear_model?(m) m.permanent_model = false m.clear_model end end end DRoby::V5::DRobyConstant.clear_cache clear_models_handlers.each { |b| b.call } call_plugins(:clear_models, self) end |
#controller(&block) ⇒ Object
Declares that the following block should be used as the robot controller
972 973 974 |
# File 'lib/roby/app.rb', line 972 def controller(&block) controllers << block end |
#created_log_dir? ⇒ Boolean
Test whether this app already created its log directory
1300 1301 1302 |
# File 'lib/roby/app.rb', line 1300 def created_log_dir? @log_dir && File.directory?(@log_dir) end |
#define_actions_module ⇒ Object
1679 1680 1681 1682 1683 |
# File 'lib/roby/app.rb', line 1679 def define_actions_module if !app_module.const_defined_here?(:Actions) app_module.const_set(:Actions, Module.new) end end |
#define_main_planner_if_needed ⇒ Object
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 |
# File 'lib/roby/app.rb', line 1685 def define_main_planner_if_needed if !app_module::Actions.const_defined_here?(:Main) app_module::Actions.const_set(:Main, Class.new(Roby::Actions::Interface)) end if backward_compatible_naming? if !Object.const_defined_here?(:Main) Object.const_set(:Main, app_module::Actions::Main) end end end |
#defined_plugin?(name) ⇒ Boolean
True if name is a plugin known to us
1044 1045 1046 |
# File 'lib/roby/app.rb', line 1044 def defined_plugin?(name) available_plugins.any? { |plugname, *_| plugname == name } end |
#definition_file_for(model) ⇒ Object
Returns the downmost app file that was involved in the given model’s definition
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 |
# File 'lib/roby/app.rb', line 1644 def definition_file_for(model) return if !model.respond_to?(:definition_location) || !model.definition_location model.definition_location.each do |location| file = location.absolute_path next if !(base_path = find_base_path_for(file)) relative = Pathname.new(file).relative_path_from(base_path) split = relative.each_filename.to_a next if split[0] != 'models' return file end nil end |
#development_mode? ⇒ Object
Whether the app should run in development mode
Some expensive tests are disabled when not in development mode. This is the default
134 |
# File 'lib/roby/app.rb', line 134 attr_predicate :development_mode?, true |
#discover_test_files(all: true, only_self: false) ⇒ Array<String>
Discover which tests should be run, and require them
2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 |
# File 'lib/roby/app.rb', line 2766 def discover_test_files(all: true, only_self: false) if all test_files = each_test_file_in_app.inject(Hash.new) do |h, k| h[k] = Array.new h end if !only_self test_files.merge!(Hash[each_test_file_for_loaded_models.to_a]) end else test_files = Hash[each_test_file_for_loaded_models.to_a] if only_self test_files = test_files.find_all { |f, _| self_file?(f) } end end test_files end |
#each_model(root_model = nil) {|| ... } ⇒ Object
Enumerate all models registered in this app
It basically enumerate all submodels of all models in #root_models
2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 |
# File 'lib/roby/app.rb', line 2509 def each_model(root_model = nil) return enum_for(__method__, root_model) if !block_given? if !root_model self.root_models.each { |m| each_model(m, &Proc.new) } return end yield(root_model) root_model.each_submodel do |m| yield(m) end end |
#each_notification_listener {|the| ... } ⇒ Object
Enumerates the listeners currently registered through #on_notification
2723 2724 2725 |
# File 'lib/roby/app.rb', line 2723 def each_notification_listener(&block) notification_listeners.each(&block) end |
#each_plugin(on_available = false) ⇒ Object
Enumerates all available plugins, yielding only the plugin module (i.e. the plugin object itself)
1050 1051 1052 1053 1054 1055 1056 1057 1058 |
# File 'lib/roby/app.rb', line 1050 def each_plugin(on_available = false) plugins = self.plugins if on_available plugins = available_plugins.map { |name, _, mod, _| [name, mod] } end plugins.each do |_, mod| yield(mod) end end |
#each_responding_plugin(method, on_available = false) ⇒ Object
Yields each plugin object that respond to method
1061 1062 1063 1064 1065 |
# File 'lib/roby/app.rb', line 1061 def each_responding_plugin(method, on_available = false) each_plugin do |mod| yield(mod) if mod.respond_to?(method) end end |
#each_test_file_for_loaded_models {|path, models| ... } ⇒ Object
Enumerate the test files that should be run to test the current app configuration
2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 |
# File 'lib/roby/app.rb', line 2828 def each_test_file_for_loaded_models(&block) models_per_file = Hash.new { |h, k| h[k] = Set.new } each_model do |m| next if m.respond_to?(:has_ancestor?) && m.has_ancestor?(Roby::Event) next if m.respond_to?(:private_specialization?) && m.private_specialization? next if !m.name test_files_for(m).each do |test_path| models_per_file[test_path] << m end end find_files('test', 'actions', 'ROBOT', 'test_main.rb', order: :specific_first, all: true).each do |path| models_per_file[path] = Set[main_action_interface] end find_dirs('test', 'lib', order: :specific_first, all: true).each do |path| Pathname.new(path).find do |p| if p.basename.to_s =~ /^test_.*.rb$/ || p.basename.to_s =~ /_test\.rb$/ models_per_file[p.to_s] = Set.new end end end models_per_file.each(&block) end |
#each_test_file_in_app ⇒ Object
Enumerate all the test files in this app and for this robot configuration
2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 |
# File 'lib/roby/app.rb', line 2802 def each_test_file_in_app return enum_for(__method__) if !block_given? dir = File.join(app_dir, 'test') return if !File.directory?(dir) Find.find(dir) do |path| # Skip the robot-specific bits that don't apply on the # selected robot if File.directory?(path) Find.prune if !autodiscover_tests_in?(path) end if File.file?(path) && path =~ /test_.*\.rb$/ yield(path) end end end |
#each_ui_event_listener {|the| ... } ⇒ Object
Enumerates the listeners currently registered through #on_ui_event
2680 2681 2682 |
# File 'lib/roby/app.rb', line 2680 def each_ui_event_listener(&block) ui_event_listeners.each(&block) end |
#execution_engine ⇒ ExecutionEngine?
The engine associated with #plan
106 |
# File 'lib/roby/app.rb', line 106 def execution_engine; plan.execution_engine if plan end |
#find_action_from_name(name) ⇒ (ActionInterface,Actions::Models::Action)?
Find an action with the given name on the action interfaces registered on #planners
2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 |
# File 'lib/roby/app.rb', line 2611 def find_action_from_name(name) candidates = [] planners.each do |planner_model| if m = planner_model.find_action_by_name(name) candidates << [planner_model, m] end end candidates = candidates.uniq if candidates.size > 1 raise ActionResolutionError, "more than one action interface provide the #{name} action: #{candidates.map { |pl, m| "#{pl}" }.sort.join(", ")}" else candidates.first end end |
#find_and_create_log_dir(time_tag = self.time_tag) ⇒ String
Create a log directory for the given time tag, and make it this app’s log directory
The time tag given to this method also becomes the app’s time tag
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 |
# File 'lib/roby/app.rb', line 1207 def find_and_create_log_dir(time_tag = self.time_tag) base_dir = log_base_dir @time_tag = time_tag while true log_dir = Roby::Application.unique_dirname(base_dir, '', time_tag) new_dirs = Array.new dir = log_dir while !File.directory?(dir) new_dirs << dir dir = File.dirname(dir) end # Create all paths necessary, but check for possible concurrency # issues with other Roby-based tools creating a log dir with the # same name failed = new_dirs.reverse.any? do |dir| begin FileUtils.mkdir(dir) false rescue Errno::EEXIST true end end if !failed new_dirs.delete(log_dir) created_log_dirs << log_dir created_log_base_dirs.concat(new_dirs) @log_dir = log_dir return log_dir end end end |
#find_base_path_for(path) ⇒ nil, String
Returns the path in search_path that contains the given file or path
2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 |
# File 'lib/roby/app.rb', line 2413 def find_base_path_for(path) if @find_base_path_rx_paths != search_path @find_base_path_rx = search_path.map { |app_dir| [Pathname.new(app_dir), app_dir, %r{(^|/)#{app_dir}(/|$)}] }. sort_by { |_, app_dir, _| app_dir.size }. reverse @find_base_path_rx_paths = search_path.dup end longest_prefix_path, _ = @find_base_path_rx.find do |app_path, app_dir, rx| (path =~ rx) || ((path[0] != ?/) && File.file?(File.join(app_dir, path))) end longest_prefix_path end |
#find_data(*name) ⇒ Object
2389 2390 2391 |
# File 'lib/roby/app.rb', line 2389 def find_data(*name) Application.find_data(*name) end |
#find_dir(*args) ⇒ Object
Returns the first match from #find_dirs, or nil if nothing matches
2293 2294 2295 2296 2297 2298 2299 2300 |
# File 'lib/roby/app.rb', line 2293 def find_dir(*args) if !args.last.kind_of?(Hash) args.push(Hash.new) end args.last.delete('all') args.last.merge!(all: true) find_dirs(*args).first end |
#find_files_in_dirs(*path, options) ⇒ Array<String>
Enumerates the subdirectories of paths in #search_path matching the given path. The subdirectories are resolved using File.join(*path) If one of the elements of the path is the string ‘ROBOT’, it gets replaced by the robot name and type.
Given a search dir of [app2, app1]
app1/models/tasks/goto.rb
app1/models/tasks/v3/goto.rb
app2/models/tasks/asguard/goto.rb
2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 |
# File 'lib/roby/app.rb', line 2075 def find_dirs(*dir_path) Application.debug { "find_dirs(#{dir_path.map(&:inspect).join(", ")})" } if dir_path.last.kind_of?(Hash) = dir_path.pop end = Kernel.( || Hash.new, :all, :order, :path) if dir_path.empty? raise ArgumentError, "no path given" end search_path = [:path] || self.search_path if !.has_key?(:all) raise ArgumentError, "no :all argument given" elsif !.has_key?(:order) raise ArgumentError, "no :order argument given" elsif ![:specific_first, :specific_last].include?([:order]) raise ArgumentError, "expected either :specific_first or :specific_last for the :order argument, but got #{options[:order]}" end relative_paths = [] base_dir_path = dir_path.dup base_dir_path.delete_if { |p| p =~ /ROBOT/ } relative_paths = [base_dir_path] if dir_path.any? { |p| p =~ /ROBOT/ } && robot_name && robot_type replacements = [robot_type] if robot_type != robot_name replacements << robot_name end replacements.each do |replacement| robot_dir_path = dir_path.map do |s| s.gsub('ROBOT', replacement) end relative_paths << robot_dir_path end end root_paths = search_path.dup if [:order] == :specific_first relative_paths = relative_paths.reverse else root_paths = root_paths.reverse end result = [] Application.debug { " relative paths: #{relative_paths.inspect}" } relative_paths.each do |rel_path| root_paths.each do |root| abs_path = File.(File.join(*rel_path), root) Application.debug { " absolute path: #{abs_path}" } if File.directory?(abs_path) Application.debug { " selected" } result << abs_path end end end if result.empty? return result elsif ![:all] return [result.first] else return result end end |
#find_file(*args) ⇒ Object
Returns the first match from #find_files, or nil if nothing matches
2283 2284 2285 2286 2287 2288 2289 2290 |
# File 'lib/roby/app.rb', line 2283 def find_file(*args) if !args.last.kind_of?(Hash) args.push(Hash.new) end args.last.delete('all') args.last.merge!(all: true) find_files(*args).first end |
#find_files(*path, options) ⇒ Array<String>
Enumerates files based on their relative paths in #search_path. The paths are resolved using File.join(*path) If one of the elements of the path is the string ‘ROBOT’, it gets replaced by the robot name and type.
Given a search dir of [app2, app1], a robot name of v3 and a robot type of asguard,
app1/config/v3.rb
app2/config/asguard.rb
2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 |
# File 'lib/roby/app.rb', line 2232 def find_files(*file_path) if file_path.last.kind_of?(Hash) = file_path.pop end = Kernel.( || Hash.new, :all, :order, :path) if file_path.empty? raise ArgumentError, "no path given" end # Remove the filename from the complete path filename = file_path.pop filename = filename.split('/') file_path.concat(filename[0..-2]) filename = filename[-1] if filename =~ /ROBOT/ && robot_name args = file_path + [.merge(pattern: filename.gsub('ROBOT', robot_name))] robot_name_matches = find_files_in_dirs(*args) robot_type_matches = [] if robot_name != robot_type args = file_path + [.merge(pattern: filename.gsub('ROBOT', robot_type))] robot_type_matches = find_files_in_dirs(*args) end if [:order] == :specific_first result = robot_name_matches + robot_type_matches else result = robot_type_matches + robot_name_matches end else args = file_path.dup args << .merge(pattern: filename) result = find_files_in_dirs(*args) end orig_path = Pathname.new(File.join(*file_path)) orig_path += filename if orig_path.absolute? && File.file?(orig_path.to_s) if [:order] == :specific_first result.unshift orig_path.to_s else result.push orig_path.to_s end end return result end |
#find_files_in_dirs(*path, options) ⇒ Array<String>
Enumerates the files that are present in subdirectories of paths in #search_path. The subdirectories are resolved using File.join(*path) If one of the elements of the path is the string ‘ROBOT’, it gets replaced by the robot name and type.
Given a search dir of [app2, app1]
app1/models/tasks/goto.rb
app1/models/tasks/v3/goto.rb
app2/models/tasks/asguard/goto.rb
2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 |
# File 'lib/roby/app.rb', line 2172 def find_files_in_dirs(*dir_path) Application.debug { "find_files_in_dirs(#{dir_path.map(&:inspect).join(", ")})" } if dir_path.last.kind_of?(Hash) = dir_path.pop end = Kernel.( || Hash.new, :all, :order, :path, pattern: Regexp.new("")) dir_search = dir_path.dup dir_search << { all: true, order: [:order], path: [:path] } search_path = find_dirs(*dir_search) result = [] search_path.each do |dirname| Application.debug { " dir: #{dirname}" } Dir.new(dirname).each do |file_name| file_path = File.join(dirname, file_name) Application.debug { " file: #{file_path}" } if File.file?(file_path) && [:pattern] === file_name Application.debug " added" result << file_path end end break if ![:all] end return result end |
#framework_file?(path) ⇒ Boolean
Tests whether a path is within a framework library
2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 |
# File 'lib/roby/app.rb', line 2443 def framework_file?(path) if path =~ /roby\/.*\.rb$/ true else Roby.app.plugins.any? do |name, _| _, dir, _, _ = Roby.app.plugin_definition(name) path =~ %r{(^|/)#{dir}(/|$)} end end end |
#guess_app_dir ⇒ String
Guess the app directory based on the current directory, and sets #app_dir. It will not do anything if the current directory is not in a Roby app. Moreover, it does nothing if #app_dir is already set
350 351 352 353 354 355 |
# File 'lib/roby/app.rb', line 350 def guess_app_dir return if @app_dir if app_dir = self.class.guess_app_dir @app_dir = app_dir end end |
#has_app? ⇒ Boolean
Whether there is a supporting app directory
341 342 343 |
# File 'lib/roby/app.rb', line 341 def has_app? !!@app_dir end |
#ignore_all_load_errors=(flag) ⇒ Object
If set to true, files that generate errors while loading will be ignored. This is used for model browsing GUIs to be usable even if there are errors
It is false by default
225 |
# File 'lib/roby/app.rb', line 225 attr_predicate :ignore_all_load_errors?, true |
#ignore_all_load_errors? ⇒ Object
If set to true, files that generate errors while loading will be ignored. This is used for model browsing GUIs to be usable even if there are errors
It is false by default
225 |
# File 'lib/roby/app.rb', line 225 attr_predicate :ignore_all_load_errors?, true |
#isolate_load_errors(message, logger = Application, level = :warn) ⇒ Object
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 |
# File 'lib/roby/app.rb', line 1510 def isolate_load_errors(, logger = Application, level = :warn) yield rescue Interrupt raise rescue ::Exception => e register_exception(e, ) if ignore_all_load_errors? Robot.warn Roby.log_exception_with_backtrace(e, logger, level) else raise end end |
#join ⇒ Object
1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 |
# File 'lib/roby/app.rb', line 1935 def join @thread.join rescue Exception => e if @thread.alive? && execution_engine.running? if execution_engine.forced_exit? raise else execution_engine.quit retry end else raise end end |
#load_all_model_files_in(prefix_name, ignored_exceptions: Array.new) ⇒ Object
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 |
# File 'lib/roby/app.rb', line 1574 def load_all_model_files_in(prefix_name, ignored_exceptions: Array.new) search_path = auto_load_search_path dirs = find_dirs( "models", prefix_name, path: search_path, all: true, order: :specific_last) dirs.each do |dir| all_files = Set.new Find.find(dir) do |path| # Skip the robot-specific bits that don't apply on the # selected robot if File.directory?(path) suffix = File.basename(File.dirname(path)) if robots.has_robot?(suffix) && ![robot_name, robot_type].include?(suffix) Find.prune end end if File.file?(path) && path =~ /\.rb$/ all_files << path end end all_files.each do |path| begin require(path) rescue *ignored_exceptions => e ::Robot.warn "ignored file #{path}: #{e.message}" end end end end |
#load_base_config ⇒ Object
Loads the base configuration
This method loads the two most basic configuration files:
* config/app.yml
* config/init.rb
It also calls the plugin’s ‘load’ method
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 |
# File 'lib/roby/app.rb', line 725 def load_base_config load_config_yaml setup_loggers(ignore_missing: true, redirections: false) setup_robot_names_from_config_dir # Get the application-wide configuration if plugins_enabled? register_plugins end update_load_path if initfile = find_file('config', 'init.rb', order: :specific_first) Application.info "loading init file #{initfile}" require initfile end update_load_path # Deprecated hook call_plugins(:load, self, deprecated: "define 'load_base_config' instead") call_plugins(:load_base_config, self) update_load_path if defined? Roby::Conf Roby::Conf.datadirs = find_dirs('data', 'ROBOT', all: true, order: :specific_first) end if has_app? require_robot_file end init_handlers.each(&:call) update_load_path # Define the app module if there is none, and define a root logger # on it app_module = begin self.app_module rescue NameError Object.const_set(module_name, Module.new) end if !app_module.respond_to?(:logger) module_name = self.module_name app_module.class_eval do extend ::Logger::Root(module_name, Logger::INFO) end end end |
#load_config_yaml ⇒ Object
1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 |
# File 'lib/roby/app.rb', line 1729 def load_config_yaml file = find_file('config', 'app.yml', order: :specific_first) return if !file Application.info "loading config file #{file}" = YAML.load(File.open(file)) || Hash.new if robot_name && (robot_config = .delete('robots')) = .recursive_merge(robot_config[robot_name] || Hash.new) end = .map_value do |k, val| val || Hash.new end = .recursive_merge() apply_config() = end |
#load_default_models ⇒ Object
Helper to the robot config files to load the root files in models/ (e.g. models/tasks.rb)
1633 1634 1635 1636 1637 1638 1639 1640 |
# File 'lib/roby/app.rb', line 1633 def load_default_models ['tasks.rb', 'actions.rb'].each do |root_type| if path = find_file('models', root_type, path: [app_dir], order: :specific_first) require path end end call_plugins(:load_default_models, self) end |
#load_plugin_file(appfile) ⇒ Object
Load the given Roby plugin file. It is usually called app.rb, and should call register_plugin with the relevant information
Note that the file should not do anything yet. The actions required to have a functional plugin should be taken only in the block given to register_plugin or in the relevant plugin methods.
1023 1024 1025 1026 1027 1028 1029 1030 |
# File 'lib/roby/app.rb', line 1023 def load_plugin_file(appfile) begin require appfile rescue Roby.warn "cannot load plugin #{appfile}: #{$!.full_message}\n" end Roby.info "loaded plugin #{appfile}" end |
#load_plugins_from_prefix(dir) ⇒ Object
Looks into subdirectories of dir for files called app.rb and registers them as Roby plugins
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 |
# File 'lib/roby/app.rb', line 1002 def load_plugins_from_prefix(dir) dir = File.(dir) $LOAD_PATH.unshift dir Dir.new(dir).each do |subdir| subdir = File.join(dir, subdir) next unless File.directory?(subdir) appfile = File.join(subdir, "app.rb") next unless File.file?(appfile) load_plugin_file(appfile) end ensure $LOAD_PATH.shift end |
#loaded_plugin?(name) ⇒ Boolean
Returns true if name is a loaded plugin
1033 1034 1035 |
# File 'lib/roby/app.rb', line 1033 def loaded_plugin?(name) plugins.any? { |plugname, _| plugname == name } end |
#log ⇒ Object
:method: log_server=
Sets whether the log server should be started
607 |
# File 'lib/roby/app.rb', line 607 overridable_configuration 'log', 'filter_backtraces', predicate: true |
#log_base_dir ⇒ Object
The base directory in which logs should be saved
Logs are saved in log_base_dir/$time_tag by default, and a log_base_dir/current symlink gets updated to reflect the most current log directory.
The path is controlled by the log/dir configuration variable. If the provided value, it is interpreted relative to the application directory. It defaults to “data”.
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 |
# File 'lib/roby/app.rb', line 1181 def log_base_dir maybe_relative_dir = if @log_base_dir ||= log['dir'] @log_base_dir elsif global_base_dir = ENV['ROBY_BASE_LOG_DIR'] File.join(global_base_dir, app_name) else 'logs' end File.(maybe_relative_dir, app_dir || Dir.pwd) end |
#log_base_dir=(dir) ⇒ Object
Sets the directory under which logs should be created
This cannot be called after log_dir has been set
1196 1197 1198 |
# File 'lib/roby/app.rb', line 1196 def log_base_dir=(dir) @log_base_dir = dir end |
#log_create_current=(flag) ⇒ Object
If set to true, this Roby application will create a ‘current’ entry in #log_base_dir that points to the latest log directory. Otherwise, it will not. It is false when ‘roby run’ is started with an explicit log directory (the –log-dir option)
This symlink will never be created if #public_logs? is false, regardless of this setting.
2338 |
# File 'lib/roby/app.rb', line 2338 attr_predicate :log_create_current?, true |
#log_create_current? ⇒ Object
If set to true, this Roby application will create a ‘current’ entry in #log_base_dir that points to the latest log directory. Otherwise, it will not. It is false when ‘roby run’ is started with an explicit log directory (the –log-dir option)
This symlink will never be created if #public_logs? is false, regardless of this setting.
2338 |
# File 'lib/roby/app.rb', line 2338 attr_predicate :log_create_current?, true |
#log_current_dir ⇒ Object
The path to the current log directory
If #log_dir is set, it is used. Otherwise, the current log directory is inferred by the directory pointed to the ‘current’ symlink
1352 1353 1354 1355 1356 1357 1358 1359 |
# File 'lib/roby/app.rb', line 1352 def log_current_dir if @log_dir @log_dir else current_path = File.join(log_base_dir, "current") self.class.read_current_dir(current_path) end end |
#log_current_file ⇒ Object
The path to the current log file
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 |
# File 'lib/roby/app.rb', line 1364 def log_current_file log_current_dir = self.log_current_dir = if .empty? raise NoCurrentLog, "#{log_current_dir} is not a valid Roby log dir, it does not have an info.yml metadata file" elsif !(robot_name = .map { |h| h['robot_name'] }.compact.last) raise NoCurrentLog, "#{log_current_dir}'s metadata does not specify the robot name" end full_path = File.join(log_current_dir, "#{robot_name}-events.log") if !File.file?(full_path) raise NoCurrentLog, "inferred log file #{full_path} for #{log_current_dir}, but that file does not exist" end full_path end |
#log_dir ⇒ Object
The directory in which logs are to be saved Defaults to app_dir/data/$time_tag
1245 1246 1247 1248 1249 1250 |
# File 'lib/roby/app.rb', line 1245 def log_dir if !@log_dir raise LogDirNotInitialized, "the log directory has not been initialized yet" end @log_dir end |
#log_dir=(dir) ⇒ Object
Explicitely set the log directory
It is usually automatically created under #log_base_dir during #base_setup
1266 1267 1268 1269 1270 1271 |
# File 'lib/roby/app.rb', line 1266 def log_dir=(dir) if !File.directory?(dir) raise ArgumentError, "log directory #{dir} does not exist" end @log_dir = dir end |
#log_read_metadata ⇒ Object
Read the time tag from the current log directory
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 |
# File 'lib/roby/app.rb', line 1330 def dir = begin log_current_dir rescue ArgumentError end if dir && File.exists?(File.join(dir, 'info.yml')) YAML.load(File.read(File.join(dir, 'info.yml'))) else Array.new end end |
#log_read_time_tag ⇒ Object
1343 1344 1345 1346 |
# File 'lib/roby/app.rb', line 1343 def log_read_time_tag = .last && ['time_tag'] end |
#log_save_metadata(append: true) ⇒ Object
Save #app_metadata in the log directory
1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 |
# File 'lib/roby/app.rb', line 1309 def (append: true) path = File.join(log_dir, 'info.yml') info = Array.new current = if File.file?(path) YAML.load(File.read(path)) || Array.new else Array.new end if append || current.empty? current << else current[-1] = end File.open(path, 'w') do |io| YAML.dump(current, io) end end |
#log_setup(mod_path, level, file = nil) ⇒ Object
Configures a text logger in the system. It has to be called before #setup to have an effect.
It overrides configuration from the app.yml file
For instance,
log_setup 'roby/execution_engine', 'DEBUG'
will be equivalent to having the following entry in config/app.yml:
log:
levels:
roby/execution_engine: DEBUG
592 593 594 595 |
# File 'lib/roby/app.rb', line 592 def log_setup(mod_path, level, file = nil) levels = (log_overrides['levels'] ||= Hash.new) levels[mod_path] = [level, file].compact.join(":") end |
#main_action_interface ⇒ Object
Returns this app’s main action interface
This is usually set up in the robot configuration file by calling Robot.actions
269 270 271 |
# File 'lib/roby/app.rb', line 269 def main_action_interface app_module::Actions::Main end |
#make_path_relative(path) ⇒ Object
Transforms path into a path relative to an entry in search_path (usually the application root directory)
1492 1493 1494 1495 1496 1497 1498 1499 1500 |
# File 'lib/roby/app.rb', line 1492 def make_path_relative(path) if !File.exists?(path) path elsif root_path = find_base_path_for(path) return Pathname.new(path).relative_path_from(root_path).to_s else path end end |
#model_defined_in_app?(model) ⇒ Boolean
Tests whether a model class has been defined in this app’s code
2480 2481 2482 2483 2484 2485 2486 |
# File 'lib/roby/app.rb', line 2480 def model_defined_in_app?(model) model.definition_location.each do |location| return if location.label == 'require' return true if app_file?(location.absolute_path) end false end |
#modelling_only ⇒ Object
2351 |
# File 'lib/roby/app.rb', line 2351 def modelling_only; self.modelling_only = true end |
#needs_to_be_in_current_app(allowed_outside: true) ⇒ Object
Call to check whether the current directory is within #app_dir. If not, raises
This is called by tools for which being in another app than the currently selected would be really too confusing
375 376 377 378 379 380 381 382 |
# File 'lib/roby/app.rb', line 375 def needs_to_be_in_current_app(allowed_outside: true) guessed_dir = self.class.guess_app_dir if guessed_dir && (@app_dir != guessed_dir) raise NotInCurrentApp, "#{@app_dir} is currently selected, but the current directory is within #{guessed_dir}" elsif !guessed_dir && !allowed_outside raise NotInCurrentApp, "not currently within an app dir" end end |
#notify(source, level, message) ⇒ Object
Sends a message to all notification listeners
2728 2729 2730 2731 2732 |
# File 'lib/roby/app.rb', line 2728 def notify(source, level, ) each_notification_listener do |block| block.call(source, level, ) end end |
#on_cleanup(&block) ⇒ Object
Declares that the following block should be called when #clear_models is called
993 994 995 996 997 998 |
# File 'lib/roby/app.rb', line 993 def on_cleanup(&block) if !block raise ArgumentError, "missing expected block argument" end cleanup_handlers << block end |
#on_clear_models(&block) ⇒ Object
Declares that the following block should be called when #clear_models is called
984 985 986 987 988 989 |
# File 'lib/roby/app.rb', line 984 def on_clear_models(&block) if !block raise ArgumentError, "missing expected block argument" end clear_models_handlers << block end |
#on_config(&block) ⇒ Object
use #on_setup instead
966 967 968 |
# File 'lib/roby/app.rb', line 966 def on_config(&block) on_setup(&block) end |
#on_init(&block) ⇒ Object
Declares a block that should be executed when the Roby app gets initialized (i.e. just after init.rb gets loaded)
940 941 942 943 944 945 |
# File 'lib/roby/app.rb', line 940 def on_init(&block) if !block raise ArgumentError, "missing expected block argument" end init_handlers << block end |
#on_notification {|source, level, message| ... } ⇒ Object
Registers a block to be called when a message needs to be dispatched from #notify
2742 2743 2744 2745 2746 2747 2748 |
# File 'lib/roby/app.rb', line 2742 def on_notification(&block) if !block raise ArgumentError, "missing expected block argument" end notification_listeners << block block end |
#on_require(&block) ⇒ Object
Declares a block that should be executed when the Roby app loads models (i.e. in #require_models)
958 959 960 961 962 963 |
# File 'lib/roby/app.rb', line 958 def on_require(&block) if !block raise ArgumentError, "missing expected block argument" end require_handlers << block end |
#on_setup(&block) ⇒ Object
Declares a block that should be executed when the Roby app is begin setup
949 950 951 952 953 954 |
# File 'lib/roby/app.rb', line 949 def on_setup(&block) if !block raise ArgumentError, "missing expected block argument" end setup_handlers << block end |
#on_ui_event {|name, args| ... } ⇒ Object
Registers a block to be called when a message needs to be dispatched from #ui_event
2698 2699 2700 2701 2702 2703 2704 |
# File 'lib/roby/app.rb', line 2698 def on_ui_event(&block) if !block raise ArgumentError, "missing expected block argument" end ui_event_listeners << block block end |
#plugin_definition(name) ⇒ Object
Returns the [name, dir, file, module] array definition of the plugin name, or nil if name is not a known plugin
1039 1040 1041 |
# File 'lib/roby/app.rb', line 1039 def plugin_definition(name) available_plugins.find { |plugname, *_| plugname == name } end |
#plugins_enabled=(flag) ⇒ Object
True if plugins should be discovered, registered and loaded (true by default)
460 |
# File 'lib/roby/app.rb', line 460 attr_predicate :plugins_enabled?, true |
#plugins_enabled? ⇒ Object
True if plugins should be discovered, registered and loaded (true by default)
460 |
# File 'lib/roby/app.rb', line 460 attr_predicate :plugins_enabled?, true |
#prepare ⇒ Object
Prepares the environment to actually run
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 |
# File 'lib/roby/app.rb', line 882 def prepare if public_shell_interface? setup_shell_interface end if public_rest_interface? setup_rest_interface end if public_logs? && log_create_current? FileUtils.rm_f File.join(log_base_dir, "current") FileUtils.ln_s log_dir, File.join(log_base_dir, 'current') end if log['events'] && public_logs? logfile_path = prepare_event_log # Start a log server if needed, and poll the log directory for new # data sources if = (log.has_key?('server') ? log['server'] : Hash.new) if !.kind_of?(Hash) = Hash.new end plan.event_logger.sync = true start_log_server(logfile_path, ) Roby.info "log server started" else plan.event_logger.sync = false Roby.warn "log server disabled" end end call_plugins(:prepare, self) end |
#prepare_action(name, mission: false, **arguments) ⇒ Object
Generate the plan pattern that will call the required action on the planning interface, with the given arguments.
This returns immediately, and the action is not yet deployed at that point.
2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 |
# File 'lib/roby/app.rb', line 2656 def prepare_action(name, mission: false, **arguments) if name.kind_of?(Class) planner_model, m = action_from_model(name) else planner_model, m = action_from_name(name) end if mission plan.add_mission_task(task = m.plan_pattern(arguments)) else plan.add(task = m.plan_pattern(arguments)) end return task, task.planning_task end |
#prepare_event_log ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
867 868 869 870 871 872 873 874 875 876 877 878 879 |
# File 'lib/roby/app.rb', line 867 def prepare_event_log require 'roby/droby/event_logger' require 'roby/droby/logfile/writer' logfile_path = File.join(log_dir, "#{robot_name}-events.log") event_io = File.open(logfile_path, 'w') logfile = DRoby::Logfile::Writer.new(event_io, plugins: plugins.map { |n, _| n }) plan.event_logger = DRoby::EventLogger.new(logfile) plan.execution_engine.event_logger = plan.event_logger Robot.info "logs are in #{log_dir}" logfile_path end |
#public_logs=(flag) ⇒ Object
If set to true, this Roby application will make its logs public, i.e. will save the logs in logs/ and update the logs/current symbolic link accordingly. Otherwise, the logs are saved in a folder in logs/ that is deleted on teardown, and current is not updated
Only the run modes have public logs by default
2326 |
# File 'lib/roby/app.rb', line 2326 attr_predicate :public_logs?, true |
#public_logs? ⇒ Object
If set to true, this Roby application will make its logs public, i.e. will save the logs in logs/ and update the logs/current symbolic link accordingly. Otherwise, the logs are saved in a folder in logs/ that is deleted on teardown, and current is not updated
Only the run modes have public logs by default
2326 |
# File 'lib/roby/app.rb', line 2326 attr_predicate :public_logs?, true |
#public_rest_interface=(flag) ⇒ Object
If set to true, this Roby application will publish a Interface::REST::API object
632 |
# File 'lib/roby/app.rb', line 632 attr_predicate :public_rest_interface?, true |
#public_rest_interface? ⇒ Object
If set to true, this Roby application will publish a Interface::REST::API object
632 |
# File 'lib/roby/app.rb', line 632 attr_predicate :public_rest_interface?, true |
#public_shell_interface=(flag) ⇒ Object
If set to true, this Roby application will publish a Interface::Interface object as a TCP server.
2315 |
# File 'lib/roby/app.rb', line 2315 attr_predicate :public_shell_interface?, true |
#public_shell_interface? ⇒ Object
If set to true, this Roby application will publish a Interface::Interface object as a TCP server.
2315 |
# File 'lib/roby/app.rb', line 2315 attr_predicate :public_shell_interface?, true |
#register_exception(e, reason = nil) ⇒ Object
1502 1503 1504 |
# File 'lib/roby/app.rb', line 1502 def register_exception(e, reason = nil) registered_exceptions << [e, reason] end |
#register_plugins(force: false) ⇒ Object
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 |
# File 'lib/roby/app.rb', line 1078 def register_plugins(force: false) if !plugins_enabled? && !force raise PluginsDisabled, "cannot call #register_plugins while the plugins are disabled" end # Load the plugins 'main' files if plugin_path = ENV['ROBY_PLUGIN_PATH'] plugin_path.split(':').each do |plugin| if File.directory?(plugin) load_plugins_from_prefix plugin elsif File.file?(plugin) load_plugin_file plugin end end end end |
#register_server(name, port) ⇒ Object
Register a server port that can be discovered later
1487 1488 |
# File 'lib/roby/app.rb', line 1487 def register_server(name, port) end |
#reload_actions ⇒ Object
Reload action models defined in models/actions/
2561 2562 2563 2564 2565 2566 2567 2568 |
# File 'lib/roby/app.rb', line 2561 def reload_actions unload_features("actions", ".*\.rb$") unload_features("models", "actions", ".*\.rb$") planners.each do |planner_model| planner_model.clear_model end require_planners end |
#reload_config ⇒ Object
Reload files in config/
2470 2471 2472 2473 2474 2475 2476 2477 |
# File 'lib/roby/app.rb', line 2470 def reload_config clear_config unload_features("config", ".*\.rb$") if has_app? require_robot_file end call_plugins(:require_config, self) end |
#reload_models ⇒ Object
Reload model files in models/
2551 2552 2553 2554 2555 2556 2557 2558 |
# File 'lib/roby/app.rb', line 2551 def reload_models clear_models unload_features("models", ".*\.rb$") additional_model_files.each do |path| unload_features(path) end require_models end |
#reload_planners ⇒ Object
2570 2571 2572 2573 2574 2575 2576 2577 |
# File 'lib/roby/app.rb', line 2570 def reload_planners unload_features("planners", ".*\.rb$") unload_features("models", "planners", ".*\.rb$") planners.each do |planner_model| planner_model.clear_model end require_planners end |
#remove_notification_listener(listener) ⇒ Object
Removes a notification listener added with #on_notification
2754 2755 2756 |
# File 'lib/roby/app.rb', line 2754 def remove_notification_listener(listener) notification_listeners.delete(listener) end |
#remove_ui_event_listener(listener) ⇒ Object
Removes a notification listener added with #on_ui_event
2710 2711 2712 |
# File 'lib/roby/app.rb', line 2710 def remove_ui_event_listener(listener) ui_event_listeners.delete(listener) end |
#require(absolute_path) ⇒ Object
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 |
# File 'lib/roby/app.rb', line 1523 def require(absolute_path) # Make the file relative to the search path file = make_path_relative(absolute_path) Roby::Application.info "loading #{file} (#{absolute_path})" isolate_load_errors("ignored file #{file}") do if file != absolute_path Kernel.require(file) else Kernel.require absolute_path end end end |
#require_app_dir(needs_current: false, allowed_outside: true) ⇒ Object
Call to require this roby application to be in a Roby application
It tries to guess the app directory. If none is found, it raises.
360 361 362 363 364 365 366 367 368 |
# File 'lib/roby/app.rb', line 360 def require_app_dir(needs_current: false, allowed_outside: true) guess_app_dir if !@app_dir raise ArgumentError, "your current directory does not seem to be a Roby application directory; did you forget to run 'roby init'?" end if needs_current needs_to_be_in_current_app(allowed_outside: allowed_outside) end end |
#require_models ⇒ Object
Loads the models, based on the given robot name and robot type
1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 |
# File 'lib/roby/app.rb', line 1537 def require_models # Set up the loaded plugins call_plugins(:require_config, self, deprecated: "define 'require_models' instead") call_plugins(:require_models, self) require_handlers.each do |handler| isolate_load_errors("while calling #{handler}") do handler.call end end define_actions_module if auto_load_models? auto_require_planners end define_main_planner_if_needed action_handlers.each do |act| isolate_load_errors("error in #{act}") do app_module::Actions::Main.class_eval(&act) end end additional_model_files.each do |path| require File.(path) end if auto_load_models? auto_require_models end # Set up the loaded plugins call_plugins(:finalize_model_loading, self) plan.refresh_relations end |
#require_planners ⇒ Object
Loads the planner models
This method is called at the end of #require_models, before the plugins’ require_models hook is called
1700 1701 1702 1703 |
# File 'lib/roby/app.rb', line 1700 def require_planners Roby.warn_deprecated "Application#require_planners is deprecated and has been renamed into #auto_require_planners" auto_require_planners end |
#require_robot_file ⇒ Object
1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 |
# File 'lib/roby/app.rb', line 1810 def require_robot_file p = find_file('config', 'robots', "#{robot_name}.rb", order: :specific_first) || find_file('config', 'robots', "#{robot_type}.rb", order: :specific_first) if p @default_auto_load = false require p if !robot_type robot(robot_name, robot_name) end elsif !find_dir('config', 'robots', order: :specific_first) || (robot_name == robots.default_robot_name) || !robots.strict? Roby.warn "#{robot_name}:#{robot_type} is selected as the robot, but there is" if robot_name == robot_type Roby.warn "no file named config/robots/#{robot_name}.rb" else Roby.warn "neither config/robots/#{robot_name}.rb nor config/robots/#{robot_type}.rb" end Roby.warn "run roby gen robot #{robot_name} in your app to create one" Roby.warn "initialization will go on, but this behaviour is deprecated and will be removed in the future" else raise NoSuchRobot, "cannot find config file for robot #{robot_name} of type #{robot_type} in config/robots/" end end |
#reset_log_dir ⇒ Object
Reset the current log dir so that #setup picks a new one
1253 1254 1255 |
# File 'lib/roby/app.rb', line 1253 def reset_log_dir @log_dir = nil end |
#reset_plan(plan = ExecutablePlan.new) ⇒ Object
Reset the plan to a new Plan object
1258 1259 1260 |
# File 'lib/roby/app.rb', line 1258 def reset_plan(plan = ExecutablePlan.new) @plan = plan end |
#restart(*cmdline) ⇒ Object
Quits this app and replaces with a new one after a proper cleanup
1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 |
# File 'lib/roby/app.rb', line 1966 def restart(*cmdline) @restarting = true @restart_cmdline = if cmdline.empty? if defined? ORIGINAL_ARGV [$0, *ORIGINAL_ARGV] else [$0, *ARGV] end else cmdline end plan.execution_engine.quit end |
#restarting? ⇒ Boolean
Whether #run should exec a new process on quit or not
1957 1958 1959 |
# File 'lib/roby/app.rb', line 1957 def restarting? !!@restarting end |
#robot(name, type = nil) ⇒ Object
Sets up the name and type of the robot. This can be called only once in a given Roby controller.
1168 1169 1170 |
# File 'lib/roby/app.rb', line 1168 def robot(name, type = nil) @robot_name, @robot_type = robots.resolve(name, type) end |
#robot_name ⇒ String?
The robot name
1151 1152 1153 1154 1155 |
# File 'lib/roby/app.rb', line 1151 def robot_name if @robot_name then @robot_name else robots.default_robot_name end end |
#robot_name?(name) ⇒ Boolean
Test if the given name is a valid robot name
1627 1628 1629 |
# File 'lib/roby/app.rb', line 1627 def robot_name?(name) !robots.strict? || robots.has_robot?(name) end |
#robot_type ⇒ String?
The robot type
1160 1161 1162 1163 1164 |
# File 'lib/roby/app.rb', line 1160 def robot_type if @robot_type then @robot_type else robots.default_robot_type end end |
#robots ⇒ App::RobotNames
The robot names configuration
929 930 931 932 933 934 935 936 |
# File 'lib/roby/app.rb', line 929 def robots if !@robots robots = App::RobotNames.new(['robots'] || Hash.new) robots.strict = !!['robots'] @robots = robots end @robots end |
#root_models ⇒ Array<#each_submodel>
The list of model classes that allow to discover all models in this app
2492 2493 2494 2495 2496 2497 2498 2499 2500 |
# File 'lib/roby/app.rb', line 2492 def root_models models = [Task, TaskService, TaskEvent, Actions::Interface, Actions::Library, Coordination::ActionScript, Coordination::ActionStateMachine, Coordination::TaskScript] each_responding_plugin(:root_models) do |config_extension| models.concat(config_extension.root_models) end models end |
#run(thread_priority: 0, &block) ⇒ Object
1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 |
# File 'lib/roby/app.rb', line 1912 def run(thread_priority: 0, &block) prepare engine_config = self.engine engine = self.plan.execution_engine plugins = self.plugins.map { |_, mod| mod if (mod.respond_to?(:start) || mod.respond_to?(:run)) }.compact engine.once do run_plugins(plugins, &block) end @thread = Thread.new do Thread.current.priority = thread_priority engine.run cycle: engine_config['cycle'] || 0.1 end join ensure shutdown @thread = nil if restarting? Kernel.exec *@restart_cmdline end end |
#run_plugins(mods, &block) ⇒ Object
Helper for Application#run to call the plugin’s run or start methods while guaranteeing the system’s cleanup
This method recursively calls each plugin’s #run method (if defined) in block forms. This guarantees that the plugins can use a begin/rescue/end mechanism to do their cleanup
If no run-related cleanup is required, the plugin can define a #start(app) method instead.
Note that the cleanup we talk about here is related to running. Cleanup required after #setup must be done in #cleanup
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 |
# File 'lib/roby/app.rb', line 1992 def run_plugins(mods, &block) if mods.empty? yield if block_given? else mod = mods.shift if mod.respond_to?(:start) mod.start(self) run_plugins(mods, &block) else mod.run(self) do run_plugins(mods, &block) end end end end |
#running? ⇒ Boolean
Whether we’re inside #run
1952 1953 1954 |
# File 'lib/roby/app.rb', line 1952 def running? !!@thread end |
#self_file?(path) ⇒ Boolean
Returns true if the given path points to a file under #app_dir
2429 2430 2431 |
# File 'lib/roby/app.rb', line 2429 def self_file?(path) find_base_path_for(path) == app_path end |
#setup ⇒ Object
Does basic setup of the Roby environment. It loads configuration files and sets up singleton objects.
After a call to #setup, the Roby services that do not require an execution loop to run should be available
Plugins that define a setup(app) method will see their method called at this point
The #cleanup method is the reverse of #setup
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 |
# File 'lib/roby/app.rb', line 815 def setup base_setup # Set up the loaded plugins call_plugins(:setup, self) # And run the setup handlers setup_handlers.each(&:call) require_models # Main is always included in the planner list self.planners << app_module::Actions::Main # Attach the global fault tables to the plan self.planners.each do |planner| if planner.respond_to?(:each_fault_response_table) planner.each_fault_response_table do |table, arguments| plan.use_fault_response_table table, arguments end end end rescue Exception begin cleanup rescue Exception => e Roby.warn "failed to cleanup after #setup raised" Roby.log_exception_with_backtrace(e, Roby, :warn) end raise end |
#setup_for_minimal_tooling ⇒ Object
2302 2303 2304 2305 2306 2307 2308 |
# File 'lib/roby/app.rb', line 2302 def setup_for_minimal_tooling self.public_logs = false self.auto_load_models = false self.single = true self.modelling_only = true setup end |
#setup_loggers(ignore_missing: false, redirections: true) ⇒ Object
Sets up all the default loggers. It creates the logger for the Robot module (accessible through Robot.logger), and sets up log levels as specified in the config/app.yml file.
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 |
# File 'lib/roby/app.rb', line 1444 def setup_loggers(ignore_missing: false, redirections: true) Robot.logger.progname = robot_name || 'Robot' return if !log['levels'] # Set up log levels log['levels'].each do |name, value| const_name = name.modulize mod = begin Kernel.constant(const_name) rescue NameError => e if ignore_missing next elsif name != const_name raise InvalidLoggerName, "cannot resolve logger #{name} (resolved as #{const_name}): #{e.message}" else raise InvalidLoggerName, "cannot resolve logger #{name}: #{e.message}" end end if value =~ /^(\w+):(.+)$/ value, file = $1, $2 file = file.gsub('ROBOT', robot_name) if robot_name end level = Logger.const_get(value) io = if redirections && file path = File.(file, log_dir) Robot.info "redirected logger for #{mod} to #{path} (level #{level})" io = File.open(path, 'w') io.sync = true log_files[path] ||= io else STDOUT end new_logger = Logger.new(io) new_logger.level = level new_logger.formatter = mod.logger.formatter new_logger.progname = [name, robot_name].compact.join(" ") mod.logger = new_logger end end |
#setup_rest_interface ⇒ Object
Publishes a REST API
The REST API will long-term replace the shell interface. It is however currently too limited for this purpose. Whether one should use one or the other is up to the application, but prefer the REST API if it suits your needs
1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 |
# File 'lib/roby/app.rb', line 1877 def setup_rest_interface require 'roby/interface/rest' if @rest_interface raise RuntimeError, "there is already a REST interface started, call #stop_rest_interface first" end composite_api = Class.new(Grape::API) composite_api.mount Interface::REST::API call_plugins(:setup_rest_interface, self, composite_api) @rest_interface = Interface::REST::Server.new( self, host: rest_interface_host, port: rest_interface_port, api: composite_api) @rest_interface.start if rest_interface_port != Interface::DEFAULT_REST_PORT Robot.info "REST interface started on port #{@rest_interface.port(timeout: nil)}" else Robot.debug "REST interface started on port #{rest_interface_port}" end @rest_interface end |
#setup_robot_names_from_config_dir ⇒ Object
1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 |
# File 'lib/roby/app.rb', line 1797 def setup_robot_names_from_config_dir robot_config_files = find_files_in_dirs 'config', 'robots', all: true, order: :specific_first, pattern: lambda { |p| File.extname(p) == ".rb" } robots.strict = !robot_config_files.empty? robot_config_files.each do |path| robot_name = File.basename(path, ".rb") robots.robots[robot_name] ||= robot_name end end |
#setup_shell_interface ⇒ Object
Publishes a shell interface
This method publishes a Roby::Interface object using Interface::TCPServer. It is published on Interface::DEFAULT_PORT by default. This default can be overriden by setting #shell_interface_port either in config/init.rb, or in a Robot.setup block in the robot configuration file.
The shell interface is started in #setup and stopped in #cleanup
1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 |
# File 'lib/roby/app.rb', line 1845 def setup_shell_interface require 'roby/interface' if @shell_interface raise RuntimeError, "there is already a shell interface started, call #stop_shell_interface first" end @shell_interface = Interface::TCPServer.new( self, host: shell_interface_host, port: shell_interface_port) shell_interface.abort_on_exception = shell_abort_on_exception? if shell_interface_port != Interface::DEFAULT_PORT Robot.info "shell interface started on port #{shell_interface_port}" else Robot.debug "shell interface started on port #{shell_interface_port}" end end |
#shell ⇒ Object
2346 |
# File 'lib/roby/app.rb', line 2346 def shell; self.shell = true end |
#shutdown ⇒ Object
The inverse of #prepare. It gets called either at the end of #run or at the end of #setup if there is an error during loading
919 920 921 922 923 924 |
# File 'lib/roby/app.rb', line 919 def shutdown call_plugins(:shutdown, self) stop_log_server stop_shell_interface stop_rest_interface(join: true) end |
#simulation ⇒ Object
2341 |
# File 'lib/roby/app.rb', line 2341 def simulation; self.simulation = true end |
#single ⇒ Object
2348 |
# File 'lib/roby/app.rb', line 2348 def single; @single = true end |
#start_log_server(logfile, options = Hash.new) ⇒ Object
2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 |
# File 'lib/roby/app.rb', line 2010 def start_log_server(logfile, = Hash.new) require 'roby/droby/logfile/server' # Allocate a TCP server to get an ephemeral port, and pass it to # roby-display sampling_period = DRoby::Logfile::Server::DEFAULT_SAMPLING_PERIOD sampling_period = Float(['sampling_period'] || sampling_period) tcp_server = TCPServer.new(Integer(['port'] || 0)) server_flags = ["--fd=#{tcp_server.fileno}", "--sampling=#{sampling_period}", logfile] redirect_flags = Hash[tcp_server => tcp_server] if ['debug'] server_flags << "--debug" elsif ['silent'] redirect_flags[:out] = redirect_flags[:err] = :close end @log_server_port = tcp_server.local_address.ip_port @log_server_pid = Kernel.spawn( Gem.ruby, File.join(Roby::BIN_DIR, "roby-display"), 'server', *server_flags, redirect_flags) ensure tcp_server.close if tcp_server end |
#stop ⇒ Object
2008 |
# File 'lib/roby/app.rb', line 2008 def stop; call_plugins(:stop, self) end |
#stop_log_server ⇒ Object
2035 2036 2037 2038 2039 2040 |
# File 'lib/roby/app.rb', line 2035 def stop_log_server if @log_server_pid Process.kill('INT', @log_server_pid) @log_server_pid = nil end end |
#stop_rest_interface(join: false) ⇒ Object
Stops a running REST interface
1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 |
# File 'lib/roby/app.rb', line 1901 def stop_rest_interface(join: false) if @rest_interface # In case we're shutting down while starting up, # we must synchronize with the start to ensure that # EventMachine will be properly stopped @rest_interface.wait_start @rest_interface.stop @rest_interface.join if join end end |
#stop_shell_interface ⇒ Object
Stops a running shell interface
This is a no-op if no shell interface is currently running
1864 1865 1866 1867 1868 1869 |
# File 'lib/roby/app.rb', line 1864 def stop_shell_interface if @shell_interface @shell_interface.close @shell_interface = nil end end |
#test_files_for(model) ⇒ Object
Given a model class, returns the full path of an existing test file that is meant to verify this model
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 |
# File 'lib/roby/app.rb', line 1659 def test_files_for(model) return [] if !model.respond_to?(:definition_location) || !model.definition_location test_files = Array.new model.definition_location.each do |location| file = location.absolute_path next if !(base_path = find_base_path_for(file)) relative = Pathname.new(file).relative_path_from(base_path) split = relative.each_filename.to_a next if split[0] != 'models' split[0] = 'test' split[-1] = "test_#{split[-1]}" canonical_testpath = [base_path, *split].join(File::SEPARATOR) if File.exist?(canonical_testpath) test_files << canonical_testpath end end test_files end |
#testing ⇒ Object
2344 |
# File 'lib/roby/app.rb', line 2344 def testing; self.testing = true end |
#time_tag ⇒ Object
The time tag. It is a time formatted as YYYYMMDD-HHMM used to mark log directories
1275 1276 1277 |
# File 'lib/roby/app.rb', line 1275 def time_tag @time_tag ||= Time.now.strftime('%Y%m%d-%H%M') end |
#ui_event(name, *args) ⇒ Object
Sends a message to all UI event listeners
2685 2686 2687 2688 2689 |
# File 'lib/roby/app.rb', line 2685 def ui_event(name, *args) each_ui_event_listener do |block| block.call(name, *args) end end |
#unload_features(*pattern) ⇒ Object
Ensure tha require’d files that match the given pattern can be re-required
2456 2457 2458 2459 2460 |
# File 'lib/roby/app.rb', line 2456 def unload_features(*pattern) patterns = search_path.map { |p| Regexp.new(File.join(p, *pattern)) } patterns << Regexp.new("^#{File.join(*pattern)}") $LOADED_FEATURES.delete_if { |path| patterns.any? { |p| p =~ path } } end |
#update_load_path ⇒ Object
1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 |
# File 'lib/roby/app.rb', line 1778 def update_load_path search_path.reverse.each do |app_dir| $LOAD_PATH.delete(app_dir) $LOAD_PATH.unshift(app_dir) libdir = File.join(app_dir, 'lib') if File.directory?(libdir) $LOAD_PATH.delete(libdir) $LOAD_PATH.unshift(libdir) end end find_dirs('lib', 'ROBOT', all: true, order: :specific_last). each do |libdir| if !$LOAD_PATH.include?(libdir) $LOAD_PATH.unshift libdir end end end |
#using(*names, force: false) ⇒ Object
Loads the plugins whose name are listed in names
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 |
# File 'lib/roby/app.rb', line 1096 def using(*names, force: false) if !plugins_enabled? && !force raise PluginsDisabled, "plugins are disabled, cannot load #{names.join(", ")}" end register_plugins(force: true) names.map do |name| name = name.to_s unless plugin = plugin_definition(name) raise ArgumentError, "#{name} is not a known plugin (available plugins: #{available_plugins.map { |n, *_| n }.join(", ")})" end name, dir, mod, init = *plugin if already_loaded = plugins.find { |n, m| n == name && m == mod } next(already_loaded[1]) end if dir filter_out_patterns.push(/#{Regexp.quote(dir)}/) end if init begin $LOAD_PATH.unshift dir init.call mod.reset(self) if mod.respond_to?(:reset) rescue Exception => e Roby.fatal "cannot load plugin #{name}: #{e.full_message}" exit(1) ensure $LOAD_PATH.shift end end add_plugin(name, mod) end end |