Class: WelcomeJob

Inherits:
Cybertrain::Job::Base show all
Defined in:
docs/api/stubs/generated_app.rb

Overview

What spin run gen writes into gen/jobs.rb for a job class in app/jobs, here the spec’s example:

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

The generated file reopens the class with the class methods below and registers it, Cybertrain::Job.register("WelcomeJob", concurrency: 1, concurrency_key: "user_id", max_attempts: 5) { |run| WelcomeJob.run_job(run) } (Cybertrain::Job.register); gen/app.rb loads it after the job files. Every job class gets the same methods under its own name; a namespaced one (Admin::ReportJob in app/jobs/admin/report_job.rb) is reopened in its modules and keeps its full name as its kind. See Cybertrain::Job::Base for the macros.

Class Method Summary collapse

Methods inherited from Cybertrain::Job::Base

discard_on, limits_concurrency, #perform, #retries_exhausted, retry_on

Class Method Details

.job_kind ⇒ String

The kind its rows carry in cybertrain_jobs.kind: the class name.

Returns:

  • (String) —

    "WelcomeJob"



902
# File 'docs/api/stubs/generated_app.rb', line 902

def self.job_kind = "WelcomeJob"

.perform_at(time, args = {}) ⇒ Integer

perform_later, run no earlier than time.

Parameters:

  • time (Time)
  • args (Hash{String => Object}) (defaults to: {})

Returns:

Raises:



921
# File 'docs/api/stubs/generated_app.rb', line 921

def self.perform_at(time, args = {}); end

.perform_in(seconds, args = {}) ⇒ Integer

perform_later, run no earlier than seconds from now.

Parameters:

  • seconds (Integer, Float)
  • args (Hash{String => Object}) (defaults to: {})

Returns:

Raises:



929
# File 'docs/api/stubs/generated_app.rb', line 929

def self.perform_in(seconds, args = {}); end

.perform_later(args = {}) ⇒ Integer

Queues the job to run as soon as a worker is free: the row’s payload is JSON.generate(args), which perform receives parsed back.

Examples:

WelcomeJob.perform_later("user_id" => user.id)

Parameters:

  • args (Hash{String => Object}) (defaults to: {}) —

    the job’s arguments (a JSON object)

Returns:

Raises:

  • (ArgumentError) —

    when args is not a Hash, or the class’s limits_concurrency key: field is missing from it (Cybertrain::Job.enqueue)



913
# File 'docs/api/stubs/generated_app.rb', line 913

def self.perform_later(args = {}); end

.perform_now(args = {}) ⇒ nil

Runs perform at once on the calling thread: no row, no retry policy, no rescue (an exception propagates), the shape a test wants. The args are copied with Cybertrain::Cast.hash (keys as Strings), which is shallow: nested values stay the objects given, where a worker sees them parsed back from JSON.

Parameters:

  • args (Hash{String => Object}) (defaults to: {})

Returns:

  • (nil)

Raises:

  • (ArgumentError) —

    when args is not a Hash



940
# File 'docs/api/stubs/generated_app.rb', line 940

def self.perform_now(args = {}); end

.run_job(run) ⇒ nil

What the worker runs for each attempt (the block given to Cybertrain::Job.register): perform(run.run_args) inside one rescue arm per retry_on / discard_on line, in declaration order, so the first matching line wins. A retry_on arm calls Cybertrain::Job.retry_later and runs Cybertrain::Job::Base#retries_exhausted only when that answered Cybertrain::Job::OUTCOME_FAILED; a discard_on arm calls Cybertrain::Job.discard. An exception no line matches follows the default retry policy.

Parameters:

Returns:

  • (nil)


953
# File 'docs/api/stubs/generated_app.rb', line 953

def self.run_job(run); end