Skip to content
This repository has been archived by the owner on Jan 26, 2022. It is now read-only.

Commit

Permalink
Add timeout options for redis backup script
Browse files Browse the repository at this point in the history
Change-Id: Ifb76085acaf9155ee3ac52f14b88b21478929ec3
  • Loading branch information
rtang committed Jun 9, 2011
1 parent fc6cadf commit f9019a1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
42 changes: 40 additions & 2 deletions redis/bin/redis_backup
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require 'logger'
require "rubygems"
require "bundler/setup"
require 'optparse'
require 'timeout'

puts "backup task start.."

Expand Down Expand Up @@ -79,6 +80,31 @@ else
end
end

# Simple version of system with timeout support
# It's called simple because it doesn't handle SIGINT and SIGQUIT
# in a standard POSIX way. But it should be enough in this case.
def timeout_system(timeout, *args)
pid = fork
if pid
# parent process
success = false
begin
success = Timeout::timeout(timeout) do
Process.waitpid(pid)
$? == 0
end
rescue Timeout::Error
Process.detach(pid)
Process.kill("KILL", pid)
raise Timeout::Error
end
success
else
# child process
exec(*args)
end
end

# backup
inst_path = config["service_base_dir"]
if File.directory? inst_path
Expand All @@ -91,9 +117,21 @@ if File.directory? inst_path
logger.info "new dir: " + new_dirname_nfs
begin
FileUtils.mkdir_p new_dirname_nfs
FileUtils.cp inst_path + "/" + dirname + "/data/dump.rdb", new_dirname_nfs
#FileUtils.cp inst_path + "/" + dirname + "/data/dump.rdb", new_dirname_nfs
cmd = "cp #{inst_path}/#{dirname}/data/dump.rdb #{new_dirname_nfs}"
if config["timeout"] == nil
# If no timeout option, set a default value
config["timeout"] = 600
end
if timeout_system(config["timeout"], cmd) == false
logger.error "Got errors when doing copy"
end
rescue => e
logger.error "Could not copy dumpfile: #{e}"
if e.is_a?(Timeout::Error)
logger.error "Timeout when doing copy"
else
logger.error "Could not copy dumpfile: #{e}"
end
next
end
end
Expand Down
1 change: 1 addition & 0 deletions redis/config/redis_backup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ service_name: redis
backup_base_dir: /mnt/appcloud
service_base_dir: /var/vcap/services/redis/instances
backup_log: /tmp/redis_backup.log
timeout: 600

0 comments on commit f9019a1

Please sign in to comment.