Class: Cloudtasker::CloudTask

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudtasker/cloud_task.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, http_request:, schedule_time: nil, retries: 0, queue: nil, dispatch_deadline: nil) ⇒ CloudTask

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

Parameters:

  • id (String)

    The task id.

  • http_request (Hash)

    The content of the http request.

  • schedule_time (Integer) (defaults to: nil)

    When to run the job (Unix timestamp)

  • retries (Integer) (defaults to: 0)

    The number of times the job failed.

  • queue (String) (defaults to: nil)

    The queue the task is in.



110
111
112
113
114
115
116
117
# File 'lib/cloudtasker/cloud_task.rb', line 110

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

Instance Attribute Details

#dispatch_deadlineObject

Returns the value of attribute dispatch_deadline.



6
7
8
# File 'lib/cloudtasker/cloud_task.rb', line 6

def dispatch_deadline
  @dispatch_deadline
end

#http_requestObject

Returns the value of attribute http_request.



6
7
8
# File 'lib/cloudtasker/cloud_task.rb', line 6

def http_request
  @http_request
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/cloudtasker/cloud_task.rb', line 6

def id
  @id
end

#queueObject

Returns the value of attribute queue.



6
7
8
# File 'lib/cloudtasker/cloud_task.rb', line 6

def queue
  @queue
end

#retriesObject

Returns the value of attribute retries.



6
7
8
# File 'lib/cloudtasker/cloud_task.rb', line 6

def retries
  @retries
end

#schedule_timeObject

Returns the value of attribute schedule_time.



6
7
8
# File 'lib/cloudtasker/cloud_task.rb', line 6

def schedule_time
  @schedule_time
end

Class Method Details

.backendBackend::MemoryTask, ...

The backend to use for cloud tasks.

] The cloud task backend.

Returns:



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cloudtasker/cloud_task.rb', line 18

def self.backend
  # Re-evaluate backend every time if testing mode enabled
  @backend = nil if defined?(Cloudtasker::Testing)

  @backend ||= 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
                 gct_backend
               end
end

.create(payload) ⇒ Cloudtasker::CloudTask

Create a new cloud task.

Parameters:

  • payload (Hash)

    Thee task payload

Returns:

Raises:



84
85
86
87
88
89
# File 'lib/cloudtasker/cloud_task.rb', line 84

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) ⇒ Object

Delete a cloud task by id.

Parameters:

  • id (String)

    The task id.



96
97
98
# File 'lib/cloudtasker/cloud_task.rb', line 96

def self.delete(id)
  backend.delete(id)
end

.find(id) ⇒ Cloudtasker::Cloudtask

Find a cloud task by id.

Parameters:

  • id (String)

    The id of the task.

Returns:

  • (Cloudtasker::Cloudtask)

    The task.



72
73
74
75
# File 'lib/cloudtasker/cloud_task.rb', line 72

def self.find(id)
  payload = backend.find(id)&.to_h
  payload ? new(**payload) : nil
end

.gct_backendCloudtasker::Backend::GoogleCloudTaskV1, Cloudtasker::Backend::GoogleCloudTaskV2

Return the GoogleCloudTaskV* backend to use based on the version of the currently installed google-cloud-tasks gem

] The google cloud task backend.

Returns:



42
43
44
45
46
47
48
49
50
# File 'lib/cloudtasker/cloud_task.rb', line 42

def self.gct_backend
  @gct_backend ||= if !defined?(Google::Cloud::Tasks::VERSION) || Google::Cloud::Tasks::VERSION < '2'
                     require 'cloudtasker/backend/google_cloud_task_v1'
                     Backend::GoogleCloudTaskV1
                   else
                     require 'cloudtasker/backend/google_cloud_task_v2'
                     Backend::GoogleCloudTaskV2
                   end
end

.setup_production_queue(**kwargs) ⇒ Google::Cloud::Tasks::V2::Queue, Google::Cloud::Tasks::V2beta3::Queue

Create the google cloud task queue based on provided parameters if it does not exist already.

Parameters:

  • :name (String)

    The queue name

  • :concurrency (Integer)

    The queue concurrency

  • :retries (Integer)

    The number of retries for the queue

Returns:

  • (Google::Cloud::Tasks::V2::Queue, Google::Cloud::Tasks::V2beta3::Queue)

    The queue



61
62
63
# File 'lib/cloudtasker/cloud_task.rb', line 61

def self.setup_production_queue(**kwargs)
  gct_backend.setup_queue(**kwargs)
end

Instance Method Details

#==(other) ⇒ Boolean

Equality operator.

Parameters:

  • other (Any)

    The object to compare.

Returns:

  • (Boolean)

    True if the object is equal.



126
127
128
# File 'lib/cloudtasker/cloud_task.rb', line 126

def ==(other)
  other.is_a?(self.class) && other.id == id
end