Sha256: d80635e232afa10110cde5ed7300c749cabe457b0aa100ad5743bc32b6528992

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

require "json"
require "litequeue"

module Litejob
  # Litejob::Client is responsible for pushing job payloads to the SQLite queue.
  class Client
    def initialize
      @queue = Litequeue.instance
    end

    def push(jobclass, params, options = {})
      delay = options[:delay] || 0
      attempts = options[:attempts] || 5
      queue = options[:queue]
      payload = JSON.dump({class: jobclass, params: params, attempts: attempts, queue: queue})
      atomic_push(payload, delay, queue)
    end

    def delete(id)
      payload = @queue.delete(id)
      JSON.parse(payload)
    end

    private

    def atomic_push(payload, delay, queue)
      retryable = true
      begin
        @queue.push(payload, queue: queue, delay: delay)
      rescue => exception
        # Retry once retryable exceptions
        # https://github.com/sparklemotion/sqlite3-ruby/blob/master/lib/sqlite3/errors.rb
        if retryable && exception.is_a?(SQLite3::BusyException)
          retryable = false
          retry
        else
          raise exception
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
litejob-0.2.1 lib/litejob/client.rb
litejob-0.2.0 lib/litejob/client.rb