Rake tasks, database scanning, and the whenever gem

Hey guys!

I’ve noticed in some services I sign up for that they send me automated emails if I have failed to complete events critical to the application. I’d like to add that functionality to my production application.

So I added the whenever gem and made some rake tasks and have set up all the mailers I need. I’m just curious on the next step to take. My goal is that daily I can the database if a user has not completed an event that triggers my tasks. My code is below.

email_reminders.rake

namespace :email_reminders do
  desc "reminds the user to complete their profile"
  task :founder_welcome => :environment do
    if Rails.env == "production"
      if @user.profile.present?
        ConfirmMailer.founder_welcome(@user).deliver
      end
    end
  end
end
 
namespace :email_reminders do
  desc "reminds a user to click the confirm link in their confirm email"
  task :confirm_email => :environment do
    if Rails.env == "production"
     users = User.where(‚confirmation_token IS NOT NULL‘)
     if users
      @user = user
      users.each do |user|
        ConfirmMailer.user_confirmation(@user).deliver
      end 
     end
    end
  end
end
 
namespace :email_reminders do
  desc "reminds the user to complete their profile"
  task :make_profile => :environment do
    if Rails.env == "production"
      if @user.profile.nil?
        ConfirmMailer.create_profile(@user).deliver
      end
    end
  end
end
 
namespace :email_reminders do
  desc "reminds the user to list their property"
  task :list_property => :environment do
    if Rails.env == "production"
      if @user.properties.present? && @user.listings.empty?
        ConfirmMailer.list_property(@user).deliver
      end
    end
  end
end
 
namespace :email_reminders do
  desc "reminds a user to invite their roommates"
  task :invite_roommates => :environment do
    if Rails.env == "production"
      if @user.listings.present?
        ConfirmMailer.invite_roommates(@user).deliver
      end
    end
  end
end

confirm_mailer.rb

class ConfirmMailer < ActionMailer::Base
  default from: "mike@trydomi.com"
 
  def founder_welcome(user)
    @user = user
    @url  = 'https://www.trydomi.com'
    mail(to: @user.email, subject: 'Welcome to Domi!')
  end
 
  def user_confirmation(user)
    @user = user
    mail(to: @user.email, subject: 'Confirm your account to join Domi!')
  end
 
  def create_profile(user)
    @user = user
    @url  = 'https://www.trydomi.com/profiles/new'
    mail(to: @user.email, subject: 'Get started with Domi! Create your profile!')
  end
 
  def list_property(user)
    @user = user
    @url  = 'https://www.trydomi.com/listings/new'
    mail(to: @user.email, subject: 'Get your place sublet and list!')
  end
 
  def invite_roommates(user)
    @user = user
    @url  = @user.listings.last
    mail(to: @user.email, subject: 'Listings with roommates are more likely to sublet!')
  end
end

schedule.rb

# Use this file to easily define all of your cron jobs.
#
# It's helpful, but not entirely necessary to understand cron before proceeding.
# http://en.wikipedia.org/wiki/Cron
 
# Example:
#
# set :output, "/path/to/my/cron_log.log"
#
# every 2.hours do
#   command "/usr/bin/some_great_command"
#   runner "MyModel.some_method"
#   rake "some:great:rake:task"
# end
#
# every 4.days do
#   runner "AnotherModel.prune_old_records"
# end
 
# Learn more: http://github.com/javan/whenever

Gemfile

gem “whenever”

1 Like