I have been working on a Ruby on Rails project (a basic survey application using the 'surveyor' gem) that lets a user download a pdf of the completed survey results. To accomplish, I have written a basic rake task (as below) that calls the wkhtmltopdf command line utility to convert from HTML/CSS to a pdf every 15 minutes via whenever/cron.
Here is the rake task (lib/tasks/pdf_survey_results.rake):
desc "PDF Completed Survey Results for Admin Download"
task :pdf_survey_results => :environment do
# iterate through all the response sets
ResponseSet.all.each do |response_set|
if !response_set.completed_at.blank?
puts "response set #{response_set.id} is complete, checking for pdf..."
if response_set.pdf_url.blank?
puts "no pdf found, now generating..."
# make the pdf
result = system "wkhtmltopdf #{BASE_URL}/surveys/student-questionnaire/#{response_set.access_code} /opt/deployed_rails_apps/survey_app/public/survey-pdfs/survey-#{response_set.access_code}.pdf"
if !result.nil?
response_set.pdf_url = "#{BASE_URL}/survey-pdfs/survey-#{response_set.access_code}.pdf"
response_set.save
end
end
end
end
end
This works totally fine in my development environment (OS X) but when I have deployed to Ubuntu 14.04,
I CAN manually create pdf's via the command line (i.e. wkhtmltopdf http://surveyurl.com sample.pdf)
I CANNOT create pdf's via running the rake task manually (i.e. rake pdf_survey_results will give me the output through "no pdf found, now generating..." but none of the output from the wkhtmltopdf tool even though I DO see this output when running on my local OS X dev environment)
I CANNOT create pdf's via the cron job set up by whenever, which is not surprising as the manual rake task execution is not working.
What I am most stuck with is that I cannot figure out why the rake task is not giving me text output from wkhtmltopdf when it does that in Mac OS X even though the command line utility by itself works fine. So far that aspect has made this a bit difficult for me to debug. By the way, I am using unicorn as an application server in this project.
Any advice is very much appreciated!!! Will answer any relevant questions if anything is unclear.
Related
Capistrano v2 had two helpful tasks: cap deploy:update_code would do a full deployment to a new releases/ directory, but not change the current symlink or start/restart the server (so the server keeps running the current version without interruption). And cap deploy:update did the same thing plus changing the current symlink, but didn't start/restart the server. These were useful to shake out issues e.g. with asset compilation, before doing a real deploy.
Those two "update" tasks are gone in Capistrano v3. Is there an equivalent way to do a full deploy without changing the current symlink or restarting the server?
A custom task list this should do it:
task :deploy_without_symlink do
set(:deploying, true)
%w{ starting started
updating updated }.each do |task|
invoke "deploy:#{task}"
end
end
You can look at the code here: https://github.com/capistrano/capistrano/blob/master/lib/capistrano/tasks/framework.rake#L58 to see what deploy triggers. And the Publishing task per https://github.com/capistrano/capistrano/blob/master/lib/capistrano/tasks/deploy.rake#L38 is what changes the symlinks. So by omitting everything afterwards, you get what you are looking for.
I've been long time investigating how to generate complex .pdf files (many images and 30 pages aprox). from a html web, of my rails application.
At the end I realized than the best option is wkhtmltopdf from command line.
using pdfKit gem don't work in production, but it works in development
using wkhtmltopdf from command line, works in development but neither in production.
I have read a lot of issues with wkhtmltopdf, so I abandon use wkhtmltopdf getting info from production web server. --> Instead I have created a new environment "genera_pdf" to run in production database, with development configuration (assets, cache..etc)
And now I need to run some proces than execute this:
1) RAILS_ENV=genera_pdf rails s
2) wkhtmltopdf localhost:3000 result.pdf
3) stops (CTRL+C) rails s
If this is achievable, Whats the best way to do this in Linux?
I'm absolutly missed...with rake tasks? or rails runner? or Cron-task?
Thanks a lot
I'm running a custom rake task...
namespace :import do
desc "Import terms of service as HTML from stdin"
task :terms => :environment do
html = STDIN.read
settings = ApplicationWideSetting.first
settings.terms_and_conditions = html
if settings.save
puts "Updated terms of service"
else
puts "There was an error updating terms of service"
end
end
end
The model ApplicationWideSetting is reported as undefined when running the task in the production environment. However, when running the task on other environments (ie. development, staging, test.) the task runs fine.
Running the process in rails console, in all environments, completes ok.
Does anyone know what's going on, things I could check?
note: I ran the task with
puts Rails.env
To check the shell environment var RAILS_ENV was getting set/read correctly. I've also tried both with and without the square brackets around the :environment dependency declaration.
additional info: Rails v3.2.14
further info: I've setup a completely fresh rails app, and the script works fine in any environment. Since the install in question is a real production environment, I'll have to setup another deploy and check it thoroughly. More info as I find it.
In a nutshell, Rails doesn't eager load models (or anything else) when running rake tasks on Production.
The simplest way to work with a model is to require it when you begin the rake task, and it should work as expected, in this case:
# explicitly require model
require 'application_wide_setting'
It's possible to eager load the entire rails app with:
Rails.application.eager_load!
However, you may have issues with some initializers (ie. devise)
The app is running on Engine Yard. I need to log some data from inside the scripts ran by Whenever. Here's what it looks like:
schedule.rb:
every 1.minute do
rake 'my_tasks:task1 my_tasks:task2', :output => 'log/my_tasks.log'
end
task1 runs the script that has the following lines in it (just for testing purposes):
...
log('test')
...
def self.log(string)
logfile = '/var/log/engineyard/apps/my_app/test_log.log'
File.open(logfile, 'a') { |log| log.puts(string << "\n\n") }
end
The problem is that none of the logs (my_tasks.log, test_log.log) get created, no matter what I tried. When I run bundle exec rake my_tasks:task1 from console it successfully creates the test_log.log file and logs the message. What could be wrong? Is it related to how Engine Yard works?
This is probably related to how Engine Yard works. I am not familiar with Engine Yard itself, but it is of a similar service as Heroku, and I know that in oder to send something to logs in Heroku, you have to use the stdout or stderr.
See here for more details about Heroku, and see if the same works for Engine Yard (keep in mind that this does not allow you to have separate log files, it simply puts the logged line in the same log file as other logged lines).
I wonder how to setup testing in my rails apps. When I run rake test, first thing odd, it launch a bunch of CREATE TABLE against my dev. database (hum.. do not like this..). So I launch rake test RAILS_ENV=test and I even try bundle exec rake test RAILS_ENV=test. Now, the CREATE TABLE is against my test database but all fails with this error :
** Execute test:units
test/unit/category_test.rb:5:in `test': unknown command 't' (ArgumentError)
from test/unit/category_test.rb:5:in `<class:CategoryTest>'
I have used basic generator in Rails 3 and do not change anything. So I have this in caterogy_test.rb :
require 'test_helper'
class CategoryTest < ActiveSupport::TestCase
# Replace this with your real tests.
test "the truth" do
assert true
end
end
I use Rails 3.0.7 and basic config.
Any ideas ?
EDIT
I am becoming crazy, made a lot of tries, neither seems to work. When I start a new application with a few things, rake test works fine but when I try this on my current one, it launch always against my dev. db and do not work at all. I have tried to edit the test files, to revert them back, try to remove/setup test db with different ways, try different rake version, compare a lot of things on one side my current application and on the other a brand new one... Found nothing.. Help !
EDIT 2
Sounds lame, but is it normal that rake does the same thing than rake test ?
EDIT 3
Sounds odds, while I continue to work on what's wrong, I realize that every-time I run rake test, it does stuff on the dev environment and not the test one (watching the logs). It does this on my computer OSX and on our server FreeBSD for all the Rails 3.0.7 apps. Are you sure rake test is supposed to work on the test environment by default ?
EDIT 4
Please help!
EDIT 5 - SUMMARY
When running rake test in my computer or on our server in Rails 3.0.7 with different apps it does the following :
run CREATE TABLE and INSERT INTO migration against the dev. db.
do not empty the dev. db.
development.log gets written not the test.log
also an issue with the error unknowm comman 't' with one specific app.
EDIT 6 - db config
Nothing change from the default yet : https://gist.github.com/1006199
EDIT 7
rake db:test:prepare --trace -> nothing break (but keep printing (first_time)
https://gist.github.com/1007340
With RAILS_ENV="test" for rake, everything goes fine. It write on the test logs.
ruby -I test test/unit/category_test.rb same erros than with rake, but no write on the dev. or test logs.
a bunch of unorderd answers:
the "CREATE TABLE" statements usually means that your test_db is created from scratch (by default, before test task, a db:migrate is launched). are you sure they're called on dev_db?
also check your config/database.yml to see if there's some typo (eg: using same table for test and dev environments)
it looks like there's an error in some of your migration files (that 't' error remember blocks in migrations).
"rake test" is the default task, that's why it's run when you just launch "rake" without arguments.
EDIT:
according on what I see on edits, from 5 and above, it looks like you have some issue with environment files. so try to double-check:
* config/environments/test.rb
* config/application.rb
* config/environment.rb
if with RAILS_ENV="test", everything goes fine, then I'm almost sure you have changed some default behaviour in your app (configs, env variables, any particular gem?)
also, in your test/test_helper.rb, add RAILS_ENV='test' at the beginning of file, this should force test environment.
I had that same error message, except to me it said: in `test': unknown command 'i' (ArgumentError).
The 'fix' or 'workaround' was to simply use:
$> bundle exec rake test
instead of using 'rake test'