aboutsummaryrefslogtreecommitdiffstats
path: root/script/runner
blob: 32a0e6b7e26f71071d9386492cdeac90ecfbd4af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env ruby

daemon_mode = !ARGV.empty? && ARGV[0] == "--daemon"

script_dir = File.dirname(__FILE__)
alaveteli_dir = File.expand_path(File.join(script_dir, ".."))

Dir.chdir(alaveteli_dir) do
    if daemon_mode
        # Run in daemon mode.

        # If the environment variable LOGFILE is present,
        # redirect STDERR and STDOUT to that file.
        if ENV.has_key? "LOGFILE"
            STDERR.reopen(STDOUT.reopen(ENV["LOGFILE"], "a"))
            STDOUT.sync=true
            puts "Daemon starting at #{Time.new}"
        end

        # Load the runner in a subprocess
        pid = fork do
            exec("bundle exec rails runner #{ARGV[1]}")
            exit 0
        end

        # If the environment variable PIDFILE is present,
        # write the pid of the daemon process to that file.

        if ENV.has_key? "PIDFILE"
            File.open(ENV["PIDFILE"], 'w') do |fh|
                fh.puts pid
            end
        end

        Process.detach(pid)
    else
        # Not daemon mode
        exec("bundle exec rails runner #{ARGV[1]}")
    end
end