ActionMailer fails with "undefined function" when called from delayed job - actionmailer

So if I try sending an email with action mailer directly, I can use all application helpers like url_for, content_for etc, but when I try to do the exact same action [sending email] with delayed_job [send_later] I getting a delayed job fail, of undefined function content_for etc, so it is like no helpers are loaded in my ActionMailer. I am using rails 2.3.8, active_mailer 2.3.8 and delayed_job 2.0.3
Thanks!!

Your problem is that your sw is not a child of ActionView since your sw is not being initiated as a result of a web request. Rather, your sw is being initiated by the DelayedJob machinery.
Solution: create and use your own instance of ActionView --
action_view = ActionView::Base.new # used for calling helper tags, eg link_to
html = action_view.link_to(link_text, url)
# etc...

Related

Email Queuing with OctoberCMS

Can anyone help with some tips on how to queue email using an OctoberCMS ajax page?
function sendRecipientMsg($dataset, $sendCounter, $recipients){
$template = $dataset['template'];
Mail::queue($template, $dataset, function($message) use($dataset, $recipients){
$message->to('piggy#teamprema.co.nz','MissPiggy');
$message->subject('Have a good day');
$message->from('us#prema.co.nz', 'Mike and Stephie');
$message->sender('us#prema.co.nz', 'Mike and Stephie');
trace_log('$message');
$message->cc($address, $name = null);
$message->bcc('systems#safe.org.nz', 'SAFE Campaigns Feedlots ECards');
});
}
This code works when we use Mail::send but not with Mail::queue
Any help or tips very welcome
In your config/queue.php file, which driver do you have set as the default?
For example: 'default' => env('QUEUE_DRIVER', 'sync')
(if you are using DotEnv then check the .env file in your docroot).
If you are using sync, it should send right away as sync is really only for development and will still block.
If you are using another method, like database, then you do have to ensure that your queues are configured to process how you expect.
Try running php artisan queue:work, then trigger your ajax call and it should send.

Upgrading to Rails 5 and rspec 3.7 no longer sends emails via ActionMailer::Base.deliveries

I had a functional spec suite using rspec with a Rails 4.2.8. I upgraded to Rails 5.0.6 and rspec 3.7 accordingly.
Many of the specs work, but none of my specs that send emails actually add the email to the ActionMailer::Base.deliveries array.
I've verified that config/environments/test/rb has config.action_mailer.delivery_method = :test, which should add any sent emails to the array instead of actually sending them.
I suspect it has something to do with ActiveJob as well, because when I send an email via SomeMailer.some_method().deliver_later in my specs, it returns an ActionMailer::DeliveryJob object, but it does not add anything to the ActionMailer::Base array.
When I call SomeMailer.some_method().deliver_now, I find the emails actually are added to the ActionMailer::Base array. I'm wondering if for my specs I just need to configure them to all use deliver_now instead of deliver_later?
I've reviewed and followed the Ruby on Rails upgrade guide from 4.2 -> 5.0 and can't figure this out.
My specs with emails follow the format:
# spec/models/record_spec.rb
require "rails_helper"
describe Model do
describe "#method" do
it "sends email" do
record = Record.create(attributes = {})
RecordMailer.welcome_email(record).deliver_later
last_email = ActionMailer::Base.deliveries.last
expect(last_email.subject).eq ("Welcome")
end
end
end
Assuming you're including ActiveJob::TestHelper somewhere, you can wrap your deliver_later (or if in a feature spec, for example, a button click triggering the delivery) inside a perform_enqueued_jobs block:
perform_enqueued_jobs do
RecordMailer.welcome_email(record).deliver_later
end
Now your ActionMailer::Base.deliveries should contain the email sent via ActiveJob.

Perl Dancer after hook

Is there a way in Dancer to execute a code after every request?
I tried with an after hook but it seems that it doesn't execute after a file request...
There is a hook called 'after_file_render' which is executed a decent number of times after each request but I am not sure what is its purpose. Is it always called after every request?
The after_file_render hook is run after each successful request for a static file (e.g., a CSS file or an image), while the after hook runs after an action is performed by a route handler.
If you want to run the same code for both after and after_file_render, you can put it in a subroutine and assign it to the two hooks using a reference, e.g.:
sub foo {
...
}
hook after_file_render => \&foo;
hook after => \&foo;

async_sinatra requests crash silently with em-http. How do I fix this?

I have the following code:
require 'rubygems'
require 'eventmachine'
require 'em-http'
require 'sinatra/base'
require 'sinatra/async'
class Api < Sinatra::Base
register Sinatra::Async
aget '/1' do
EventMachine.run {
http = EventMachine::HttpRequest.new( "http://www.google.com").get(:timeout => 5)
http.callback { puts "h2" ;ret_val = http.response; EventMachine.stop}
http.errback {puts "was h2ere1" ;ret_val = nil; EventMachine.stop}
}
body "done processing 1"
end
aget '/2' do
body "done processing 2"
end
end
When I issue the following, it works well:
curl http://localhost:3000/2
But, when I issue the following request, it prints "h2" and the application silently quits:
curl http://localhost:3000/1
Any help will be appreciated. Thanks!
If your web server (Eg. thin) is based on EventMachine, then the EventMachine.stop line will actually stop the webserver as well as the EventMachine instance created by EventMachine.run.
I can't find a way to stop nested EventMachines like this. My advice - use Weary or another non-blocking HTTP request library.
Sinatra::Async provides it's own body helper that needs to be called from within the EventMachine loop. Also worth noting: if you're running Sinatra through Thin, you shouldn't call EM.run explicitly, as Sinatra is already operating within an EventMachine loop.

How to do error handling in web application written in Perl (CGI.pm)?

When writing / maintaining web application written in Perl using CGI.pm, how should I handle errors (exceptions)? Webapp in question can be deployed as plain CGI app, as FastCGI app (using CGI::Fast), and as mod_perl app using ModPerl::Registry handler.
I mean here handling errors like page not found, or configuration forbids given action, or some external command used in the app failed (like e.g. cannot connect to the database).
Edit: added 2010-12-14.
The example code flow could look like this:
sub run {
...
run_request();
...
}
sub run_request {
...
$actions{$cgi->param('a')}->();
...
}
sub action_foo {
foo_body()
}
sub foo_body {
check_something()
or handle_error(some description);
}
I mean here that error might have be to be thrown / handled in some nested call, and not only in the action handler / route handler.
Pass the appropriate status code to the header method as per Creating A Standard HTTP Header