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

Commit

Permalink
Spawn a background export job when a new export is created
Browse files Browse the repository at this point in the history
  • Loading branch information
escowles committed Jan 27, 2017
1 parent a87b128 commit 9824808
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ RSpec/MessageChain:
RSpec/MultipleExpectations:
Enabled: false

RSpec/VerifiedDoubles:
Exclude:
- 'spec/services/grocer/export_object_spec.rb'

Style/Documentation:
Enabled: false

Expand Down
79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,81 @@
# grocer
Backup manager for Fedora's produce.

## Installation

* Add Grocer to your Gemfile

```
gem 'grocer', github: 'pulibrary/grocer'
```

* Then run the Grocer installation

```
bundle install
rails g grocer:install
rake db:migrate
```

* If your application is based on CurationConcerns 2.0 or Hyrax, you can trigger a Grocer export on
workflow changes by adding this service call to an action:

```
"methods": ["Grocer::ExportObject"]
```

and then reloading your workflow:

```
rake curation_concerns:workflow:load
```


## Configuration

To configure Grocer's behavior, you can add an initializer:

```
Grocer.configure do |conf|
conf.export_dir = ENV['GROCER_EXPORT_DIR'] || Rails.root.join('tmp', 'export')
end
```

The available configuration options are:

* `baseurl`: Fedora base URL, defaults to using ActiveFedora settings

* `export_dir`: Base directory to export resources to, defaults to `/pub/export`

* `metadata_template`: YAML file defining Bag metadata, defaults to `[export_dir]/metadata.yml`

* `jar`: Import/Export Java JAR file, defaults to `[export_dir]/export.jar`

* `predicates`: Comma-separated list of membership predicates to follow when retrieving a resources,
defaults to `pcdm:hasMember,ldp:contains`

See the [Fedora Import/Export README](https://github.com/fcrepo4-labs/fcrepo-import-export) for more
information about BagIt metadata, membership predicates, and the Import/Export tool in general.

## Required files

You must create two files required by the import/export tool:

* The Import/Export JAR file, which can be downloaded from the [Import/Export releases]
(https://github.com/fcrepo4-labs/fcrepo-import-export/releases).

* A Bag metadata config file, such as:

```
bag-info.txt:
Source-Organization: York University Libraries
Organization-Address: 4700 Keele Street Toronto, Ontario M3J 1P3 Canada
Contact-Name: Nick Ruest
Contact-Phone: +14167362100
Contact-Email: ruestn@yorku.ca
External-Description: Sample bag exported from fcrepo
External-Identifier: SAMPLE_001
Bag-Group-Identifier: SAMPLE
Internal-Sender-Identifier: SAMPLE_001
Internal-Sender-Description: Sample bag exported from fcrepo
```
3 changes: 2 additions & 1 deletion app/controllers/grocer/exports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def create
@export = Export.new(export_params)

if @export.save
Grocer::ExportJob.perform_later(@export.pid, @export.ark)
redirect_to @export, notice: 'Export was successfully created.'
else
render :new
Expand All @@ -37,7 +38,7 @@ def set_export
# Only allow a trusted parameter "white list" through.
def export_params
params.require(:export).permit(:pid, :job, :status, :last_error, :last_success, :logfile,
:page)
:page, :ark)
end

def page
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/grocer/export_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def export(pid, url, dir, ark = nil)
log ? export.error!(log.path) : export.success!
end
ensure
File.delete(meta.path)
File.delete(meta.path) if meta
end

def monitor_process(out, wait_thread)
Expand Down
8 changes: 8 additions & 0 deletions app/services/grocer/export_object.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module Grocer
class ExportObject
def self.call(target:, user: nil, comment: nil)
Rails.logger.info "Exporting #{target.id}, user: #{user}, comment: #{comment}"
Grocer::ExportJob.perform_later(target.id, target.identifier)
end
end
end
1 change: 0 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
Grocer::Engine.routes.draw do
resources :exports, only: [:create, :index, :new, :show]
root to: 'exports#index'
end
5 changes: 5 additions & 0 deletions db/migrate/20170124154627_add_ark_to_grocer_exports.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddArkToGrocerExports < ActiveRecord::Migration[5.0]
def change
add_column :grocer_exports, :ark, :string
end
end
5 changes: 4 additions & 1 deletion spec/controllers/grocer/exports_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,21 @@
describe 'POST #create' do
context 'with valid params' do
it 'creates a new Grocer::Export' do
allow(Grocer::ExportJob).to receive(:perform_later).with('pid4', 'ark4')
expect do
post :create, params: { export: { pid: 'pid4' } }, session: valid_session
post :create, params: { export: { pid: 'pid4', ark: 'ark4' } }, session: valid_session
end.to change(Grocer::Export, :count).by(1)
end

it 'assigns a newly created export as @export' do
allow(Grocer::ExportJob).to receive(:perform_later)
post :create, params: { export: { pid: 'pid5' } }, session: valid_session
expect(assigns(:export)).to be_a(Grocer::Export)
expect(assigns(:export)).to be_persisted
end

it 'redirects to the created export' do
allow(Grocer::ExportJob).to receive(:perform_later)
post :create, params: { export: { pid: 'pid6' } }, session: valid_session
expect(response).to redirect_to(Grocer::Export.last)
end
Expand Down
15 changes: 15 additions & 0 deletions spec/services/grocer/export_object_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'

RSpec.describe Grocer::ExportObject do
let(:obj) { double('object', id: 'obj1', identifier: 'ark:/12345/x12345678x') }
let(:job) { class_double(Grocer::ExportJob).as_stubbed_const }

before do
allow(job).to receive(:perform_later)
end

it 'triggers an export job' do
described_class.call(target: obj)
expect(job).to have_received(:perform_later).with('obj1', 'ark:/12345/x12345678x')
end
end

0 comments on commit 9824808

Please sign in to comment.