Sha256: e779f0a93858a04fb6aab3b08355b57f85a4781a086f6f2f79875d7a5a3f4faa

Contents?: true

Size: 1.98 KB

Versions: 3

Compression:

Stored size: 1.98 KB

Contents

# -*- coding: utf-8 -*-
# This writer uses sidekiq to enqueue index jobs
# @author Michael Stämpfli and John Bradley

require 'sidekiq'

module XapianDb
  module IndexWriters
    class SidekiqWriter

      SidekiqWorker.class_eval do
        include Sidekiq::Worker

        sidekiq_options set_max_expansion: set_max_expansion
      end

      class << self
        def queue
          XapianDb::Config.sidekiq_queue
        end

        # Update an object in the index
        # @param [Object] obj An instance of a class with a blueprint configuration
        def index(obj, _commit= true, changed_attrs: [])
          Sidekiq::Client.push('queue' => queue,
                               'class' => worker_class,
                               'args' => ['index', { class: obj.class.name, id: obj.id, changed_attrs: changed_attrs }.to_json],
                               'retry' => sidekiq_retry)
        end

        # Remove an object from the index
        # @param [String] xapian_id The document id
        def delete_doc_with(xapian_id, _commit= true)
          Sidekiq::Client.push('queue' => queue,
                               'class' => worker_class,
                               'args' => ['delete_doc', { xapian_id: xapian_id }.to_json],
                               'retry' => sidekiq_retry)
        end

        # Reindex all objects of a given class
        # @param [Class] klass The class to reindex
        def reindex_class(klass, _options = {})
          Sidekiq::Client.push('queue' => queue,
                               'class' => worker_class,
                               'args' => ['reindex_class', { class: klass.name }.to_json],
                               'retry' => sidekiq_retry)
        end

        def set_max_expansion
          XapianDb::Config.set_max_expansion
        end

        def sidekiq_retry
          XapianDb::Config.sidekiq_retry
        end

        def worker_class
          SidekiqWorker
        end
        private :worker_class
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
xapian_db-1.3.15 lib/xapian_db/index_writers/sidekiq_writer.rb
xapian_db-1.3.14 lib/xapian_db/index_writers/sidekiq_writer.rb
xapian_db-1.3.13 lib/xapian_db/index_writers/sidekiq_writer.rb