Sha256: 4ff4e86b254f3e759ff4cc33cdf866e54aedddb94102d690049a762acb3277e0

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require 'open3'

module TasksScheduler
  class Daemon
    ACTIONS = %w(status start stop restart).freeze

    class << self
      def run(rails_root)
        dir = File.expand_path('tmp/pids', rails_root)
        FileUtils.mkdir_p(dir)
        Daemons.run_proc 'tasks_scheduler', dir_mode: :normal, dir: dir do
          require File.join(rails_root, 'config', 'environment')
          ::TasksScheduler::Checker.instance.run
        end
      end

      def execute(action)
        raise "Action not allowed: #{action} (Allowed: #{ACTIONS})" unless ACTIONS.include?(action)
        command = ['bundle', 'exec', 'tasks_scheduler', action]
        env_args = { 'RAILS_ENV' => Rails.env }
        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
      end

      def running?
        execute('status')[:status].zero?
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tasks_scheduler-0.0.6 lib/tasks_scheduler/daemon.rb