Class: Cybertrain::Job::Base

Inherits:
Object
  • Object
show all
Defined in:
cybertrain/job/base.rb

Overview

The superclass of an application’s job classes (app/jobs/*.rb):

class WelcomeJob < Cybertrain::Job::Base retry_on Cybertrain::Mailer::TimeoutError, wait: 60, attempts: 5 discard_on ArgumentError limits_concurrency to: 1, key: “user_id”

def perform(args)
  user = User.find(Cybertrain::Cast.int(args["user_id"]))
  Cybertrain::Mailer.deliver(to: user.email, subject: "Welcome", body: "Hello!\n")
end   end

WelcomeJob.perform_later(“user_id” => user.id)

spin run gen reads the class as text and writes its other half into gen/jobs.rb: job_kind, perform_later, perform_at, perform_in, perform_now and run_job, with the policies as literal rescue arms, and the register call. The macros here do nothing at run time; they exist so that the class body loads. Write each on one line with literal values (an Integer or :polynomially_longer for wait:, an Integer for attempts: and to:, a String for key:), and name only exception classes the program defines before the job: core classes, the framework’s own (the mailer’s, the HTTP client’s, the socket layer’s), or a class under app/models, in app/jobs/application_job.rb, or above the macro in the job’s file.

Direct Known Subclasses

WelcomeJob

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.discard_on(*args) ⇒ nil

Declares that an exception of the class (or a subclass) ends the job at once, discarded, with no retry and no #retries_exhausted.

Examples:

discard_on ArgumentError

Parameters:

  • args (Array) —

    the exception class

Returns:

  • (nil)


54
55
56
# File 'cybertrain/job/base.rb', line 54

def self.discard_on(*args)
  nil
end

.limits_concurrency(**opts) ⇒ nil

Declares a concurrency limit, read by spin run gen: at most to: jobs of the class running at once across every process, counted per value of the args field key: when given (Cybertrain::Job.register).

Examples:

limits_concurrency to: 1, key: "user_id"

Parameters:

  • opts (Hash) —

    to: (Integer, default 1) and key: (String)

Returns:

  • (nil)


66
67
68
# File 'cybertrain/job/base.rb', line 66

def self.limits_concurrency(**opts)
  nil
end

.retry_on(*args) ⇒ nil

Declares a retry policy, read by spin run gen: when perform raises the class (or a subclass), the job is queued again after wait: seconds (default 3; :polynomially_longer waits attempts ** 4 + 2), until attempts: attempts (default 5, the first included) have failed; then the row is failed and #retries_exhausted runs. Several lines may be given: the first whose class matches wins.

Examples:

retry_on Cybertrain::HttpClient::TimeoutError, wait: :polynomially_longer, attempts: 10

Parameters:

  • args (Array) —

    the exception class and the wait: / attempts: options

Returns:

  • (nil)


43
44
45
# File 'cybertrain/job/base.rb', line 43

def self.retry_on(*args)
  nil
end

Instance Method Details

#perform(args) ⇒ Object

The job’s work. args is the Hash given to perform_later, read back from JSON: narrow its values with Cast.int, Cast.str, Cast.hash, Cast.array.

Parameters:

  • args (Hash{String => Object})

Returns:

  • (Object) —

    ignored

Raises:

  • (NotImplementedError) —

    unless the job class defines it



77
78
79
80
# File 'cybertrain/job/base.rb', line 77

def perform(args)
  message = "#{self.class.name}#perform is not defined"
  raise NotImplementedError, message
end

#retries_exhausted(error) ⇒ nil

Runs once when a retry_on policy has used its last attempt and the row became failed (never for an attempt whose row was taken over by another worker, nor for discard_on). The default does nothing.

Parameters:

  • error (StandardError) —

    the exception of the last attempt

Returns:

  • (nil)


88
89
90
# File 'cybertrain/job/base.rb', line 88

def retries_exhausted(error)
  nil
end