Skip to content

Commit

Permalink
Add "BACKUP_ASYNC" env flag
Browse files Browse the repository at this point in the history
  • Loading branch information
choonkeat committed Dec 14, 2015
1 parent 3a8bae4 commit 4af1888
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ The `paths` value should be delimited by the newline character, aka `\n`. In the

If backup remote storage is not configured, this API call will be a noop. If configured, the backup storage must be accessible by the same credentials as default cloud storage as the system. Please refer to the `BACKUP_CONFIG` configuration illustrated in `config/vhost.example.yml` file in this repository.

By default, `backup` operation is performed synchronously. Set `BACKUP_ASYNC` environment variable to make it follow the same synchronicity as `delete`

The main reason to configure a backup storage is to make the default cloud storage auto expire files; mitigating [abuse](https://github.com/choonkeat/attache/issues/13). You should consult the documentation of your cloud storage provider on how to setup auto expiry, e.g. [here](https://aws.amazon.com/blogs/aws/amazon-s3-object-expiration/) or [here](https://cloud.google.com/storage/docs/lifecycle)

## License
Expand Down
11 changes: 5 additions & 6 deletions lib/attache/backup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ def _call(env, config)
params = request.params
return config.unauthorized unless config.authorized?(params)

params['paths'].to_s.split("\n").each do |relpath|
Attache.logger.info "CONFIRM local #{relpath}"
cachekey = File.join(request_hostname(env), relpath)
if config.storage && config.bucket
Attache.logger.info "CONFIRM remote #{relpath}"
config.async(:backup_file, relpath: relpath)
if config.storage && config.bucket
sync_method = (ENV['BACKUP_ASYNC'] ? :async : :send)
params['paths'].to_s.split("\n").each do |relpath|
Attache.logger.info "BACKUP remote #{relpath}"
config.send(sync_method, :backup_file, relpath: relpath)
end
end
[200, config.headers_with_cors, []]
Expand Down
6 changes: 2 additions & 4 deletions spec/lib/attache/backup_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,14 @@
end

it 'should backup file' do
expect_any_instance_of(Attache::VHost).to receive(:async) do |instance, method, path|
expect(method).to eq(:backup_file)
end.exactly(2).times
expect_any_instance_of(Attache::VHost).to receive(:backup_file).exactly(2).times
subject.call
end
end

context 'storage NOT configured' do
it 'should backup file' do
expect_any_instance_of(Attache::VHost).not_to receive(:async)
expect_any_instance_of(Attache::VHost).not_to receive(:backup_file)
subject.call
end
end
Expand Down

0 comments on commit 4af1888

Please sign in to comment.