From 4b2305e5a04751f4387bbfe93f35c4d835a3c74f Mon Sep 17 00:00:00 2001 From: Jens Kraemer Date: Thu, 15 Jun 2017 11:45:24 +0800 Subject: [PATCH] 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 benefit from background text extraction. --- app/jobs/extract_fulltext_job.rb | 19 +++++++++++++++++++ app/models/attachment.rb | 22 +++++++++++----------- test/jobs/extract_fulltext_job_test.rb | 23 +++++++++++++++++++++++ 3 files changed, 53 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 0000000..0a9fd24 --- /dev/null +++ b/app/jobs/extract_fulltext_job.rb @@ -0,0 +1,19 @@ +class ExtractFulltextJob < ActiveJob::Base + queue_as :text_extraction + + def perform(attachment_id) + if att = find_attachment(attachment_id) and + att.readable? and + text = Redmine::TextExtractor.new(att).text + + att.update_column :fulltext, text + end + end + + private + + def find_attachment(id) + Attachment.find_by_id id + end + +end diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 0ee8637..ca9cbc1 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -415,15 +415,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 @@ -482,10 +473,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 0000000..ed4b666 --- /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.1.4