Reputation: 1915
I'm trying to use ActiveJob
to queue jobs like email scheduling, but I get a
NotImplementedError (Use a queueing backend to enqueue jobs in the future.
error
After a update is committed to the database, in my model.rb
file
class Article < ActiveRecord::Base
include ActiveModel::Dirty
require './app/jobs/email_scheduler_job'
def send_approved_mail
if self.approved_was == false && self.approved
ArticleMailer.article_approved(self.owner).deliver_later
EmailSchedulerJob.set(wait: 2.weeks).perform_later(owner)
end
end
end
and in my EmailSchedulerJob
class EmailSchedulerJob < ActiveJob::Base
queue_as :default
def perform(*args)
# Do something later
end
end
Upvotes: 6
Views: 4803
Reputation: 1627
You need to use a queue adapter to enqueue jobs in the future. Sidekiq is great in most cases, but it is quite complicated to set up. I think using sidekiq for email scheduling seems like an overkill. I would recommend using sucker_punch for this.
To use sucker_punch as the adapter for ActiveJob, add this to your Gemfile:
gem 'sucker_punch'
And then configure the backend to use sucker_punch:
# config/initializers/sucker_punch.rb
Rails.application.configure do
config.active_job.queue_adapter = :sucker_punch
end
You don't need to create a job for sending email. ActionMailer is integrated with ActiveJob so deliver_later should be enough.
Hope it helps :)
Upvotes: 5