Skip to content

Commit f16c5a3

Browse files
pankajtanujdljvette
authored andcommitted
[SIM Fix] Fix PID file 0 issue.
Prior to this change, an empty PID file would cause the status method to return that the agent was running at PID 0. This change ensures that an empty PID file will return a status of No CodeDeploy Agent Running and clean up the PID file. * Unit Tests : Y * Integration Tests : N
1 parent ce26d85 commit f16c5a3

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
require 'spec_helper'
2+
require_relative '../../../../vendor/gems/process_manager-0.0.13/lib/process_manager/master'
3+
require 'fileutils'
4+
5+
describe ProcessManager::Daemon::Master do
6+
describe "check status" do
7+
context "PID file is empty" do
8+
it "status is nil and PID file is deleted" do
9+
# Make directory
10+
file_name = ProcessManager::Daemon::Master.pid_file
11+
12+
dirname = File.dirname(file_name)
13+
unless File.directory?(dirname)
14+
FileUtils.mkdir_p(dirname)
15+
end
16+
17+
# Write empty file
18+
out_file = File.new(file_name, "w")
19+
out_file.close
20+
21+
# Check that status is equal to nil and that the PID file is deleted
22+
# Note: This used to give a status of 0 but we want it to be nil
23+
expect(ProcessManager::Daemon::Master.status).to eq(nil)
24+
expect(File.exist?(file_name)).to eq(false)
25+
26+
# Clean up directory
27+
FileUtils.remove_dir(dirname) if File.directory?(dirname)
28+
end
29+
end
30+
31+
context "PID file has a process that is running" do
32+
it "status is the PID number" do
33+
# Make directory
34+
file_name = ProcessManager::Daemon::Master.pid_file
35+
36+
dirname = File.dirname(file_name)
37+
unless File.directory?(dirname)
38+
FileUtils.mkdir_p(dirname)
39+
end
40+
41+
# Write empty file
42+
out_file = File.new(file_name, "w")
43+
File.write(file_name, $$) # Using $$ to mock a running process
44+
out_file.close
45+
46+
expect(ProcessManager::Daemon::Master.status).to eq($$)
47+
48+
# Clean up and delete the file and directory
49+
File.delete(file_name) if File.exist?(file_name)
50+
FileUtils.remove_dir(dirname) if File.directory?(dirname)
51+
end
52+
end
53+
end
54+
end

vendor/gems/process_manager-0.0.13/lib/process_manager/master.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def self.restart
5151

5252
def self.status
5353
if pid = find_pid
54-
if ProcessManager::process_running?(pid)
54+
if pid > 0 and ProcessManager::process_running?(pid)
5555
pid
5656
else
5757
clean_stale_pid

0 commit comments

Comments
 (0)