Created
November 6, 2012 05:26
-
-
Save baya/4022774 to your computer and use it in GitHub Desktop.
确保程序能够独断运行
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Dogmatism | |
def initialize(process_name) | |
@process_name = process_name | |
@uuid = SecureRandom.uuid | |
end | |
def protect | |
begin | |
if File.exists? pidfile_path | |
puts "因为已经有#{pidfile_path}存在,将立即退出进程:#{@process_name}@#{@uuid}\n" | |
# do nothing | |
else | |
create_pidfile | |
yield | |
end | |
ensure | |
clear_pidfile | |
end | |
end | |
def pidfile_path | |
File.expand_path File.join(File.dirname(__FILE__), '..', 'tmp', "#{@process_name}.pid") | |
end | |
def create_pidfile | |
File.open(pidfile_path, 'w+') {|f| f.write @uuid } | |
end | |
def clear_pidfile | |
if File.exists? pidfile_path | |
pid = File.open(pidfile_path, 'r') {|f| f.read } | |
File.delete(pidfile_path) if pid == @uuid or pid.blank? | |
end | |
end | |
end | |
def Dogmatism(process_name) | |
Dogmatism.new(process_name).protect do | |
yield | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
require 'test_helper'
class DogmatismTest < ActiveSupport::TestCase
def setup
@process_name = 'lotfarm_autobatch_bets'
@pidfile_path = File.join(Rails.root, "tmp", "#{@process_name}.pid")
File.delete(@pidfile_path) if File.exists? @pidfile_path
@Dog = Dogmatism.new(@process_name)
end
test '#pidfile_path' do
assert_equal @pidfile_path, @dog.pidfile_path
end
test '#create_pidfile' do
@dog.create_pidfile
assert File.exists?(@pidfile_path)
end
test '#clear_pidfile' do
assert_nothing_raised do
@dog.clear_pidfile
end
end
test '#protect' do
# 程序正常结束,清理pid文件
Dogmatism(@process_name) do
assert File.exists?(@pidfile_path)
1 + 1
end
assert !File.exists?(@pidfile_path)
end
end