lib/cloud_crowd/command_line.rb in cloud-crowd-0.2.6 vs lib/cloud_crowd/command_line.rb in cloud-crowd-0.2.7

- old
+ new

@@ -22,10 +22,11 @@ install Install the CloudCrowd configuration files to the specified directory server Start up the central server (requires a database) node Start up a worker node (only one node per machine, please) console Launch a CloudCrowd console, connected to the central database load_schema Load the schema into the database specified by database.yml + cleanup Removes jobs that were finished over --days (7 by default) ago server -d [start | stop | restart] Servers and nodes can be launched as node -d [start | stop | restart] daemons, then stopped or restarted. Options: @@ -40,10 +41,11 @@ when 'console' then run_console when 'server' then run_server(subcommand) when 'node' then run_node(subcommand) when 'load_schema' then run_load_schema when 'install' then run_install(subcommand) + when 'cleanup' then run_cleanup else usage end end # Spin up an IRB session with the CloudCrowd code loaded in, and a database @@ -51,17 +53,18 @@ def run_console require 'irb' require 'irb/completion' require 'pp' load_code - connect_to_database(true) + connect_to_database true + CloudCrowd::Server # Preload server to autoload classes. + Object.send(:include, CloudCrowd) IRB.start end # `crowd server` can either 'start', 'stop', or 'restart'. def run_server(subcommand) - ensure_config load_code subcommand ||= 'start' case subcommand when 'start' then start_server when 'stop' then stop_server @@ -97,11 +100,11 @@ end # `crowd node` can either 'start', 'stop', or 'restart'. def run_node(subcommand) load_code - ENV['RACK_ENV'] = @options['environment'] + ENV['RACK_ENV'] = @options[:environment] case (subcommand || 'start') when 'start' then start_node when 'stop' then stop_node when 'restart' then restart_node end @@ -144,10 +147,17 @@ install_file "#{CC_ROOT}/config/config.example.ru", "#{install_path}/config.ru" install_file "#{CC_ROOT}/config/database.example.yml", "#{install_path}/database.yml" install_file "#{CC_ROOT}/actions", "#{install_path}/actions", true end + # Clean up all Jobs in the CloudCrowd database older than --days old. + def run_cleanup + load_code + connect_to_database(true) + Job.cleanup_all(:days => @options[:days]) + end + # Print `crowd` usage. def usage puts "\n#{@option_parser}\n" end @@ -161,10 +171,11 @@ found = CONFIG_FILES.all? {|f| File.exists? "#{@options[:config_path]}/#{f}" } found ? @config_dir = true : config_not_found end # Parse all options for all commands. + # Valid options are: --config --port --environment --daemonize --days. def parse_options @options = { :environment => 'production', :config_path => ENV['CLOUD_CROWD_CONFIG'] || '.', :daemonize => false @@ -174,14 +185,17 @@ @options[:config_path] = conf_path end opts.on('-p', '--port PORT', 'port number for server (central or node)') do |port_num| @options[:port] = port_num end - opts.on('-e', '--environment ENV', 'server environment (sinatra)') do |env| + opts.on('-e', '--environment ENV', 'server environment (defaults to production)') do |env| @options[:environment] = env end opts.on('-d', '--daemonize', 'run as a background daemon') do |daemonize| @options[:daemonize] = daemonize + end + opts.on('--days NUM_DAYS', 'grace period before cleanup (7 by default)') do |days| + @options[:days] = days.to_i if days.match(/\A\d+\Z/) end opts.on_tail('-v', '--version', 'show version') do require "#{CC_ROOT}/lib/cloud-crowd" puts "CloudCrowd version #{VERSION}" exit \ No newline at end of file