summaryrefslogtreecommitdiff
path: root/test/support/tapicero_process.rb
blob: fdfb37debafa6efcc80c44b412063a61777b36b7 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
##
## TAPICERO PROCESS MANIPULATION
##

module TapiceroProcess

  public

  def self.run_with_config(config_path)
    if running?
      if config_path != config_of_running_tapicero()
        kill
        start(config_path)
      end
    else
      start(config_path)
    end
  end

  def self.start(config_path)
    Dir.chdir(base_dir) do
      other_process = fork do
        if DEBUG
          puts "bin/tapicero run -- '#{config_path}'"
          exec "bin/tapicero run -- '#{config_path}'"
        else
          exec "bin/tapicero run -- '#{config_path}' > /dev/null 2>&1"
        end
      end
      Process.detach(other_process)
      10.times do
        sleep 0.1
        break if get_pid
      end
      if !running?
        puts 'Tapicero should be running'
        exit 1
      end
    end
  end

  # kill everything, not just the ones we started
  def self.kill!
    kill # first try the pid file method
    pids = `pgrep -f 'bin/tapicero'`.strip
    if !pids.empty?
      pids.gsub!("\n", " ")
      puts "Killing all bin/tapicero #{pids}" if DEBUG
      `pkill -f 'bin/tapicero'`
    end
  end

  def self.kill
    pid = get_pid
    if pid
      puts "Killing bin/tapicero #{pid}" if DEBUG
      Process.kill("TERM", pid)
      10.times do
        sleep 0.1
        break if get_pid.nil?
      end
      if running?
        puts 'Tapicero could not be killed'
        exit 1
      end
    end
  end

  private

  def self.base_dir
    File.expand_path('../../..', __FILE__)
  end

  def self.pid_file
    '/tmp/tapicero.pid'
  end

  def self.get_pid
    if File.exists?(pid_file)
      pid = File.read(pid_file).strip
      if pid !~ /^\d+/
        puts "Bad #{pid_file}: Remove the file and try again.";
        exit(1)
      else
        return pid.to_i
      end
    else
      return nil
    end
  end

  def self.running?
    return false if !File.exists?(pid_file)
    pid = get_pid()
    begin
      Process.kill(0, pid)
      return pid
    rescue Errno::EPERM
      puts "Failed to test tapicero pid: No permission to query #{_id}!"
      exit(1)
    rescue Errno::ESRCH
      puts "Bad #{pid_file}: #{pid} is NOT running. Remove the file and try again.";
      exit(1)
    rescue
      puts "Unable to determine status for tapicero process #{pid} : #{$!}"
      exit(1)
    end
  end

  #
  # returns the path of the currently running tapicero.
  #
  def self.config_of_running_tapicero
    config = `ps -o cmd= --pid #{get_pid}`.split('--')[1]
    if config.nil?
      puts "Could not determine config file of currently running tapicero. Please kill it and rerun the tests."
    else
      return config.strip.gsub("'", '')
    end
  rescue StandardError => ex
    puts "Could not parse running tapicero options: #{ex}"
    exit 1
  end

end