#puts "Restoring stuck Rpush notifications..." batch_size = YAML.load_file("#{Rails.root}/config/rpush.yml")[Rails.env]['batch_size'] * 5 rpush_dump_file = File.open("#{Rails.root}/log/rpush.dump", 'a') config = YAML.load_file("#{::Rails.root}/config/redis.yml")[::Rails.env] client = Redis.new config notification_ids = client.smembers('rpush:notifications:all').map(&:to_i).sort.reverse # This is the standard set where Rpush stores notifications, that will be processed # It acts like a queue pending_notification_ids = client.zrange('rpush:notifications:pending', 0, -1).map(&:to_i) notifications_restored = [] notification_ids.each_with_index do |id, index| # Skip if notification is already marked for being processed soon next if pending_notification_ids.include?(id) notification = Rpush::Notification.find(id) rescue nil next unless notification if notification.delivered || notification.failed # Check for at least one batch size of notifications for being delivered break if index > batch_size next end if notifications_restored.empty? rpush_dump_file.write("Writing dump at #{Time.now.utc}\n") end rpush_dump_file.write("#{notification.attributes}\n") client.zadd('rpush:notifications:pending', 0, id) notifications_restored << id end rpush_dump_file.write("Current rpush status:\n") rpush_state = `bundle exec rpush status -e #{Rails.env}` rpush_dump_file.write("#{rpush_state}\n") rpush_dump_file.write("Rpush memory usage:\n") rpush_dump_file.write("#{`ps aux | grep 'rpush.*dispatchers' | grep -v grep`}\n") rpush_dump_file.write("Overall memory usage:\n") rpush_dump_file.write("#{`free`}\n\n") rpush_dump_file.close system("RAILS_ENV=#{Rails.env} bundle exec god restart rpush") #puts "#{notifications_restored.count} notifications restored"