Class: CloudCrowd::CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/cloud_crowd/command_line.rb

Constant Summary collapse

CONFIG_FILES =

Configuration files required for the ‘crowd` command to function.

['config.yml', 'config.ru', 'database.yml']
CC_ROOT =

Reference the absolute path to the root.

File.expand_path(File.dirname(__FILE__) + '/../..')
<<-EOS
CloudCrowd is a MapReduce-inspired Parallel Processing System for Ruby.

Wiki: http://wiki.github.com/documentcloud/cloud-crowd
Rdoc: http://rdoc.info/projects/documentcloud/cloud-crowd

Usage: crowd COMMAND OPTIONS

Commands:
  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:
EOS

Instance Method Summary collapse

Constructor Details

#initializeCommandLine

Creating a CloudCrowd::CommandLine runs from the contents of ARGV.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/cloud_crowd/command_line.rb', line 36

def initialize
  parse_options
  command     = ARGV.shift
  subcommand  = ARGV.shift
  case command
  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

Instance Method Details

#restart_nodeObject

Restart the daemonized Node, if it exists.



127
128
129
130
131
# File 'lib/cloud_crowd/command_line.rb', line 127

def restart_node
  stop_node
  sleep 1
  start_node
end

#restart_serverObject

Restart the daemonized central server.



96
97
98
99
100
# File 'lib/cloud_crowd/command_line.rb', line 96

def restart_server
  stop_server
  sleep 1
  start_server
end

#run_cleanupObject

Clean up all Jobs in the CloudCrowd database older than –days old.



153
154
155
156
157
# File 'lib/cloud_crowd/command_line.rb', line 153

def run_cleanup
  load_code
  connect_to_database(true)
  Job.cleanup_all(:days => @options[:days])
end

#run_consoleObject

Spin up an IRB session with the CloudCrowd code loaded in, and a database connection established. The equivalent of Rails’ ‘script/console`.



53
54
55
56
57
58
59
60
61
62
# File 'lib/cloud_crowd/command_line.rb', line 53

def run_console
  require 'irb'
  require 'irb/completion'
  require 'pp'
  load_code
  connect_to_database true
  CloudCrowd::Server # Preload server to autoload classes.
  Object.send(:include, CloudCrowd)
  IRB.start
end

#run_install(install_path) ⇒ Object

Install the required CloudCrowd configuration files into the specified directory, or the current one.



142
143
144
145
146
147
148
149
150
# File 'lib/cloud_crowd/command_line.rb', line 142

def run_install(install_path)
  require 'fileutils'
  install_path ||= '.'
  FileUtils.mkdir_p install_path unless File.exists?(install_path)
  install_file "#{CC_ROOT}/config/config.example.yml", "#{install_path}/config.yml"
  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

#run_load_schemaObject

Load in the database schema to the database specified in ‘database.yml’.



134
135
136
137
138
# File 'lib/cloud_crowd/command_line.rb', line 134

def run_load_schema
  load_code
  connect_to_database(false)
  require 'cloud_crowd/schema.rb'
end

#run_node(subcommand) ⇒ Object

‘crowd node` can either ’start’, ‘stop’, or ‘restart’.



103
104
105
106
107
108
109
110
111
# File 'lib/cloud_crowd/command_line.rb', line 103

def run_node(subcommand)
  load_code
  ENV['RACK_ENV'] = @options[:environment]
  case (subcommand || 'start')
  when 'start'    then start_node
  when 'stop'     then stop_node
  when 'restart'  then restart_node
  end
end

#run_server(subcommand) ⇒ Object

‘crowd server` can either ’start’, ‘stop’, or ‘restart’.



65
66
67
68
69
70
71
72
73
# File 'lib/cloud_crowd/command_line.rb', line 65

def run_server(subcommand)
  load_code
  subcommand ||= 'start'
  case subcommand
  when 'start'    then start_server
  when 'stop'     then stop_server
  when 'restart'  then restart_server
  end
end

#start_nodeObject

Launch a Node. Please only run a single node per machine. The Node process will be long-lived, although its workers will come and go.



115
116
117
118
119
# File 'lib/cloud_crowd/command_line.rb', line 115

def start_node
  @options[:port] ||= Node::DEFAULT_PORT
  puts "Starting CloudCrowd Node (#{VERSION}) on port #{@options[:port]}..."
  Node.new(@options)
end

#start_serverObject

Convenience command for quickly spinning up the central server. More sophisticated deployments, load-balancing across multiple app servers, should use the config.ru rackup file directly. This method will start a single Thin server.



79
80
81
82
83
84
85
86
87
88
# File 'lib/cloud_crowd/command_line.rb', line 79

def start_server
  port        = @options[:port] || 9173
  daemonize   = @options[:daemonize] ? '-d' : ''
  log_path    = CloudCrowd.log_path('server.log')
  pid_path    = CloudCrowd.pid_path('server.pid')
  rackup_path = File.expand_path("#{@options[:config_path]}/config.ru")
  FileUtils.mkdir_p(CloudCrowd.log_path) if @options[:daemonize] && !File.exists?(CloudCrowd.log_path)
  puts "Starting CloudCrowd Central Server (#{VERSION}) on port #{port}..."
  exec "thin -e #{@options[:environment]} -p #{port} #{daemonize} --tag cloud-crowd-server --log #{log_path} --pid #{pid_path} -R #{rackup_path} start"
end

#stop_nodeObject

If the daemonized Node is running, stop it.



122
123
124
# File 'lib/cloud_crowd/command_line.rb', line 122

def stop_node
  Thin::Server.kill CloudCrowd.pid_path('node.pid')
end

#stop_serverObject

Stop the daemonized central server, if it exists.



91
92
93
# File 'lib/cloud_crowd/command_line.rb', line 91

def stop_server
  Thin::Server.kill(CloudCrowd.pid_path('server.pid'), 0)
end

#usageObject

Print ‘crowd` usage.



160
161
162
# File 'lib/cloud_crowd/command_line.rb', line 160

def usage
  puts "\n#{@option_parser}\n"
end