Sha256: 921720b04478d250ed95917305d6c9dc89cc8e4030d9953678d7be6e48c3f574

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

require 'google-cloud-tasks'

module RailsCloudTasks
  class Adapter
    attr_reader :client

    delegate :project_id, :location_id, :host, :tasks_path, :auth, to: 'RailsCloudTasks.config'

    def initialize(client = Google::Cloud::Tasks.cloud_tasks)
      @client = client
    end

    def enqueue(job, timestamp = nil)
      path = client.queue_path(project: project_id, location: location_id, queue: job.queue_name)
      task = build_task(job, timestamp)

      begin
        client.create_task(parent: path, task: task)
      rescue Google::Cloud::FailedPreconditionError => e
        raise e if e.details != 'Queue does not exist.'

        client.create_queue(build_queue(path))
        retry
      end
    end

    def enqueue_at(job, timestamp)
      enqueue(job, timestamp.to_i)
    end

    private

    def url
      "#{host}#{tasks_path}"
    end

    def build_task(job, timestamp)
      {
        http_request:  {
          http_method: :POST,
          url:         url,
          body:        { job: job.serialize }.to_json
        }.merge(auth),
        schedule_time: timestamp && Google::Protobuf::Timestamp.new.tap do |ts|
          ts.seconds = timestamp
        end
      }.compact
    end

    def build_queue(path)
      {
        parent: path.split('/queues').first,
        queue:  { name: path }
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails-cloud-tasks-0.0.1 lib/rails_cloud_tasks/adapter.rb