Skip to content

Instantly share code, notes, and snippets.

@marzzz21
Created August 29, 2016 07:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marzzz21/9282c6b447f1db5b07e34c15686b36fe to your computer and use it in GitHub Desktop.
Save marzzz21/9282c6b447f1db5b07e34c15686b36fe to your computer and use it in GitHub Desktop.
#code
def check_status
if File.exist?(PID_FILE)
pid = File.read(PID_FILE).to_i
check_pid_status(pid) if pid > 0
File.delete(PID_FILE)
end
end
def check_pid_status(pid)
Process.kill(0, pid)
puts "Process(#{pid}) is already running."
exit(1)
rescue Errno::ESERCH #do nothing, and continue with the script
rescue Errno::EPERM
puts "Process with pid=#{pid} is already running but is owned by a different user"
exit(1)
end
#test
require 'spec_helper'
require 'mirrorsyncdaemon'
RSpec.describe MirrorSyncDaemon do
it 'deletes the PID_FILE if it exists but contains invalid PID' do
stub_const("MirrorSyncDaemon::PID_FILE", File.open('test_file', ::File::CREAT|::File::TRUNC|::File::RDWR, 0644) { |f| f.write('123') } )
@mirrorsyncdaemon = MirrorSyncDaemon.new
@mirrorsyncdaemon.stub(:check_pid_status).with(Integer) { raise Errno::ESERCH }
@mirrorsyncdaemon.check_status
expect(MirrorSyncDaemon::PID_FILE).to_not exist
end
end
#error
Failures:
1) MirrorSyncDaemon deletes the PID_FILE if it exists but contains invalid PID
Failure/Error: if File.exist?(PID_FILE)
TypeError:
no implicit conversion of Fixnum into String
# ./lib/mirrorsyncdaemon.rb:124:in `exist?'
# ./lib/mirrorsyncdaemon.rb:124:in `check_status'
# ./spec/test_mirrorsyncdaemon.rb:10:in `block (2 levels) in <top (required)>'
Deprecation Warnings:
Using `stub` from rspec-mocks' old `:should` syntax without explicitly enabling the syntax is deprecated. Use the new `:expect` syntax or explicitly enable `:should` instead. Called from /home/mihaelkeehl/rise-chef-repo/cookbooks/mirror/files/default/scripts/spec/test_mirrorsyncdaemon.rb:9:in `block (2 levels) in <top (required)>'.
If you need more of the backtrace for any of these deprecations to
identify where to make the necessary changes, you can configure
`config.raise_errors_for_deprecations!`, and it will turn the
deprecation warnings into errors, giving you the full backtrace.
1 deprecation warning total
Finished in 0.00102 seconds (files took 0.12823 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/test_mirrorsyncdaemon.rb:6 # MirrorSyncDaemon deletes the PID_FILE if it exists but contains invalid PID
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment