Feature #306 » 0006-moves-text-extraction-to-an-ActiveJob-Job.patch
| app/jobs/extract_fulltext_job.rb | ||
|---|---|---|
|
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
|
||
| app/models/attachment.rb | ||
|---|---|---|
|
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
|
||
| ... | ... | |
|
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"
|
||
| test/jobs/extract_fulltext_job_test.rb | ||
|---|---|---|
|
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
|
||