Feature #306 » 0006-moves-text-extraction-to-an-ActiveJob-Job.patch
| app/jobs/extract_fulltext_job.rb | ||
|---|---|---|
| 1 |
class ExtractFulltextJob < ActiveJob::Base |
|
| 2 |
queue_as :text_extraction |
|
| 3 | ||
| 4 |
def perform(attachment_id) |
|
| 5 |
if att = find_attachment(attachment_id) and |
|
| 6 |
att.readable? and |
|
| 7 |
text = Redmine::TextExtractor.new(att).text |
|
| 8 | ||
| 9 |
att.update_column :fulltext, text |
|
| 10 |
end |
|
| 11 |
end |
|
| 12 | ||
| 13 |
private |
|
| 14 | ||
| 15 |
def find_attachment(id) |
|
| 16 |
Attachment.find_by_id id |
|
| 17 |
end |
|
| 18 | ||
| 19 |
end |
|
| app/models/attachment.rb | ||
|---|---|---|
| 415 | 415 |
digest.size < 64 ? "MD5" : "SHA256" if digest.present? |
| 416 | 416 |
end |
| 417 | 417 | |
| 418 |
def extract_fulltext |
|
| 419 |
if Redmine::Configuration['enable_fulltext_search'] and |
|
| 420 |
readable? and |
|
| 421 |
text = Redmine::TextExtractor.new(self).text |
|
| 422 | ||
| 423 |
update_column :fulltext, text |
|
| 424 |
end |
|
| 425 |
end |
|
| 426 | ||
| 427 | 418 |
private |
| 428 | 419 | |
| 429 | 420 |
def reuse_existing_file_if_possible |
| ... | ... | |
| 482 | 473 |
time.strftime("%Y/%m")
|
| 483 | 474 |
end |
| 484 | 475 | |
| 476 |
def extract_fulltext |
|
| 477 |
if Redmine::Configuration['enable_fulltext_search'] |
|
| 478 |
ExtractFulltextJob.perform_later(id) |
|
| 479 |
end |
|
| 480 |
end |
|
| 481 | ||
| 482 |
# attempts to extract the fulltext of any attachments that do not have a |
|
| 483 |
# fulltext set currently. This runs inline, does not enqueue any background |
|
| 484 |
# jobs. |
|
| 485 | 485 |
def self.extract_fulltext |
| 486 | 486 |
if Redmine::Configuration['enable_fulltext_search'] |
| 487 |
Attachment.where(fulltext: nil).find_in_batches do |group|
|
|
| 488 |
group.each{|a| a.extract_fulltext}
|
|
| 487 |
Attachment.where(fulltext: nil).pluck(:id).each do |id|
|
|
| 488 |
ExtractFulltextJob.perform_now id
|
|
| 489 | 489 |
end |
| 490 | 490 |
else |
| 491 | 491 |
logger.info "fulltext search is disabled, check configuration.yml" |
| test/jobs/extract_fulltext_job_test.rb | ||
|---|---|---|
| 1 |
require 'test_helper' |
|
| 2 | ||
| 3 |
class ExtractFulltextJobTest < ActiveJob::TestCase |
|
| 4 | ||
| 5 |
def test_should_extract_fulltext |
|
| 6 |
att = nil |
|
| 7 |
Redmine::Configuration.with 'enable_fulltext_search' => false do |
|
| 8 |
att = Attachment.create( |
|
| 9 |
:container => Issue.find(1), |
|
| 10 |
:file => uploaded_test_file("testfile.txt", "text/plain"),
|
|
| 11 |
:author => User.find(1), |
|
| 12 |
:content_type => 'text/plain') |
|
| 13 |
end |
|
| 14 |
att.reload |
|
| 15 |
assert_nil att.fulltext |
|
| 16 | ||
| 17 |
ExtractFulltextJob.perform_now(att.id) |
|
| 18 | ||
| 19 |
att.reload |
|
| 20 |
assert att.fulltext.include?("this is a text file for upload tests\r\nwith multiple lines")
|
|
| 21 |
end |
|
| 22 | ||
| 23 |
end |
|