Sha256: 89ca4c8a2c50e3346735e014c7e63174f8e719b097cd7768d7bbd589851b368b

Contents?: true

Size: 1.44 KB

Versions: 3

Compression:

Stored size: 1.44 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
      WorkerHandler.execute_from_payload!(
        request.params.slice(:worker, :job_id, :job_args, :job_meta).merge(
          job_retries: job_retries
        )
      )
      head :no_content
    rescue Cloudtasker::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

    #
    # 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

3 entries across 3 versions & 1 rubygems

Version Path
cloudtasker-0.5.0 app/controllers/cloudtasker/worker_controller.rb
cloudtasker-0.4.0 app/controllers/cloudtasker/worker_controller.rb
cloudtasker-0.3.0 app/controllers/cloudtasker/worker_controller.rb