Sha256: bf150e63add1e9bec0d5d7e49e42d12bdecde401beea788d0962b7687da3240f

Contents?: true

Size: 899 Bytes

Versions: 6

Compression:

Stored size: 899 Bytes

Contents

require 'json'
require 'rack'

module RailsCloudTasks
  module Rack
    class Tasks
      class << self
        def call(env)
          request = ::Rack::Request.new(env)
          job = extract_job(request)

          RailsCloudTasks::Instrumentation.transaction_name!(
            "RailsCloudTasks/#{job['job_class']}/perform_now"
          )

          ActiveJob::Base.execute(job)
          response(200, {})
        rescue Rack::InvalidPayloadError => e
          response(400, { error: e.cause.message })
        end

        private

        def extract_job(request)
          body = request.body.read
          JSON.parse(body).fetch('job')
        rescue JSON::ParserError, KeyError
          raise Rack::InvalidPayloadError
        end

        def response(status, body)
          [status, { 'Content-Type' => 'application/json' }, [body.to_json]]
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rails-cloud-tasks-0.0.9 lib/rails_cloud_tasks/rack/tasks.rb
rails-cloud-tasks-0.0.9.pre.rc lib/rails_cloud_tasks/rack/tasks.rb
rails-cloud-tasks-0.0.8 lib/rails_cloud_tasks/rack/tasks.rb
rails-cloud-tasks-0.0.7 lib/rails_cloud_tasks/rack/tasks.rb
rails-cloud-tasks-0.0.7.pre.rc lib/rails_cloud_tasks/rack/tasks.rb
rails-cloud-tasks-0.0.6 lib/rails_cloud_tasks/rack/tasks.rb