Search code examples
ruby-on-railsresque

Rescue.enqueue on after_commit callback returns uninitialized constant error


I'm working on configuring Resque and I want to enqueue a job in an after_commit callback on my SocialMediaPost model. When I try to save an instance of my model, I get an uninitialized constant SocialMediaPost::Postman error. This is my first time working with background jobs so I'm not sure what I'm missing. I've been following the RailsCast on this as well as the readme on the Resque repo.

Here is my model SocialMediaPost:

class SocialMediaPost < ActiveRecord::Base
    belongs_to :company
    belongs_to :employer
    validates :post, length: {in: 0..140}, if: Proc.new { |t| t.twitter }
    validates :post, length: {in: 0..256}, if: Proc.new { |t| t.linkedin }
    after_commit :enqueue

    private

    def enqueue
        if self.scheduled_time
            options = {
                id: self.id,
                services: JSON.parse(self.services),
                posts: JSON.parse(self.posts),
                attachment_url: self.attachment_url,
                media_url: self.media_url,
                time: self.scheduled_time
            }
            Resque.enqueu(Postman, options)
        else
            post_tweet if self.twitter && self.attachment_url.blank?
            post_tweet_with_media if self.twitter && self.attachment_url.present?
            post_linkedin_status if self.linkedin
        end
    end
end

Here is my worker Postman (app/workers/postman.rb):

class Postman
    @queue = :social_media_posts

    def self.perform(options)
        post = SocialMediaPost.find(options.id)
        post.post_tweet if options[:services][:twitter] && options[:attachment_url].blank?
        post.post_tweet_with_media if options[:services][:twitter] && options[:attachment_url].present?
        post.post_linkedin_status if options[:services][:linkedin]
    end
end

Here is my resque.rake file:

require 'resque/tasks'

namespace :resque do
    puts "Loading Rails environment for Resque"
    task :setup => :environment do
        ActiveRecord::Base.descendants.each { |klass|  klass.columns }
    end
end

Solution

  • You likely need

    require 'postman'
    

    at the top of your SocialMediaPost file.