From 6f880bd026fd9463356438c7abf0ccc816d008d5 Mon Sep 17 00:00:00 2001 From: Jens Kraemer Date: Thu, 15 Jun 2017 11:45:24 +0800 Subject: [PATCH 5/5] moves text extraction to an ActiveJob Job. - without further configuration, this will run inline as before - users may setup DelayedJob or any other ActiveJob backend to profit from background text extraction. Conflicts: app/models/attachment.rb --- app/jobs/extract_fulltext_job.rb | 13 +++++++++++++ app/models/attachment.rb | 22 +++++++++++----------- test/jobs/extract_fulltext_job_test.rb | 23 +++++++++++++++++++++++ 3 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 app/jobs/extract_fulltext_job.rb create mode 100644 test/jobs/extract_fulltext_job_test.rb diff --git a/app/jobs/extract_fulltext_job.rb b/app/jobs/extract_fulltext_job.rb new file mode 100644 index 000000000..aaa716d7d --- /dev/null +++ b/app/jobs/extract_fulltext_job.rb @@ -0,0 +1,13 @@ +class ExtractFulltextJob < ActiveJob::Base + queue_as :text_extraction + + def perform(attachment_id) + if att = Attachment.find_by_id(attachment_id) and + att.readable? and + text = Redmine::TextExtractor.new(att).text + + att.update_column :fulltext, text + end + end + +end diff --git a/app/models/attachment.rb b/app/models/attachment.rb index df7516581..87d26ee1b 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -422,15 +422,6 @@ class Attachment < ActiveRecord::Base digest.size < 64 ? "MD5" : "SHA256" if digest.present? end - def extract_fulltext - if Redmine::Configuration['enable_fulltext_search'] and - readable? and - text = Redmine::TextExtractor.new(self).text - - update_column :fulltext, text - end - end - private def reuse_existing_file_if_possible @@ -489,10 +480,19 @@ class Attachment < ActiveRecord::Base time.strftime("%Y/%m") end + def extract_fulltext + if Redmine::Configuration['enable_fulltext_search'] + ExtractFulltextJob.perform_later(id) + end + end + + # attempts to extract the fulltext of any attachments that do not have a + # fulltext set currently. This runs inline, does not enqueue any background + # jobs. def self.extract_fulltext if Redmine::Configuration['enable_fulltext_search'] - Attachment.where(fulltext: nil).find_in_batches do |group| - group.each{|a| a.extract_fulltext} + Attachment.where(fulltext: nil).pluck(:id).each do |id| + ExtractFulltextJob.perform_now id end else logger.info "fulltext search is disabled, check configuration.yml" diff --git a/test/jobs/extract_fulltext_job_test.rb b/test/jobs/extract_fulltext_job_test.rb new file mode 100644 index 000000000..ed4b66606 --- /dev/null +++ b/test/jobs/extract_fulltext_job_test.rb @@ -0,0 +1,23 @@ +require 'test_helper' + +class ExtractFulltextJobTest < ActiveJob::TestCase + + def test_should_extract_fulltext + att = nil + Redmine::Configuration.with 'enable_fulltext_search' => false do + att = Attachment.create( + :container => Issue.find(1), + :file => uploaded_test_file("testfile.txt", "text/plain"), + :author => User.find(1), + :content_type => 'text/plain') + end + att.reload + assert_nil att.fulltext + + ExtractFulltextJob.perform_now(att.id) + + att.reload + assert att.fulltext.include?("this is a text file for upload tests\r\nwith multiple lines") + end + +end -- 2.11.0