Class: PgbackupsArchive::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/pgbackups-archive/job.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Job

Returns a new instance of Job.



15
16
17
18
# File 'lib/pgbackups-archive/job.rb', line 15

def initialize(attrs={})
  Heroku::Command.load
  @client = Heroku::Command::Pg.new([], :app => ENV["PGBACKUPS_APP"])
end

Instance Attribute Details

#backup_urlObject

Returns the value of attribute backup_url.



9
10
11
# File 'lib/pgbackups-archive/job.rb', line 9

def backup_url
  @backup_url
end

#clientObject (readonly)

Returns the value of attribute client.



8
9
10
# File 'lib/pgbackups-archive/job.rb', line 8

def client
  @client
end

#created_atObject

Returns the value of attribute created_at.



9
10
11
# File 'lib/pgbackups-archive/job.rb', line 9

def created_at
  @created_at
end

Class Method Details

.callObject



11
12
13
# File 'lib/pgbackups-archive/job.rb', line 11

def self.call
  new.call
end

Instance Method Details

#archiveObject



28
29
30
31
32
# File 'lib/pgbackups-archive/job.rb', line 28

def archive
  if PgbackupsArchive::Storage.new(key, file).store
    client.display "Backup archived"
  end
end

#callObject



20
21
22
23
24
25
26
# File 'lib/pgbackups-archive/job.rb', line 20

def call
  # expire  # Disabled b/c Heroku seems to be keeping only 2 on its own
  capture
  download
  archive
  delete
end

#captureObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/pgbackups-archive/job.rb', line 34

def capture
  attachment = client.send(:generate_resolver).resolve(database)
  backup = client.send(:hpg_client, attachment).backups_capture
  client.send(:poll_transfer, "backup", backup[:uuid])

  self.created_at = backup[:created_at]

  self.backup_url = Heroku::Client::HerokuPostgresqlApp
    .new(ENV["PGBACKUPS_APP"]).transfers_public_url(backup[:num])[:url]
end

#deleteObject



45
46
47
# File 'lib/pgbackups-archive/job.rb', line 45

def delete
  File.delete(temp_file)
end

#downloadObject



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pgbackups-archive/job.rb', line 49

def download
  File.open(temp_file, "wb") do |output|
    streamer = lambda do |chunk, remaining_bytes, total_bytes|
      output.write chunk
    end

    # https://github.com/excon/excon/issues/475
    Excon.get backup_url,
      :response_block    => streamer,
      :omit_default_port => true
  end
end

#expireObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/pgbackups-archive/job.rb', line 62

def expire
  transfers = client.send(:hpg_app_client, ENV["PGBACKUPS_APP"]).transfers
    .select  { |b| b[:from_type] == "pg_dump" && b[:to_type] == "gof3r" }
    .sort_by { |b| b[:created_at] }

  if transfers.size > pgbackups_to_keep
    backup_id  = "b%03d" % transfers.first[:num]
    backup_num = client.send(:backup_num, backup_id)

    expire_backup(backup_num)

    client.display "Backup #{backup_id} expired"
  end
end