Created
July 24, 2018 13:13
-
-
Save benoittgt/224dcfc439dabd94376b8d1ef5f60d56 to your computer and use it in GitHub Desktop.
refactor Remi worker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# from https://twitter.com/mercier_remi/status/1021669831190433792 | |
class CreateNotificationsWorker | |
include Sidekiq::Worker | |
def perform | |
users = User.all | |
users.each do |user| | |
tasks = user.tasks | |
tasks.each do |task| | |
if task.deadline && task.status == 'pending' && task.notified_user == false | |
@notification = Notification.create!(user_id: user.id, task_id: task.id) | |
task.notified_user = true | |
task.save! | |
end | |
end | |
end | |
end | |
end |
Task.pending
=> Great idea! I had completely forgotten about these methods associated with enums.
.where.not
=> Sweet!
find_each
=> Sonia talked to me about it yesterday. Really cool way to reduce the load on memory.
Thanks, @benoittgt for all these ameliorations.
I don't use any bug library (not that I'm aware of anyway). Which one would you suggest I should start with?
We use airbrake an are quite happy about it. But honeybadger seems good too.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tasks.each
load all task. Check.find_each
if you can have many tasks.'pending'
is an enum you can instead refer to it asTask.statuses[:pending]
save!
raise an exception?Instead you could write (assuming
task.deadline
need to be not nil, andstatus
an enum)For the long query you could put into a method.