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



120
121
122
# File 'lib/sidekiq/active_record/task_worker.rb', line 120

def identifier_key
  get_sidekiq_task_options[:identifier_key]
end

.method_aliases_mappingObject



133
134
135
# File 'lib/sidekiq/active_record/task_worker.rb', line 133

def method_aliases_mapping
  @_method_aliases_mapping
end

.model_classObject



114
115
116
117
118
# File 'lib/sidekiq/active_record/task_worker.rb', line 114

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

.perform_async_on(model, *args) ⇒ Object

Helper method, to automatically call the task worker with the identifier. This will allow you to change the :identifier_key option, without needing to change it in other places.

@example:

class UserMailerTaskWorker < Sidekiq::ActiveRecord::TaskWorker

  sidekiq_task_model User
  sidekiq_task_options :identifier_key => :email

end

user = User.find_by(:email => [email protected])

UserMailerTaskWorker.perform_async(user.email, arg1, arg2)

# is the same as doing
UserMailerTaskWorker.perform_async_on(user, arg1, arg2)


26
27
28
29
30
# File 'lib/sidekiq/active_record/task_worker.rb', line 26

def self.perform_async_on(model, *args)
  fail ArgumentError.new "Specified model must be a #{model_class.to_s}" unless model.class <= model_class
  identifier = model.send(self.identifier_key)
  perform_async(identifier, *args)
end

.sidekiq_task_model(model_klass) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/sidekiq/active_record/task_worker.rb', line 106

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'


129
130
131
# File 'lib/sidekiq/active_record/task_worker.rb', line 129

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

Instance Method Details

#call_alias_method(method_name, *args) ⇒ Object

Try calling the alias method, and fallback to default name if not defined



95
96
97
98
99
100
101
102
# File 'lib/sidekiq/active_record/task_worker.rb', line 95

def call_alias_method(method_name, *args)
  alias_name = self.class.method_aliases_mapping[method_name]
  if respond_to?(alias_name.to_sym)
    send(alias_name, *args)
  else
    send(method_name, *args)
  end
end

#did_not_perform_on_modelObject

Hook to handel a model that was not performed



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

def did_not_perform_on_model
  task_model
end

#fetch_model(identifier, *args) ⇒ Object



90
91
92
# File 'lib/sidekiq/active_record/task_worker.rb', line 90

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



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

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_async(user.id, :new_email)


59
60
61
62
63
64
65
66
67
68
# File 'lib/sidekiq/active_record/task_worker.rb', line 59

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

  if call_alias_method(:should_perform_on_model?)
    call_alias_method(:perform_on_model, *args)
  else
    call_alias_method(:did_not_perform_on_model)
  end
end

#perform_on_model(*args) ⇒ Object



70
71
72
# File 'lib/sidekiq/active_record/task_worker.rb', line 70

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)


76
77
78
# File 'lib/sidekiq/active_record/task_worker.rb', line 76

def should_perform_on_model?
  true
end