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

MAX_PERMITTED_MESSAGE_SIZE_BYTES =
256 * 1024
NotSqewerJob =
Class.new(Sqewer::Error)
MessageTooLarge =
Class.new(Sqewer::Error)

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.



13
14
15
# File 'lib/sqewer/submitter.rb', line 13

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

Instance Method Details

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sqewer/submitter.rb', line 17

def submit!(job, **kwargs_for_send)
  validate_job_responds_to_run!(job)
  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
  validate_message_for_size!(message_body, job)

  connection.send_message(message_body, **kwargs_for_send)
end