lib/tasks_scheduler/daemon.rb in tasks_scheduler-0.4.0 vs lib/tasks_scheduler/daemon.rb in tasks_scheduler-0.4.1

- old
+ new

@@ -1,32 +1,36 @@ # frozen_string_literal: true -require 'open3' +require 'tasks_scheduler/app_gem' module TasksScheduler class Daemon ACTIONS = %w[status start stop restart].freeze class << self - def execute(action) + def daemon_command(action) raise "Action not allowed: #{action} (Allowed: #{ACTIONS})" unless ACTIONS.include?(action) - command = ['bundle', 'exec', 'tasks_scheduler', action] - Dir.chdir(Rails.root) do - Open3.popen3(env_args, *command) do |_stdin, stdout, stderr, wait_thr| - { action: action, env_args: env_args.map { |k, v| "#{k}=#{v}" }.join(' | '), - command: command.join(' '), status: wait_thr.value.to_i, stdout: stdout.read, - stderr: stderr.read } - end - end + ::TasksScheduler::AppGem.instance.bundle('exec', 'tasks_scheduler', action) + .envvar(::TasksScheduler::Checker::LOG_ON_FILE_ENV_KEY, '1') end + def execute(action) + command = daemon_command(action) + result = command.execute + { + action: action, env_args: env_args_to_s(command), command: command.to_s, + status: result.fetch(:exit_code), stdout: result.fetch(:stdout), + stderr: result.fetch(:stderr) + } + end + def running? execute('status')[:status].zero? end - def env_args - { 'RAILS_ENV' => Rails.env, ::TasksScheduler::Checker::LOG_ON_FILE_ENV_KEY => '1' } + def env_args_to_s(command) + command.send(:extra_options).fetch(:envvars).map { |k, v| "#{k}=#{v}" }.join(' | ') end end end end