0005-moves-text-extraction-to-an-ActiveJob-Job.patch

[new] extract fulltext in the background (if AJ is set up accordingly, otherwise its still inline) - Jens Krämer, 2018-10-26 07:35

Download (3.43 KB)

View differences:

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 = Attachment.find_by_id(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
end
app/models/attachment.rb
422 422
    digest.size < 64 ? "MD5" : "SHA256" if digest.present?
423 423
  end
424 424

  
425
  def extract_fulltext
426
    if Redmine::Configuration['enable_fulltext_search'] and
427
      readable? and
428
      text = Redmine::TextExtractor.new(self).text
429

  
430
      update_column :fulltext, text
431
    end
432
  end
433

  
434 425
  private
435 426

  
436 427
  def reuse_existing_file_if_possible
......
489 480
    time.strftime("%Y/%m")
490 481
  end
491 482

  
483
  def extract_fulltext
484
    if Redmine::Configuration['enable_fulltext_search']
485
      ExtractFulltextJob.perform_later(id)
486
    end
487
  end
488

  
489
  # attempts to extract the fulltext of any attachments that do not have a
490
  # fulltext set currently. This runs inline, does not enqueue any background
491
  # jobs.
492 492
  def self.extract_fulltext
493 493
    if Redmine::Configuration['enable_fulltext_search']
494
      Attachment.where(fulltext: nil).find_in_batches do |group|
495
        group.each{|a| a.extract_fulltext}
494
      Attachment.where(fulltext: nil).pluck(:id).each do |id|
495
        ExtractFulltextJob.perform_now id
496 496
      end
497 497
    else
498 498
      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
0
-