Class: StompServer::Run

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

Overview

Run server start up.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Run

Intiialize

[View source]

352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/stomp_server_ng.rb', line 352

def initialize(opts)
  @@log = Logger.new(STDOUT)
  @@log.level = StompServer::LogHelper.get_loglevel()

  @opts = opts
  @queue_manager = nil
  @auth_required = nil
  @stompauth = nil
  @topic_manager = nil
  @session_cache = nil
  @@log.debug("#{self.class} Run class initialize method complete")
end

Instance Attribute Details

#auth_requiredObject

Returns the value of attribute auth_required.


346
347
348
# File 'lib/stomp_server_ng.rb', line 346

def auth_required
  @auth_required
end

#queue_managerObject

Returns the value of attribute queue_manager.


346
347
348
# File 'lib/stomp_server_ng.rb', line 346

def queue_manager
  @queue_manager
end

#session_cacheObject

Returns the value of attribute session_cache.


349
350
351
# File 'lib/stomp_server_ng.rb', line 349

def session_cache
  @session_cache
end

#stompauthObject

Returns the value of attribute stompauth.


346
347
348
# File 'lib/stomp_server_ng.rb', line 346

def stompauth
  @stompauth
end

#topic_managerObject

Returns the value of attribute topic_manager.


346
347
348
# File 'lib/stomp_server_ng.rb', line 346

def topic_manager
  @topic_manager
end

Instance Method Details

#startObject

Startup

[View source]

375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/stomp_server_ng.rb', line 375

def start
  @@log.debug("#{self.class}.start begins")

  # Handle group priviliges!
  # N.B.: Handle these options from the command line ?????????
  begin
    if @opts[:group]
      @@log.debug "Changing group to #{@opts[:group]}."
      Process::GID.change_privilege(Etc.getgrnam(@opts[:group]).gid)
    end

    if @opts[:user]
      @@log.debug "Changing user to #{@opts[:user]}."
      Process::UID.change_privilege(Etc.getpwnam(@opts[:user]).uid)
    end
  rescue Errno::EPERM
    @@log.error "FAILED to change user:group #{@opts[:user]}:#{@opts[:group]}: #$!"
    exit 1
  end

  # Make required directories unless they already exist
  Dir.mkdir(@opts[:working_dir]) unless File.directory?(@opts[:working_dir])
  Dir.mkdir(@opts[:logdir]) unless File.directory?(@opts[:logdir])
  Dir.mkdir(@opts[:etcdir]) unless File.directory?(@opts[:etcdir])

  # Determine qstore type
  if @opts[:queue] == 'dbm'
    qstore=StompServer::DBMQueue.new(@opts[:storage])
    @@log.debug "Queue storage is DBM"
  elsif @opts[:queue] == 'file'
    qstore=StompServer::FileQueue.new(@opts[:storage])
    @@log.debug "Queue storage is FILE"
  elsif @opts[:queue] == 'activerecord'
    require 'stomp_server_ng/queue/activerecord_queue'
    qstore=StompServer::ActiveRecordQueue.new(@opts[:etcdir], @opts[:storage], @opts[:dbyml])
    @@log.debug "Queue storage is ActiveRecord"
  else
    qstore=StompServer::MemoryQueue.new
    @@log.debug "Queue storage is MEMORY"
  end

  # Set checkpoint interval
  qstore.checkpoint_interval = @opts[:checkpoint]
  @@log.debug "Checkpoint interval is #{qstore.checkpoint_interval}" if $DEBUG

  #
  @topic_manager = StompServer::TopicManager.new
  @queue_manager = StompServer::QueueManager.new(qstore)
  @@log.debug("Managers are initialized.")
  @@log.debug("Topic Manager: #{@topic_manager}")
  @@log.debug("Queue Manager: #{@queue_manager}")

  # Authorization: requirement
  @auth_required = @opts[:auth]
  if @auth_required
    @stompauth = StompServer::StompAuth.new(@opts[:passwd])
  end

  # Initialize session ID cache
  if @opts[:session_cache] > 0
    StompServer::SessionIDManager.initialize_cache(@opts[:session_cache])
    StompServer::SessionIDManager.dump_cache(@@log);
  end

  # If we are going to daemonize, it should be almost the last
  # thing we do here.
  @@log.debug("#{self.class}.start Daemonize: #{@opts[:daemon]}")
  if @opts[:daemon]
    @@log.debug("#{self.class}.start going to background")
    @@log.debug("#{self.class}.start check #{@opts[:logfile]}")

    StompServer::LogHelper.showversion(@@log)

    STDOUT.flush    # clear the decks
    Daemonize.daemonize(log_file=@opts[:logfile])
    # change back to the original starting directory
    Dir.chdir(@opts[:working_dir])
  end

  # But write pidfile only after possible daemonization
  curr_pid = Process.pid
  open(@opts[:pidfile],"w") {|f| f.write(curr_pid) }
  @@log.debug("Pid File Contents: #{curr_pid}")

  # OK, log and set the SIGINT signal handler.
  @@log.debug("#{self.class}.start setting trap at completion")
  StompServer::LogHelper.showversion(@@log) # one more time at startup
  # Show Load Path
  StompServer::LogHelper.showloadpath(@@log)
  StompServer::LogHelper.showoptions(@@log, @opts) # Dump runtime options
  trap("INT") { @@log.debug "INT signal received.";stop(@opts[:pidfile]) }
  trap("TERM") { @@log.debug "TERM signal received.";stop(@opts[:pidfile]) }
end

#stop(pidfile) ⇒ Object

Server stop on SIGINT or SIGTERM

[View source]

366
367
368
369
370
371
372
# File 'lib/stomp_server_ng.rb', line 366

def stop(pidfile)
  @queue_manager.stop("KILL_ISSUED")
  @@log.debug "Stompserver #{StompServer::VERSION} shutting down"
  STDOUT.flush
  EventMachine::stop_event_loop
  File.delete(pidfile)
end