Skip to content

Commit

Permalink
Merge pull request #1169 from dunn/file-locks
Browse files Browse the repository at this point in the history
jobs: lock FileSets while attaching Files
  • Loading branch information
jeremyf committed Mar 23, 2017
2 parents 7a98b7d + 6c4402b commit 0915969
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
language: ruby
cache: bundler
sudo: false

rvm:
- 2.3.1

services: redis

env:
global:
- NOKOGIRI_USE_SYSTEM_LIBRARIES=true
Expand All @@ -11,5 +15,6 @@ env:
RDF_VERSION=1.99.1
- RAILS_VERSION=5.0.0.1
RDF_VERSION=2.1.1

before_script:
- jdk_switcher use oraclejdk8
17 changes: 12 additions & 5 deletions app/jobs/characterize_job.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class CharacterizeJob < ActiveJob::Base
include CurationConcerns::Lockable

queue_as CurationConcerns.config.ingest_queue_name

# @param [FileSet] file_set
Expand All @@ -7,11 +9,16 @@ class CharacterizeJob < ActiveJob::Base
def perform(file_set, file_id, filepath = nil)
filename = CurationConcerns::WorkingDirectory.find_or_retrieve(file_id, file_set.id, filepath)
raise LoadError, "#{file_set.class.characterization_proxy} was not found" unless file_set.characterization_proxy?
Hydra::Works::CharacterizationService.run(file_set.characterization_proxy, filename)
Rails.logger.debug "Ran characterization on #{file_set.characterization_proxy.id} (#{file_set.characterization_proxy.mime_type})"
file_set.characterization_proxy.save!
file_set.update_index
file_set.parent.in_collections.each(&:update_index) if file_set.parent

# Prevent other jobs from trying to modify the FileSet at the same time
acquire_lock_for(file_set.id) do
Hydra::Works::CharacterizationService.run(file_set.characterization_proxy, filename)
Rails.logger.debug "Ran characterization on #{file_set.characterization_proxy.id} (#{file_set.characterization_proxy.mime_type})"
file_set.characterization_proxy.save!
file_set.update_index
file_set.parent.in_collections.each(&:update_index) if file_set.parent
end

CreateDerivativesJob.perform_later(file_set, file_id, filename)
end
end
15 changes: 10 additions & 5 deletions app/jobs/create_derivatives_job.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class CreateDerivativesJob < ActiveJob::Base
include CurationConcerns::Lockable

queue_as CurationConcerns.config.ingest_queue_name

# @param [FileSet] file_set
Expand All @@ -8,12 +10,15 @@ def perform(file_set, file_id, filepath = nil)
return if file_set.video? && !CurationConcerns.config.enable_ffmpeg
filename = CurationConcerns::WorkingDirectory.find_or_retrieve(file_id, file_set.id, filepath)

file_set.create_derivatives(filename)
# Prevent other jobs from trying to modify the FileSet at the same time
acquire_lock_for(file_set.id) do
file_set.create_derivatives(filename)

# Reload from Fedora and reindex for thumbnail and extracted text
file_set.reload
file_set.update_index
file_set.parent.update_index if parent_needs_reindex?(file_set)
# Reload from Fedora and reindex for thumbnail and extracted text
file_set.reload
file_set.update_index
file_set.parent.update_index if parent_needs_reindex?(file_set)
end
end

# If this file_set is the thumbnail for the parent work,
Expand Down
22 changes: 13 additions & 9 deletions app/jobs/ingest_file_job.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class IngestFileJob < ActiveJob::Base
include CurationConcerns::Lockable

queue_as CurationConcerns.config.ingest_queue_name

# @param [FileSet] file_set
Expand All @@ -15,15 +17,17 @@ def perform(file_set, filepath, user, opts = {})
local_file.mime_type = opts.fetch(:mime_type, nil)
local_file.original_name = opts.fetch(:filename, File.basename(filepath))

# Tell AddFileToFileSet service to skip versioning because versions will be minted by
# VersionCommitter when necessary during save_characterize_and_record_committer.
Hydra::Works::AddFileToFileSet.call(file_set,
local_file,
relation,
versioning: false)

# Persist changes to the file_set
file_set.save!
# Prevent other jobs from trying to modify the FileSet at the same time
acquire_lock_for(file_set.id) do
# Tell AddFileToFileSet service to skip versioning because versions will be minted by
# VersionCommitter when necessary during save_characterize_and_record_committer.
Hydra::Works::AddFileToFileSet.call(file_set,
local_file,
relation,
versioning: false)
# Persist changes to the file_set
file_set.save!
end

repository_file = file_set.send(relation)

Expand Down

0 comments on commit 0915969

Please sign in to comment.