Class: Sqewer::Submitter

Inherits:
Struct
  • Object
show all
Defined in:
lib/sqewer/submitter.rb

Overview

A shim for submitting jobs to the queue. Accepts a connection (something that responds to #send_message) and the serializer (something that responds to #serialize) to convert the job into the string that will be put in the queue.

Constant Summary collapse

NotSqewerJob =
Class.new(StandardError)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connectionObject

Returns the value of attribute connection

Returns:

  • (Object)

    the current value of connection



5
6
7
# File 'lib/sqewer/submitter.rb', line 5

def connection
  @connection
end

#serializerObject

Returns the value of attribute serializer

Returns:

  • (Object)

    the current value of serializer



5
6
7
# File 'lib/sqewer/submitter.rb', line 5

def serializer
  @serializer
end

Class Method Details

.defaultObject

Returns a default Submitter, configured with the default connection and the default serializer.



10
11
12
# File 'lib/sqewer/submitter.rb', line 10

def self.default
  new(Sqewer::Connection.default, Sqewer::Serializer.default)
end

Instance Method Details

#submit!(job, **kwargs_for_send) ⇒ Object

Raises:



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sqewer/submitter.rb', line 14

def submit!(job, **kwargs_for_send)
  raise NotSqewerJob.new("Submitted object is not a valid job: #{job.inspect}") unless job.respond_to?(:run)
  message_body = if delay_by_seconds = kwargs_for_send[:delay_seconds]
    clamped_delay = clamp_delay(delay_by_seconds)
    kwargs_for_send[:delay_seconds] = clamped_delay
    # Pass the actual delay value to the serializer, to be stored in executed_at
    serializer.serialize(job, Time.now.to_i + delay_by_seconds)
  else
    serializer.serialize(job)
  end
  connection.send_message(message_body, **kwargs_for_send)
end