-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Scheduled job to send push notifications for Post resource #377
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We reduced complexity a great deal now 👏
end | ||
|
||
def send | ||
return unless push_notifications.any? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You told me Exponent::Push::Client
does not check this and so it blows up if notifications
is empty, right? It would make things simpler if they did.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, but they don't :(
push_notifications.map do |push_notification| | ||
{ | ||
'to' => push_notification.to, | ||
'title' => push_notification.title |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does Exponent::Push::Client
requires the keys to be strings? It'd be great if we could use symbols.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be bossible 👍
@@ -0,0 +1,4 @@ | |||
send_push_notifications_job: | |||
cron: '*/5 * * * *' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you remind me the syntax?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's every 5 minutes, but we still have to take a decision about this.
42058f3
to
fb9d8b9
Compare
fb9d8b9
to
cc6cea4
Compare
add_column :push_notifications, :title, :string, null: false, default: 'Title for existing records' | ||
change_column_default(:push_notifications, :title, nil) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The migration will add a DEFAULT
first and then remove it to allow the creation of a NOT NULL
column on existing records (more info).
91626ca
to
dce49e0
Compare
1b6b3f4
to
ed4b3a5
Compare
Gemfile.lock
Outdated
elasticsearch-api (= 1.0.7) | ||
elasticsearch-transport (= 1.0.7) | ||
elasticsearch-api (1.0.7) | ||
elasticsearch (5.0.5) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we need to take extra care about search when we test this PR 🙈
|
||
client = Exponent::Push::Client.new | ||
|
||
push_notifications.update_all(processed_at: Time.now.utc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather update the processed_at
after publishing the notifications just in case that fails.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We discussed about this and it's simpler this way, push notifications are not really important. You can loose one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 @enricostano that Push Notifications thing is almost complete no? Maybe we should integrate #381 first (now that we have #356 also merged) and we'll ready to
ed4b3a5
to
5c114f4
Compare
This reverts commit 13ff3d1.
db/schema.rb
Outdated
@@ -164,6 +164,7 @@ | |||
t.datetime "processed_at" | |||
t.datetime "created_at", null: false | |||
t.datetime "updated_at", null: false | |||
t.string "title", null: false |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do you need a title? isn't all the info in the Event?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to store it in the notification. The Event
doesn't understand anything about notifications.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll need to do the same for body
queue_as :cron | ||
|
||
def perform | ||
push_notifications = PushNotification.where(processed_at: nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would filter out old PushNotifications that may not be relevant anymore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll use the processed_at
to do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's a limit of 100 messages per request: https://docs.expo.io/versions/latest/guides/push-notifications#http2-api
app/models/push_notification.rb
Outdated
validates :event, :device_token, presence: true | ||
validates :event, :device_token, :title, presence: true | ||
|
||
def to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ken?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wat?
PushNotification.create!( | ||
event: event, | ||
device_token: device_token, | ||
title: title |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need to dupe this info? it is already stored in the Event
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's not. We'll need to do the same for body also.
raise PostError, "HTTP response: #{response.code}, #{response.body}" | ||
end | ||
|
||
push_notifications.update_all(processed_at: Time.now.utc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would you update updated_at
as well?
class AddTitleToPushNotification < ActiveRecord::Migration | ||
def up | ||
add_column :push_notifications, :title, :string, null: false, default: 'Title for existing records' | ||
change_column_default(:push_notifications, :title, nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this? We already set a default in the previous line, also we have a not null constrain.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hehehe, this code is yours and was about to ask the same
:D 8069d51#r193443513
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
THAT'S IMPOSSIBLE
class AddBodyToPushNotification < ActiveRecord::Migration | ||
def up | ||
add_column :push_notifications, :body, :string, null: false, default: 'Body for existing records' | ||
change_column_default(:push_notifications, :body, nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this? We already set a default in the previous line, also we have a not null constrain.
class AddDataToPushNotification < ActiveRecord::Migration | ||
def up | ||
add_column :push_notifications, :data, :json, null: false, default: '{}' | ||
change_column_default(:push_notifications, :data, nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this? We already set a default in the previous line, also we have a not null constrain.
533545c
to
bcb8684
Compare
The mobile app part: coopdevs/timeoverflow-mobile-app#3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks really good! 👏
WAT
The workflow is the following:
Post
is created or updated we create anEvent
Event
we create as manyPushNotification
as neededSendPushNotificationsJob
collect all thePushNotification
that have not been processed yet and it sends themTest
create
andupdate
of offers a inquiriesTODO
PushNotification
as processedbody
to notifications https://github.com/coopdevs/timeoverflow/blob/feature/scheduled-jobs/app/services/push_notifications/broadcast.rb#L35title