Class: Jobs::Base

Inherits:
Object show all
Defined in:
lib/jobs/base.rb

Overview

Base class for all delayed jobs

Direct Known Subclasses

Analysis::Base, CreateDataset, DestroyDataset, ExpireDownloads

Instance Method Summary (collapse)

Constructor Details

- (undefined) initialize(args = { })

Initialize the job from a hash of attributes in a generic way

This constructor will simply set all of the passed args as attribute values, if the job has the given attribute.

Examples:

Setting attributes in a job class

class TestJob < Jobs::Base
  attr_accessor :name
end
job = TestJob.new(:name => 'Testing')
job.name
# => 'Testing'

Parameters:

  • args (Hash) (defaults to: { })

    the instance variables to set



37
38
39
40
41
42
43
# File 'lib/jobs/base.rb', line 37

def initialize(args = { })
  @state_vars = args.keys
  
  args.each_pair do |key, value|
    self.send("#{key}=", value) if self.respond_to?("#{key}=")
  end
end

Instance Method Details

- (Boolean) ==(other) Also known as: eql?

Compare objects for equality based on their attributes

Returns:

  • (Boolean)

    true if self is equal to other



69
70
71
# File 'lib/jobs/base.rb', line 69

def ==(other)
  attributes == other.attributes
end

- (Hash) attributes

Get a hash of attributes from the state variables

Examples:

Get the attributes of a job class

class TestJob < Jobs::Base
  attr_accessor :name, :test
end
job = TestJob.new(:name => 'Testing')
job.test = 'woo'
job.attributes
# => { :name => 'Testing' }
# Note: does NOT include :test, as this was not set on
# construction

Returns:

  • (Hash)

    hash of all attributes set on construction



59
60
61
62
63
# File 'lib/jobs/base.rb', line 59

def attributes
  ret = {}
  @state_vars.each { |k| ret[k] = instance_variable_get("@#{k.to_s}") }      
  ret
end

- (undefined) error(job, exception)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Report any exceptions to Airbrake, if it's enabled

This method is a callback that is invoked by Delayed::Job. No tests, as it's merely a wrapper on the Airbrake gem.

:nocov:

Parameters:

  • job (Delayed::Job)

    The job currently being run

  • exception (StandardError)

    The exception raised to cause the error

Returns:

  • (undefined)


84
85
86
87
88
# File 'lib/jobs/base.rb', line 84

def error(job, exception)
  unless Settings.airbrake_key.blank?
    Airbrake.notify(exception)
  end
end

- (Integer) max_attempts

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Don't restart jobs on error

Restarting isn't going to help resolve any errors that are presented, so don't try it.

:nocov:

Returns:

  • (Integer)

    returns 1



99
100
101
# File 'lib/jobs/base.rb', line 99

def max_attempts
  1
end