My task will do the following:
Check if a user is signed in, and notify it to an admin
If true => do nothing
If false => send an email to an admin
I want this task to be executed every hour (it will be scheduled with Heroku Scheduler add-on)
The problem here is that you cannot use user_signed_in?
or current_user
in any rake file you write. In addition, I can not pass any of these values as parameters because It will be called from the Heroku Scheduler, dashboard.
Some of my code here:
lib/tasks/scheduler.rake
task :send_notification => :environment do
if user_signed_in?
puts "No need to email"
else
puts "Sending email"
u=User.find(132) #get an admin user
ModelMailer.time_notification(u).deliver
end
end
The error:
rake aborted!
NoMethodError: undefined method `user_signed_in?' for main:Object
Thanks in advance!
Devise in its default configuration does not allow you to track which users are online and offline. The helpers current_user
and user_signed_in?
pertain to the current session and are only valid during a user's request. Since there is no request in a rake task, they aren't useful.
You could potentially add a column to your database to track whether users are signed in or signed out, but this functionality isn't present in devise.