Class: Rivendell::Import::CLI
- Inherits:
-
Object
- Object
- Rivendell::Import::CLI
- Defined in:
- lib/rivendell/import/cli.rb
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#options_count ⇒ Object
Returns the value of attribute options_count.
Instance Method Summary collapse
- #config_file ⇒ Object
- #config_loader ⇒ Object
- #daemonize ⇒ Object
- #daemonize? ⇒ Boolean
- #database ⇒ Object
- #debug? ⇒ Boolean
- #dry_run? ⇒ Boolean
- #import ⇒ Object
-
#initialize(arguments = []) ⇒ CLI
constructor
A new instance of CLI.
- #listen_mode? ⇒ Boolean
- #options ⇒ Object (also: #parse)
- #parsed_parser ⇒ Object
- #parser ⇒ Object
- #paths ⇒ Object
- #pid_directory ⇒ Object
- #run ⇒ Object
- #setup_logger ⇒ Object
- #start_webserver ⇒ Object
- #syslog? ⇒ Boolean
Constructor Details
#initialize(arguments = []) ⇒ CLI
Returns a new instance of CLI.
11 12 13 |
# File 'lib/rivendell/import/cli.rb', line 11 def initialize(arguments = []) @arguments = arguments end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
8 9 10 |
# File 'lib/rivendell/import/cli.rb', line 8 def arguments @arguments end |
#options_count ⇒ Object
Returns the value of attribute options_count.
9 10 11 |
# File 'lib/rivendell/import/cli.rb', line 9 def end |
Instance Method Details
#config_file ⇒ Object
15 16 17 |
# File 'lib/rivendell/import/cli.rb', line 15 def config_file @config_file ||= [:config] end |
#config_loader ⇒ Object
109 110 111 |
# File 'lib/rivendell/import/cli.rb', line 109 def config_loader @config_loader = ConfigLoader.new(config_file, listen_mode?) end |
#daemonize ⇒ Object
113 114 115 116 117 118 |
# File 'lib/rivendell/import/cli.rb', line 113 def daemonize if daemonize? require 'daemons' Daemons.daemonize :app_name => "rivendell-import", :dir => pid_directory, :dir_mode => :normal end end |
#daemonize? ⇒ Boolean
35 36 37 |
# File 'lib/rivendell/import/cli.rb', line 35 def daemonize? [:daemon] end |
#database ⇒ Object
43 44 45 |
# File 'lib/rivendell/import/cli.rb', line 43 def database [:database] end |
#debug? ⇒ Boolean
27 28 29 |
# File 'lib/rivendell/import/cli.rb', line 27 def debug? [:debug] end |
#dry_run? ⇒ Boolean
23 24 25 |
# File 'lib/rivendell/import/cli.rb', line 23 def dry_run? [:dry_run] end |
#import ⇒ Object
73 74 75 |
# File 'lib/rivendell/import/cli.rb', line 73 def import @import ||= Rivendell::Import::Base.new end |
#listen_mode? ⇒ Boolean
19 20 21 |
# File 'lib/rivendell/import/cli.rb', line 19 def listen_mode? [:listen] end |
#options ⇒ Object Also known as: parse
60 61 62 63 64 65 |
# File 'lib/rivendell/import/cli.rb', line 60 def ||= Trollop::with_standard_exception_handling(parser) do raise Trollop::HelpNeeded if ARGV.empty? # show help screen parser.parse arguments end end |
#parsed_parser ⇒ Object
68 69 70 71 |
# File 'lib/rivendell/import/cli.rb', line 68 def parsed_parser parse parser end |
#parser ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rivendell/import/cli.rb', line 47 def parser @parser ||= Trollop::Parser.new do opt :config, "Configuration file", :type => String #, :required => true opt :listen, "Wait for files in given directory" opt :dry_run, "Just create tasks without executing them" opt :debug, "Enable debug messages (in stderr)" opt :syslog, "Log messages to syslog" opt :database, "The database file used to store tasks", :type => String opt :daemon, "Run in background" opt :pid_dir, "Directory to store pid", :type => String end end |
#paths ⇒ Object
77 78 79 |
# File 'lib/rivendell/import/cli.rb', line 77 def paths parsed_parser.leftovers end |
#pid_directory ⇒ Object
39 40 41 |
# File 'lib/rivendell/import/cli.rb', line 39 def pid_directory [:pid_dir] or Dir.pwd end |
#run ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/rivendell/import/cli.rb', line 120 def run setup_logger if database Rivendell::Import.establish_connection database else Rivendell::Import.establish_connection end config_loader.load daemonize config_loader.listen_file sleep 1 if listen_mode? start_webserver sleep 1 = {} [:dry_run] = true if dry_run? import.listen paths.first, else import.process paths import.tasks.run unless dry_run? end end |
#setup_logger ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rivendell/import/cli.rb', line 81 def setup_logger new_logger = if syslog? Syslog::Logger.new('rivendell-import').tap do |syslog| syslog.level = debug? ? Logger::DEBUG : Logger::INFO end elsif debug? Logger.new($stderr) end Rivendell::Import.logger = new_logger if new_logger end |
#start_webserver ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/rivendell/import/cli.rb', line 94 def start_webserver require 'rivendell/import/application' Rivendell::Import.logger.debug "Start webserver" Thread.new do Rivendell::Import::Application.set :config_loader, config_loader Rivendell::Import::Application.run! # FIXME we don't see difference between normal quit and start error (EADDRINUSE, ...) Rivendell::Import.logger.debug "Webserver is stopped" exit 0 end end |
#syslog? ⇒ Boolean
31 32 33 |
# File 'lib/rivendell/import/cli.rb', line 31 def syslog? [:syslog] end |