Class: Esse::AsyncIndexing::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/esse/async_indexing/worker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker_class, service: nil, **options) ⇒ Worker

Returns a new instance of Worker.



9
10
11
12
13
14
# File 'lib/esse/async_indexing/worker.rb', line 9

def initialize(worker_class, service: nil, **options)
  @worker_class = worker_class
  @service = service
  @options = options
  @payload = {}
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



7
8
9
# File 'lib/esse/async_indexing/worker.rb', line 7

def arguments
  @arguments
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/esse/async_indexing/worker.rb', line 5

def options
  @options
end

#payloadObject (readonly)

Returns the value of attribute payload.



5
6
7
# File 'lib/esse/async_indexing/worker.rb', line 5

def payload
  @payload
end

#serviceObject (readonly)

Returns the value of attribute service.



5
6
7
# File 'lib/esse/async_indexing/worker.rb', line 5

def service
  @service
end

#worker_classObject (readonly)

Returns the value of attribute worker_class.



5
6
7
# File 'lib/esse/async_indexing/worker.rb', line 5

def worker_class
  @worker_class
end

Class Method Details

.coerce(service:, payload:, **opts) ⇒ Object



16
17
18
# File 'lib/esse/async_indexing/worker.rb', line 16

def self.coerce(service:, payload:, **opts)
  Esse::AsyncIndexing::SERVICES.fetch(service).coerce_to_worker(payload, **opts)
end

Instance Method Details

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


76
77
78
79
80
81
82
# File 'lib/esse/async_indexing/worker.rb', line 76

def eql?(other)
  return false unless other.is_a?(self.class)

  worker_class == other.worker_class &&
    payload == other.payload &&
    options == other.options
end

#in(timestamp) ⇒ Object Also known as: at

Schedule the time when a job will be executed. Jobs which are scheduled in the past are enqueued for immediate execution.

Parameters:

  • timestamp (Numeric)

    timestamp, numeric or something that acts numeric.

Returns:

  • self



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/esse/async_indexing/worker.rb', line 46

def in(timestamp)
  now = Time.now.to_f
  timestamp = Time.parse(timestamp) if timestamp.is_a?(String)
  int = timestamp.respond_to?(:strftime) ? timestamp.to_f : now + timestamp.to_f
  return self if int <= now

  @payload["at"] = int
  @payload["created_at"] = now

  self
end

#pushObject

Returns Response of service.

Returns:

  • Response of service

See Also:

  • for more details


67
68
69
70
71
72
73
74
# File 'lib/esse/async_indexing/worker.rb', line 67

def push
  unless Esse::AsyncIndexing::SERVICES.key?(service)
    raise Esse::AsyncIndexing::Error, format("Service %<service>p is not implemented. Please use one of #{Esse::AsyncIndexing::SERVICES.keys.map(&:inspect).join(" or ")}.", service: service)
  end
  @payload["created_at"] ||= Time.now.to_f
  worker_to_push = with_job_jid
  Esse::AsyncIndexing::SERVICES[service].push(worker_to_push)
end

#with_args(*args) ⇒ Object

Adds arguments to the job

Returns:

  • self



37
38
39
40
41
# File 'lib/esse/async_indexing/worker.rb', line 37

def with_args(*args)
  @payload["args"] = args

  self
end

#with_job_jid(jid = nil) ⇒ Object



59
60
61
62
63
# File 'lib/esse/async_indexing/worker.rb', line 59

def with_job_jid(jid = nil)
  @payload["jid"] ||= jid || Esse::AsyncIndexing.jid

  self
end