class Cloudtasker::CloudTask

An interface class to manage tasks on the backend (Cloud Task or Redis)

Attributes

dispatch_deadline[RW]
http_request[RW]
id[RW]
queue[RW]
retries[RW]
schedule_time[RW]

Public Class Methods

backend() click to toggle source

The backend to use for cloud tasks.

@return [Cloudtasker::Backend::GoogleCloudTask, Cloudtasker::Backend::RedisTask] The cloud task backend.

# File lib/cloudtasker/cloud_task.rb, line 13
def self.backend
  # Re-evaluate backend every time if testing mode enabled
  @backend = nil if defined?(Cloudtasker::Testing)

  @backend ||= begin
    if defined?(Cloudtasker::Testing) && Cloudtasker::Testing.in_memory?
      require 'cloudtasker/backend/memory_task'
      Backend::MemoryTask
    elsif Cloudtasker.config.mode.to_sym == :development
      require 'cloudtasker/backend/redis_task'
      Backend::RedisTask
    else
      require 'cloudtasker/backend/google_cloud_task'
      Backend::GoogleCloudTask
    end
  end
end
create(payload) click to toggle source

Create a new cloud task.

@param [Hash] payload Thee task payload

@return [Cloudtasker::CloudTask] The created task.

# File lib/cloudtasker/cloud_task.rb, line 50
def self.create(payload)
  raise MaxTaskSizeExceededError if payload.to_json.bytesize > Config::MAX_TASK_SIZE

  resp = backend.create(payload)&.to_h
  resp ? new(resp) : nil
end
delete(id) click to toggle source

Delete a cloud task by id.

@param [String] id The task id.

# File lib/cloudtasker/cloud_task.rb, line 62
def self.delete(id)
  backend.delete(id)
end
find(id) click to toggle source

Find a cloud task by id.

@param [String] id The id of the task.

@return [Cloudtasker::Cloudtask] The task.

# File lib/cloudtasker/cloud_task.rb, line 38
def self.find(id)
  payload = backend.find(id)&.to_h
  payload ? new(payload) : nil
end
new(id:, http_request:, schedule_time: nil, retries: 0, queue: nil, dispatch_deadline: nil) click to toggle source

Build a new instance of the class using a backend response payload.

@param [String] id The task id. @param [Hash] http_request The content of the http request. @param [Integer] schedule_time When to run the job (Unix timestamp) @param [Integer] retries The number of times the job failed. @param [String] queue The queue the task is in.

# File lib/cloudtasker/cloud_task.rb, line 76
def initialize(id:, http_request:, schedule_time: nil, retries: 0, queue: nil, dispatch_deadline: nil)
  @id = id
  @http_request = http_request
  @schedule_time = schedule_time
  @retries = retries || 0
  @queue = queue
  @dispatch_deadline = dispatch_deadline
end

Public Instance Methods

==(other) click to toggle source

Equality operator.

@param [Any] other The object to compare.

@return [Boolean] True if the object is equal.

# File lib/cloudtasker/cloud_task.rb, line 92
def ==(other)
  other.is_a?(self.class) && other.id == id
end