Class: Dumper::Job
- Inherits:
-
Object
- Object
- Dumper::Job
- Includes:
- Utility::LoggingMethods, POSIX::Spawn
- Defined in:
- lib/dumper/job.rb
Constant Summary collapse
- MAX_FILESIZE =
4.gigabytes
Instance Method Summary collapse
- #abort_with(text, code = nil) ⇒ Object
-
#initialize(agent, job) ⇒ Job
constructor
A new instance of Job.
- #perform(server) ⇒ Object
- #run_and_exit ⇒ Object
-
#upload_to_s3(url, fields) ⇒ Object
Upload.
Methods included from Utility::LoggingMethods
#log, #log_last_error, #logger, #stdout_logger
Constructor Details
#initialize(agent, job) ⇒ Job
Returns a new instance of Job.
10 11 12 13 14 |
# File 'lib/dumper/job.rb', line 10 def initialize(agent, job) @agent = agent @stack = agent.stack @job = job end |
Instance Method Details
#abort_with(text, code = nil) ⇒ Object
97 98 99 100 101 102 103 104 |
# File 'lib/dumper/job.rb', line 97 def abort_with(text, code=nil) log text @database.try(:finalize) if code @agent.api_request('backup/fail', :params => { :backup_id => @backup_id, :code => code.to_s, :message => text }) end exit! end |
#perform(server) ⇒ Object
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 |
# File 'lib/dumper/job.rb', line 26 def perform(server) # Initialize database case server[:type] when 'mysql' @database = Dumper::Database::MySQL.new(@stack) when 'mongodb' @database = Dumper::Database::MongoDB.new(@stack) when 'redis' @database = Dumper::Database::Redis.new(@stack) else abort_with "invalid server type: #{server[:type]}" end # Prepare json = @agent.api_request('backup/prepare', :params => { :server_id => server[:id], :manual => server[:manual].to_s, :ext => @database.file_ext }) abort_with('backup/prepare failed') unless json[:status] == 'ok' @backup_id = json[:backup][:id] # Dump start_at = Time.now @database.tmpdir = Dir.mktmpdir @database.filename = json[:backup][:filename] log 'starting backup...' log "tmpdir = #{@database.tmpdir}, filename = #{@database.filename}" log "command = #{@database.command}" begin pid, stdin, stdout, stderr = popen4(@database.command) stdin.close rescue Process.kill(:INT, pid) rescue SystemCallError abort_with("dump error: #{$!}", :dump_error) ensure [stdin, stdout, stderr].each{|io| io.close unless io.closed? } Process.waitpid(pid) end dump_duration = Time.now - start_at log "dump_duration = #{dump_duration}" if (filesize = File.size(@database.dump_path)) > MAX_FILESIZE abort_with("max filesize exceeded: #{filesize}", :too_large) end upload_to_s3(json[:url], json[:fields]) json = @agent.api_request('backup/commit', :params => { :backup_id => @backup_id, :dump_duration => dump_duration.to_i }) rescue log_last_error ensure @database.finalize end |
#run_and_exit ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/dumper/job.rb', line 16 def run_and_exit @job[:servers].each do |server| perform(server) end ensure log_last_error if $! log 'exiting...' exit!(true) # Do not use exit or abort to skip at_exit execution, or pid could get deleted on thin end |
#upload_to_s3(url, fields) ⇒ Object
Upload
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/dumper/job.rb', line 80 def upload_to_s3(url, fields) require 'net/http/post/multipart' fields['file'] = UploadIO.new(@database.dump_path, 'application/octet-stream', @database.filename) uri = URI.parse(url) request = Net::HTTP::Post::Multipart.new uri.path, fields http = Net::HTTP.new(uri.host, uri.port) if uri.is_a? URI::HTTPS http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end response = http.request(request) log "response from S3 = #{response.to_s}" response rescue log_last_error end |