Search code examples
ruby-on-railsgoogle-cloud-messagingresque

Sending notifications with interval 1 minute using resque


I'm trying to send notifications to user using GCM and resque. Currently it is sending the message to all users and it spiked the server because there are too many users. So what i'm trying to approach is sending the message to 1000 users for each minute.

Is there a way to set that up for resque?

I have one thing in mind where loop through all users, then batch the users for 1000. While still inside the loop make a Timeout for 60 seconds. But i'm thinking this approach is bad.


Solution

  • You can try:

    1. find users in batches with index, therefore you can use index to calculate delayed time.
    2. Use wait option to delay the job instead of timeout.

    User.find_in_batches.with_index do |group, index|
      puts "Processing group ##{index}"
      GCMJob.set(wait: index.minutes).perform_later(group.ids) # each batch will have 60 seconds in between
    end
    

    class GCMJob < ActiveJob::Base
      def perform(user_ids)
        users = User.where(id: user_ids)
        users.map(&:send_gcm_message)
      end
    end