Sha256: 781f1da69e70bb224ca4ae702b68755bbe0a340ccb8f02fc8a4b523e5db72af5

Contents?: true

Size: 1.89 KB

Versions: 10

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

module Cloudtasker
  # Handle execution of workers
  class WorkerController < ApplicationController
    # Authenticate all requests.
    before_action :authenticate!

    # Return 401 when API Token is invalid
    rescue_from AuthenticationError do
      head :unauthorized
    end

    # POST /cloudtasker/run
    #
    # Run a worker from a Cloud Task payload
    #
    def run
      # Process payload
      WorkerHandler.execute_from_payload!(payload)
      head :no_content
    rescue DeadWorkerError
      # 205: job will NOT be retried
      head :reset_content
    rescue InvalidWorkerError
      # 404: Job will be retried
      head :not_found
    rescue StandardError => e
      # 404: Job will be retried
      Cloudtasker.logger.error(e)
      Cloudtasker.logger.error(e.backtrace.join("\n"))
      head :unprocessable_entity
    end

    private

    #
    # Parse the request body and return the actual job
    # payload.
    #
    # @return [Hash] The job payload
    #
    def payload
      @payload ||= begin
        # Get raw body
        content = request.body.read

        # Decode content if the body is Base64 encoded
        if request.headers[Cloudtasker::Config::ENCODING_HEADER].to_s.downcase == 'base64'
          content = Base64.decode64(content)
        end

        # Return content parsed as JSON and add job retries count
        JSON.parse(content).merge(job_retries: job_retries)
      end
    end

    #
    # Extract the number of times this task failed at runtime.
    #
    # @return [Integer] The number of failures
    #
    def job_retries
      request.headers[Cloudtasker::Config::RETRY_HEADER].to_i
    end

    #
    # Authenticate incoming requests using a bearer token
    #
    # See Cloudtasker::Authenticator#verification_token
    #
    def authenticate!
      Authenticator.verify!(request.headers['Authorization'].to_s.split(' ').last)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
cloudtasker-0.9.4 app/controllers/cloudtasker/worker_controller.rb
cloudtasker-0.9.3 app/controllers/cloudtasker/worker_controller.rb
cloudtasker-0.10.rc5 app/controllers/cloudtasker/worker_controller.rb
cloudtasker-0.10.rc4 app/controllers/cloudtasker/worker_controller.rb
cloudtasker-0.10.rc3 app/controllers/cloudtasker/worker_controller.rb
cloudtasker-0.10.rc2 app/controllers/cloudtasker/worker_controller.rb
cloudtasker-0.10.rc1 app/controllers/cloudtasker/worker_controller.rb
cloudtasker-0.9.2 app/controllers/cloudtasker/worker_controller.rb
cloudtasker-0.9.1 app/controllers/cloudtasker/worker_controller.rb
cloudtasker-0.9.0 app/controllers/cloudtasker/worker_controller.rb