diff options
Diffstat (limited to 'lib/tasks/collector.rake')
-rw-r--r-- | lib/tasks/collector.rake | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/lib/tasks/collector.rake b/lib/tasks/collector.rake new file mode 100644 index 0000000..3030e88 --- /dev/null +++ b/lib/tasks/collector.rake @@ -0,0 +1,68 @@ +namespace :collector do + @pid_file = Rails.root.join("tmp", "pids", "collector.pid").to_s + @log_file = Rails.root.join("log", "collector.log").to_s + + def read_pid + Integer(File.read(@pid_file)) rescue nil + end + + def process_alive?(pid) + Process.kill(0, pid) rescue false + end + + desc "Start aclog collector (master) in the foreground" + task run: :environment do + Collector::Daemon.start + end + + desc "Start aclog collector (master)" + task start: :environment do + pid = read_pid + if pid && process_alive?(pid) + STDERR.puts "Collector daemon is already started (PID: #{pid})" + next + end + + Process.daemon + File.open(@pid_file, "w").write(Process.pid) + + log = File.open(@log_file, "a") + log.sync = true + STDOUT.reopen(log) + STDERR.reopen(STDOUT) + + Collector::Daemon.start + end + + desc "Stop aclog collector (master)" + task :stop do + pid = read_pid + unless process_alive?(pid) + puts "Collector daemon is not started." + next + end + + Process.kill("TERM", pid) + while process_alive?(pid) + sleep 0.1 + end + + File.delete(@pid_file) + end + + desc "Retart aclog collector (master)" + task :restart do + Rake::Task["collector:stop"].invoke + Rake::Task["collector:start"].invoke + end + + desc "Show status of running aclog collector (master)" + task :status do + pid = read_pid + if pid && process_alive?(pid) + puts "Collector is running." + else + puts "Collector is not running." + end + end +end |