Sha256: b45a6d557fb339fec44068db96cada8496477be7d84be9c7f7a674b947ab69e2

Contents?: true

Size: 1.35 KB

Versions: 7

Compression:

Stored size: 1.35 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.force_encoding('ASCII-8BIT')
        }.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

7 entries across 7 versions & 1 rubygems

Version Path
rails-cloud-tasks-0.0.6 lib/rails_cloud_tasks/adapter.rb
rails-cloud-tasks-0.0.5 lib/rails_cloud_tasks/adapter.rb
rails-cloud-tasks-0.0.4 lib/rails_cloud_tasks/adapter.rb
rails-cloud-tasks-0.0.4rc2 lib/rails_cloud_tasks/adapter.rb
rails-cloud-tasks-0.0.4.rc1 lib/rails_cloud_tasks/adapter.rb
rails-cloud-tasks-0.0.3 lib/rails_cloud_tasks/adapter.rb
rails-cloud-tasks-0.0.2 lib/rails_cloud_tasks/adapter.rb