diff --git a/app/jobs/reindex_by_doi_job.rb b/app/jobs/reindex_by_doi_job.rb new file mode 100644 index 000000000..5f1a86e42 --- /dev/null +++ b/app/jobs/reindex_by_doi_job.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +class ReindexByDoiJob < ApplicationJob + queue_as :lupo_background + + def perform(doi_id, _options = {}) + doi = Doi.find_by(doi: doi_id) + return unless doi.present? + + if doi.agency == "datacite" + DataciteDoiImportInBulkJob.perform_later([doi.id], index: DataciteDoi.active_index) + else + OtherDoiImportInBulkJob.perform_later([doi.id], index: OtherDoi.active_index) + end + end +end diff --git a/spec/jobs/reindex_by_doi_job_spec.rb b/spec/jobs/reindex_by_doi_job_spec.rb new file mode 100644 index 000000000..271f99603 --- /dev/null +++ b/spec/jobs/reindex_by_doi_job_spec.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +require "rails_helper" + +describe ReindexByDoiJob, type: :job do + let(:datacite_doi) { create(:doi, agency: "datacite") } + let(:other_doi) { create(:doi, agency: "crossref") } + subject(:job) { ReindexByDoiJob.perform_later(datacite_doi.doi) } + + it "queues the job" do + expect { job }.to have_enqueued_job(ReindexByDoiJob).on_queue( + "test_lupo_background", + ) + end + + it "queues DataciteDoiImportInBulkJob for agency 'datacite'" do + ReindexByDoiJob.perform_now(datacite_doi.doi) + + enqueued_job = enqueued_jobs.find { |j| j[:job] == DataciteDoiImportInBulkJob } + expect(enqueued_job).to be_present + expect(enqueued_job[:args].first).to eq([datacite_doi.id]) + end + + it "queues OtherDoiImportInBulkJob for agency 'crossref'" do + ReindexByDoiJob.perform_now(other_doi.doi) + + enqueued_job = enqueued_jobs.find { |j| j[:job] == OtherDoiImportInBulkJob } + expect(enqueued_job).to be_present + expect(enqueued_job[:args].first).to eq([other_doi.id]) + end + + after do + clear_enqueued_jobs + clear_performed_jobs + end +end