every 1.minute do
runner "DailyNotificationChecker.send_notifications"
end
this is whenever gem scheduler
class DailyNotificationChecker
def self.send_notifications
puts "Triggered send_notifications"
expiry_time = Time.now + 57
while (Time.now < expiry_time)
if RUN_SCHEDULER == "true" || RUN_SCHEDULER == true
process_notes
end
sleep 4 #seconds
end
end
def self.process_notes
notes = nil
time = Benchmark.measure do
Note.uncached do
notes = Note.within_2_mins.unprocessed
if notes.present?
note_ids = notes.pluck(:id)
note_hash = {}
note_hash[:note_ids] = note_ids.to_json
url = URI.parse(PROCESS_NOTE_API_URL)
resp, data = Net::HTTP.post_form(url, note_hash)
notes.each do |note|
puts "note_id = #{note.id}"
puts "processed #{note.processed}"
RealtimeNotificationChecker.perform_async(note.user_id,"note_created","TempUserNote",note.id)
end
end
end
end
puts "time #{time}"
end
end
My purpose is I am trying to run realtime notifications. If a user creates a note in app then within 4 seconds I will send him a push notification.
so I run a cron job every 1 minute and hit the method send_notifications
in my DailyNotificationChecker
class. Now this is supposed to run a method process_notes every 4 seconds. every new note that is created is set processed = 0 flag. and I retrieve every unprocessed flag not more than 2 mins old and perform my operation on it.
the line
note_ids = notes.pluck(:id)
note_hash = {}
note_hash[:note_ids] = note_ids.to_json
url = URI.parse(PROCESS_NOTE_API_URL)
resp, data = Net::HTTP.post_form(url, note_hash)
gets all the note_ids that are unprocessed and sends them to master server to mark them processed = 1 so that in next 4 second hit these notes dont come again to processing list.
Then I loop in every note and send them in background to send push notificaion
notes.each do |note|
RealtimeNotificationChecker.perform_async(note.user_id,"note_created","TempUserNote",note.id)
end
but i guess something is wrong here. As when I have more than 2 notes in to be processed it doesn't sends notification to all users.
Can someone suggest whats wrong and required
Note: I may have more than 10-15 notes to be processed within 4 sec. I am using sucker punch gem for background jobs.
Aucun commentaire:
Enregistrer un commentaire