私は同じ問題を抱えていて、解決策を見つけました。まず、コードをlib
dir (例: /lib/listener/init.rb
) に配置し、EM を実行するクラス メソッドを 1 つ作成します(例: ) Listener.run
。
#!/usr/bin/env ruby
require File.expand_path('../../config/environment', File.dirname(__FILE__))
class Listener
def self.run
# your code here
# you can access your models too
end
end
その後ダンテジェムを使用。ファイルを作成し/init/listener
ます。コードは次のようになります。
#!/usr/bin/env ruby
require File.expand_path('../../lib/listener/init.rb', __FILE__)
log_file = File.expand_path('../../log/listener.stdout.log', __FILE__)
pid_file = File.expand_path('../../tmp/listener.pid', __FILE__)
listener = Dante::Runner.new('listener')
if ARGV[0] === 'start'
listener.execute(daemonize: true,
pid_path: pid_file,
log_path: log_file) { Listener.run }
elsif ARGV[0] === 'restart'
listener.execute(daemonize: true,
restart: true,
pid_path: pid_file,
log_path: log_file) { Listener.run }
elsif ARGV[0] === 'stop'
listener.execute(kill: true, pid_path: pid_file)
end
これで、次のようなコードを実行できます: ./bin/listener start
, ./bin/listener restart
,./bin/listener stop
Godを使用して、リスナーの実行を監視できます。ただし、同じ pid ファイル ( /tmp/listener.pid
) を使用していることを確認してください。