Class: Sidekiq::ActiveRecord::TaskWorker

Inherits:
Object
  • Object
show all
Includes:
Worker
Defined in:
lib/sidekiq/active_record/task_worker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#task_modelObject (readonly)

Returns the value of attribute task_model.



6
7
8
# File 'lib/sidekiq/active_record/task_worker.rb', line 6

def task_model
  @task_model
end

Class Method Details

.identifier_keyObject



87
88
89
# File 'lib/sidekiq/active_record/task_worker.rb', line 87

def identifier_key
  get_sidekiq_task_options[:identifier_key]
end

.model_classObject



81
82
83
84
85
# File 'lib/sidekiq/active_record/task_worker.rb', line 81

def model_class
  klass = get_sidekiq_task_options[:model_class]
  fail NotImplementedError.new('`sidekiq_task_model` was not specified') unless klass.present?
  klass
end

.sidekiq_task_model(model_klass) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/sidekiq/active_record/task_worker.rb', line 73

def sidekiq_task_model(model_klass)
  return if model_klass.blank?

  setup_task_model_alias(model_klass)

  get_sidekiq_task_options[:model_class] = active_record_class(model_klass)
end

.sidekiq_task_options(opts = {}) ⇒ Object

Allows customization for this type of TaskWorker. Legal options:

:identifier_key - the model identifier column. Default 'id'


96
97
98
# File 'lib/sidekiq/active_record/task_worker.rb', line 96

def sidekiq_task_options(opts = {})
  @sidekiq_task_options_hash = get_sidekiq_task_options.merge((opts).symbolize_keys!)
end

Instance Method Details

#did_not_perform_on_modelObject

Hook to handel a model that was not performed



57
58
59
# File 'lib/sidekiq/active_record/task_worker.rb', line 57

def did_not_perform_on_model
  task_model
end

#fetch_model(identifier, *args) ⇒ Object



66
67
68
# File 'lib/sidekiq/active_record/task_worker.rb', line 66

def fetch_model(identifier, *args)
  self.class.model_class.find_by(self.class.identifier_key => identifier)
end

#not_found_model(identifier, *args) ⇒ Object

Hook to handel not found model



62
63
64
# File 'lib/sidekiq/active_record/task_worker.rb', line 62

def not_found_model(identifier, *args)
  identifier
end

#perform(identifier, *args) ⇒ Object

@example:

class UserMailerTaskWorker < Sidekiq::ActiveRecord::TaskWorker

  sidekiq_task_model :user_model # or UserModel
  sidekiq_task_options :identifier_key => :token

  def perform_on_model
    UserMailer.deliver_registration_confirmation(user, email_type)
  end

  def not_found_model(token)
    Log.error "User not found for token:#{token}"
  end

  def should_perform_on_model?
    user.active?
  end

  def did_not_perform_on_model
    Log.error "User #{user.token} is inactive"
  end

end

UserMailerTaskWorker.perform(user.id, :new_email)


35
36
37
38
39
40
41
42
43
44
# File 'lib/sidekiq/active_record/task_worker.rb', line 35

def perform(identifier, *args)
  @task_model = fetch_model(identifier, *args)
  return not_found_model(identifier, *args) unless @task_model.present?

  if should_perform_on_model?
    perform_on_model(*args)
  else
    did_not_perform_on_model
  end
end

#perform_on_model(*args) ⇒ Object



46
47
48
# File 'lib/sidekiq/active_record/task_worker.rb', line 46

def perform_on_model(*args)
  task_model
end

#should_perform_on_model?Boolean

Hook that can block perform_on_model from being triggered, e.g in cases when the model is no longer valid

Returns:

  • (Boolean)


52
53
54
# File 'lib/sidekiq/active_record/task_worker.rb', line 52

def should_perform_on_model?
  true
end