Class: Controller

Inherits:
Object
  • Object
show all
Defined in:
bin/magent

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Controller

Returns a new instance of Controller.



77
78
79
80
81
82
83
84
85
86
87
88
# File 'bin/magent', line 77

def initialize(opts)
  @options = opts

  @queue = @options[:queue]

  @options[:log_path] ||= Dir.getwd

  @identity = @options[:identifier] || Magent::Utils.underscore(@options[:queue].to_s)
  @identity << "-#{Socket.gethostname.split('.')[0]}"

  $stderr.puts ">> Starting magent in #{@options[:type]} model"
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



75
76
77
# File 'bin/magent', line 75

def options
  @options
end

Instance Method Details

#channelObject



120
121
122
# File 'bin/magent', line 120

def channel
  @channel ||= (@options[:type] == :async) ? Magent::AsyncChannel.new(@queue) : Magent::ActorChannel.new(@queue)
end

#restartObject



111
112
113
114
115
116
117
118
# File 'bin/magent', line 111

def restart
  begin
    stop
  rescue => e
    $stderr.puts "Warning: #{e}"
  end
  start
end

#startObject



90
91
92
93
94
95
96
# File 'bin/magent', line 90

def start
  if @options[:daemonize] && @options[:piddir]
    run_as_daemon
  else
    Magent::Processor.new(self.channel, @identity).run!
  end
end

#stopObject



98
99
100
101
102
103
104
105
106
107
108
109
# File 'bin/magent', line 98

def stop
  begin
    pid = File.read(pid_file).to_i
    Process.kill("QUIT", pid)
  rescue Errno::ECHILD, Errno::ESRCH => e
    $stdout.puts "Process #{pid} has stopped"
  rescue Errno::ENOENT => e
    $stdout.puts "Warning: #{e}"
  ensure
    File.unlink(pid_file) if File.exist?(pid_file)
  end
end