Delayed Job passenger production undefined method error - passenger

I am using delayed job version 2.1.4 with action mailer 3.0.8 to send my emails in background.
UserMailer.delay.newsletter(email)
It works with me fine in development and production Rails console.
But when it is called from my live production passenger server, it creates DJ but when this DJ runs, it throws
{undefined method `newsletter' for #<Class:0xc08afa0>
I think the problem
Any help?

The problem is with rails/passenger in production mode with class methods
So, I made a work around solution... I will try to call instance method and avoid to call class method.
Lets see an example; assuming I need to call the following method in background..
UserMailer.deliver_newsletter(email)
Then, I created a new class DelayClassMethod
class DelayClassMethod
def initialize(receiver_name, method_name, options={})
#receiver_name = receiver_name
#method_name = method_name
#parameters = options[:params] || []
end
def perform
eval(#receiver_name).send(#method_name, *#parameters)
end
end
and can run the method in background now by
DelayClassMethod.new("UserMailer", "deliver_newsletter", :params=>[email]).delay
So, I am running an instance method now in background which will run the class method.
Another example..
Assume I want to run
Product.list_all_by_user_and_category(user, category).delay
Then it can be run by
DelayClassMethod.new("Product", "list_all_by_user_and_category", :params => [user, category]).delay

My hunch is that you have multiple versions of the delayed_job in production. Are you using bundler to start your delayed job process?
I would recommend when doing bundle install in production, I would use the --binstubs flag to generate a wrapper around the delayed_job and use that executable to start the jobs.

Related

Getting undefined method when trying to use a ruby wrapper for the Spotify Web API

I was messing around with RSpotify which is a ruby wrapper for the Spotify API.
I added a form and sent the search result param to be looked up in the API but i'm getting the following error:
NoMethodError: undefined method `empty?' for nil:NilClass
from /Users/user/.rvm/gems/ruby-2.3.1/gems/rspotify-1.21.0/lib/rspotify/connection.rb:71:in `send_request'
I double checked and reinstalled the gem, added it to the gemfile and everything. I can't seem to figure out why it's not working.
Here's the code (It was working a few weeks ago when I wrote it):
post '/music' do
search = RSpotify::Artist.search(params[:search])
#artist = search.first
#image = #artist.images.first["url"]
#top_tracks = #artist.top_tracks(:US)
albums = #artist.albums
#titles = []
albums.each { |album| #titles << album.name }
#titles.uniq!
#related_artists = #artist.related_artists
erb :'/music/show'
end
You seem to be using an outdated version (1.21.0, judging by your output). The current version is 1.22.1 (see https://rubygems.org/gems/rspotify) => maybe try updating.
Also, like Jeff said: Apparently there's an open GitHub issue that says that this issue occurs when you're not properly authenticated. So maybe check that your credentials are 1) set and 2) still valid. A simple way to check your credentials is to simply use them with the API directly (see http://www.compciv.org/recipes/data/touring-the-spotify-api/) => if they don't work there, it's probably not the code but your credentials that aren't working properly.

How to use PDFNet instance across the ruby app

I have to initialize the PDFNet library once in my app. I did it in my app.rb which is run first
# app.rb
require 'PDFNetC/Lib/PDFNetRuby'
include PDFNetRuby
PDFNet.Initialize(ENV['PDFTRON_LICENSE_KEY'])
Later in my controller I need to convert pdf to xod for which I will require the initialized PDFNet instance
# SomeController.rb
out_file_path = "#{ENV['TEMP_DIR']}/#{Time.now.to_i}.xod"
::PDFNetRuby::Convert.ToXod(in_file_path, out_file_path)
File.open(out_file_path, 'rb')
Will the above method get the license registerd instance of PDFNet?
PDFNet.Initialize is a process wide call, so if both code snippets are in the same process, both will be in licensed mode.

How to start/stop sinatra app in cucumber hook

PROBLEM SOLVED:
I've figured out how to solve my problem:
in the before hook I fork the current process to start my sinatra web app:
Before do
if $pid.nil?
$pid = fork do
App.run!
end
end
end
Notice I have to have the forked PID attributed to a global variable so it doesnt get flushed after each scenario in cucumber. Besides that, I make sure to start my sintra app once in the entire cucumber execution.
Then at the end of my cucumber execution, I kill the child process and wait for it to be cleared prior to terminating the cucumber session:
at_exit do
unless $pid.nil?
Process.kill "TERM", $pid
Process.wait $pid
end
end
ORIGINAL QUESTION:
I am looking to stub a web app which contain content that will then be used by my application to fetch that content from that stub and do its trick...
What I am looking to achieve is start Sinatra app within cucumber hook... then stop it at end of execution... is that possible?
Below is where I've got so far...
#myapp.rb
require 'sinatra'
class App < Sinatra::Base
set :server, 'webrick'
get '/' do
'Hello!!!'
end
end
Then in hook:
Before do
App.run!
end
The first issue there is that the sinatra app won't run in the background... after solving that problem then I would need to understand if it's easy enough to stop the sinatra session as well...

Why is mocha not stubbing this method in my Rails 3 integration test?

I have the following:
setup do
Capybara.current_driver = Capybara.javascript_driver
#project.user = #user
#project.save
Project.any_instance.stubs(:price_all)
end
And yet I have a test failing because the Project.price_all method is being run:
/Users/me/code/rails/myapp/app/models/project.rb:178:in `price_all'
This was working properly until I upgraded to Capybara 2 and the latest version of capybara-webkit.
Why is that method still being run? And how can I fix?
When you say "the Project.price_all method is being run", is that a typo, or is price_all really a class method? If it is indeed a class method, you'll want to use Project.stubs(:price_all) instead of including any_instance so the stub is directly on the Project class rather than an instance of Project. If that's not your issue, I'm not sure what else to suggest based on the code sample you've provided.

Using sessions in Espresso Framework

good progress so far.
got all controllers and actions up and running in matter of minutes.
However not sure how make sessions work.
trying:
class Cart
session :memory
# my actions
end
but it fails with an error that say to use sessions at app level.
how that? where is that app level?
I guess you are starting the app like this:
Cart.run
it is a good one unless you need to add some setup to your app.
creating app is as easy as:
app = EApp.new :automount
app.run
and if you have some setup for the app, use a block at initialization:
app = EApp.new :automount do
session :memory
end
app.run
Please note the first argument - :automount - it instructs the app to search for controllers and mount them automatically.
If you want to mount your controllers manually, omit first argument:
app = EApp.new do
session :memory
end
app.mount Cart
app.run

Resources