Sha256: 618de32c7aa0916dce49a76de4b10ade39c71250f44dbb097a85ac7898f0398b

Contents?: true

Size: 1.32 KB

Versions: 4

Compression:

Stored size: 1.32 KB

Contents

require 'json'
require 'google/cloud/tasks'

module ActiveJob
  module GoogleCloudTasks
    module HTTP
      class Adapter
        def initialize(project:, location:, url:, task_options: {}, client: nil)
          @project = project
          @location = location
          @url = url
          @task_options = task_options
          @client = client
        end

        def enqueue(job, attributes = {})
          path = client.queue_path(@project, @location, job.queue_name)
          task = build_task(job, attributes)

          client.create_task path, task
        end

        def enqueue_at(job, scheduled_at)
          enqueue job, scheduled_at: scheduled_at
        end

        private

        def client
          @client ||= Google::Cloud::Tasks.new(version: :v2beta3)
        end

        def build_task(job, attributes)
          task = {
            http_request: {
              http_method: :POST,
              url: @url,
              headers: {'Content-Type' => 'application/json'},
              body: JSON.dump(job: job.serialize).force_encoding(Encoding::ASCII_8BIT),
              **@task_options
            }
          }

          task[:schedule_time] = Google::Protobuf::Timestamp.new(seconds: attributes[:scheduled_at].to_i) if attributes.has_key?(:scheduled_at)

          task
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
activejob-google_cloud_tasks-http-0.1.3 lib/active_job/google_cloud_tasks/http/adapter.rb
activejob-google_cloud_tasks-http-0.1.2 lib/active_job/google_cloud_tasks/http/adapter.rb
activejob-google_cloud_tasks-http-0.1.1 lib/active_job/google_cloud_tasks/http/adapter.rb
activejob-google_cloud_tasks-http-0.1.0 lib/active_job/google_cloud_tasks/http/adapter.rb