Question: How can I make an ActiveJob wait for other instances of that job?

Question

How can I make an ActiveJob wait for other instances of that job?

Answers 0
Added at 2016-11-25 22:11
Tags
Question

I want a job to refresh a database view on certain actions. The job can get called multiple times in short order. I want it to execute the task only the last time it gets called for a certain period to don't clutter the database. This is what I came up with so far:

class RefreshMaterializedViewJob < ApplicationJob
  mattr_accessor :identity_token

  def perform(table_name)
    token = SecureRandom.hex
    self.identity_token = token
    sleep 2
    Scenic.database.refresh_materialized_view(table_name.to_sym, concurrently: false) if identity_token == token
  end
end

Is there any possibility to get rid of the sleep? Or are there any better solutions?

edit: I'm using Sucker Punch as ActiveJob Adapter

Answers
Source Show
◀ Wstecz