Class: ReliableMsg::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/reliable-msg/cli.rb

Overview

:nodoc:

Defined Under Namespace

Classes: InvalidUsage

Constant Summary collapse

USAGE =
<<-EOF
usage:  queues command [args] [options]

To see list of available commands and options
  queues help
EOF
HELP =
<<-EOF
usage:  queues command [args] [options]

Reliable messaging queue manager, version #{VERSION}

Available commands:

  help
    Display this help message.

  manager start
    Start the queue manager as a standalone server

  manager stop
    Stop a running queue manager.

  list <queue>
    List headers of messages in the named queue.

  empty <queue>
    Empty (delete all messages from) the named queue.

  install disk [<path>]
    Configure queue manager to use disk-based message store
    using the specified directory. Uses 'queues' by default.

  install mysql <host> <username> <password> <database> [options]
                [--port <port>] [--socket <socket>] [--prefix <prefix>]
    Configure queue manager to use MySQL for message store,
    using the specified connection properties. Updates database
    schema.

  --port  Port to connect to
  --socket  Socket to connect to
  --prefix  Prefix for table names


Available options:

  -v --version
    Show version number.

  -c --config <path>
    Points to the queue manager configuration file.

EOF
MAX_STRING =
50

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



81
82
# File 'lib/reliable-msg/cli.rb', line 81

def initialize
end

Instance Method Details

#queue_manager(config_file) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/reliable-msg/cli.rb', line 204

def queue_manager config_file
    if config_file
        config = Config.new config_file, nil
        unless config.load_no_create || config_file.nil?
            puts "Could not find configuration file #{config.path}"
            exit
        end
        drb = Config::DEFAULT_DRB
        drb.merge(config.drb) if config.drb
        drb_uri = "druby://localhost:#{drb['port']}"
    else
        drb_uri = Queue::DEFAULT_DRB_URI
    end
    DRbObject.new(nil, drb_uri)
end

#runObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/reliable-msg/cli.rb', line 84

def run
    begin
        config_file = nil
        opts = OptionParser.new
        opts.on("-c FILE", "--config FILE", String) { |value| config_file = value }
        opts.on("-v", "--version") do
            puts "Reliable messaging queue manager, version #{VERSION}"
            exit
        end
        opts.on("-h", "--help") do
            puts HELP
            exit
        end

        args = opts.parse(ARGV)

        raise InvalidUsage if args.length < 1
        case args[0]
        when 'help'
            puts HELP

        when 'manager'
            case args[1]
            when 'start', nil
                manager = QueueManager.new({:config=>config_file})
                manager.start
                begin
                    while manager.alive?
                        sleep 3
                    end
                rescue Interrupt
                    manager.stop
                end
            when 'stop'
                begin
                    queue_manager(config_file).stop
                rescue DRb::DRbConnError =>error
                    puts "Cannot access queue manager: is it running?"
                end
            else
                raise InvalidUsage
            end

        when 'install'
            config = Config.new config_file, nil
            case args[1]
            when 'disk'
                store = MessageStore::Disk.new({}, nil)
                config.store = store.configuration
                if config.create_if_none
                    store.setup
                    puts "Created queues configuration file: #{config.path}"
                else
                    puts "Found existing queues configuration file: #{config.path}"
                    puts "No changes made"
                end
            when 'mysql'
                host, username, password, database = args[2], args[3], args[4], args[5]
                raise InvalidUsage unless host && database && username && password
                conn = { "host"=>host, "username"=>username, "password"=>password, "database"=>database }
                store = MessageStore::MySQL.new(conn, nil)
                config.store = store.configuration
                if config.create_if_none
                    puts "Created queues configuration file: #{config.path}"
                    if store.setup
                        puts "Created queue manager tables in database '#{database}'"
                    end
                else
                    puts "Found existing queues configuration file: #{config.path}"
                    puts "No changes made"
                end
            else
                raise InvalidUsage
            end

        when 'list'
            queue = args[1]
            raise InvalidUsage unless queue
            begin
                qm = queue_manager(config_file)
                list = qm.list(:queue=>queue)
                puts "Found #{list.size} messages in queue #{queue}"
                list.each do |headers|
                    puts "Message #{headers[:id]}"
                    headers.each do |name, value|
                        unless name==:id
                            case value
                            when String
                                value = value[0..MAX_STRING - 3] << "..." if value.length > MAX_STRING
                                value = '"' << value.gsub('"', '\"') << '"'
                            when Symbol
                                value = ":#{value.to_s}"
                            end
                            puts "  :#{name} => #{value}"
                        end
                    end
                end
            rescue DRb::DRbConnError =>error
                puts "Cannot access queue manager: is it running?"
            end

        when 'empty'
            queue = args[1]
            raise InvalidUsage unless queue
            begin
                qm = queue_manager(config_file)
                while msg = qm.enqueue(:queue=>queue)
                end
            rescue DRb::DRbConnError =>error
                puts "Cannot access queue manager: is it running?"
            end

        else
            raise InvalidUsage
    end
    rescue InvalidUsage
        puts USAGE
    end
end