Running rake task from a daemon - ruby-on-rails

I have a daemon that i made using the daemons gem
I run it and it just closes without error, but the script doesn't seem to be actually running.
I'm running a rake task from it, will daemons run the commands inside from the directory it was called from?
This is the script
require 'rubygems'
require 'daemons'
Daemons.run('rake mytask')
the rake task has a loop which should stop it from exiting.

I'm not sure the way you're doing this will work with daemons. Can you move the rake task into an .rb file? Daemons is going to pass what ever you have in the run command to ruby, so in essence it is trying to run "ruby rake mytask"

Related

How come rake needs bundle exec, but rails doesn't?

I appreciate all the answers out there as to what bundle exec does, which is that it runs the following commands in the context of the Gemfile bundle. But why doesn't "rails server" need bundle exec? Seems like it should still apply.
The rails command runs from the executable inside the script folder. If you remove this folder, you can see that rails commands stop working. rake however runs differently.

Cant start resque worker

I've installed resque gem, gem install resque.
And I tried to start the workers :
bundle exec rake environment resque:work
Resulting in :
rake aborted!
Don't know how to build task 'resque:work' (see --tasks)
(See full trace by running task with --trace)
When I do rake -T no resque tasks appear, what am I going wrong here?
You should put the resque gem into the Gemfile instead of installing it directly.
Installation documentation (as of the time of writing) says that...
To start a worker, add this to a file in lib/tasks (ex: lib/tasks/resque.rake):
require 'resque/tasks'

Rails-way to execute one-off task on Rails server startup

I have Rails 4.2 application and I want to execute some one-off code when server starts.
My first approach was to use initializer in config/initializers to run the code. But in this case code is being executed also for all the rake tasks and Sidekiq process.
So I've created a rake task to run my code. Now I wonder how to execute it along with rails server startup. Of course, I can create a shell script that will execute my rake task and then start the server. But is there any rails-way to achieve this?
Foreman is another approach advised by SO, but it's not working for me as my task is not a daemon and the process terminates immediatelly after completion. Apparently all the processes in Procfile have to be daemonized.
Can be achieved with Foreman by running in the same process as daemonized task (e.g. with rails server).
If one-off taks is rake lego:update_all
Then corresponding Procfile is
web: rake lego:update_all && rails s
sidekiq: bundle exec sidekiq

Under what circumstances would a Ruby $LOAD_PATH be acquired from a parent process?

In my cucumber scenarios, if I call rake db:schema:load within a target Rails app folder, I get the cucumber process's $LOAD_PATH and not the Rails app's own Gemfile/load path. I think this is very weird.
The consequence is that I get the following error:
no such file to load -- rails/all
I can't reproduce it outside of my cucumber scenario.
ruby -rubygems -e "system 'rake -T'"
works normally -> the 'rake -T' has the application's own Gemfile-based $LOAD_PATH; and doesn't generate the error above.
Can anyone think why a child process (rake -T or rake db:schema:load or rails runner...; invoked by either system, exec, %x[...] or backtick; would start with the parent processes' $LOAD_PATH (from the cucumber scenario's Gemfile) instead of its own $LOAD_PATH (from the Rails app's Gemfile)?
When you use bundler, either via bundle exec or require 'bundler/setup', it finds your Gemfile, then puts its location in ENV["BUNDLE_GEMFILE"]. However, if this is already set, then bundler just reuses the value. This is what causes your Rails app to use the cucumber process's Gemfile.
If you want to execute something in the context of a different Gemfile, clear out ENV["BUNDLE_GEMFILE"] first. Bundler provides the method Bundler.with_clean_env(&blk) that may be helpful; it executes your block with the environment how it was before Bundler was loaded. Of course, you can also clear it out by hand with something like system("env -u BUNDLE_GEMFILE rake sometask").
The parent process's environment (ENV) is passed down to sub-shells. Either cucumber itself, how you are running cucumber (e.g. bundle exec cucumber), your scenarios, or code the scenario loads (e.g. the app, and therefore bundler), is messing with your ENV. Environment variables like RUBYLIB, GEM_PATH, and BUNDLE_GEMFILE can have a significant impact on what your sub-shelled Ruby processes can load / will behave.
Try printing out your ENV variable in your scenario and comparing it to what you get when you do it with ruby -rubygems -rpp -e "pp ENV", or just env on the command line.
For what it's worth, a possible alternative would be to load and invoke the rake task directly, e.g., Rake::Task['db:schema:load'].invoke, without using a sub-shell. Depends on what you're trying to do, though.

running tests via the terminal and rake, load test_helper

I am having a problem running my tests from the terminal and from rake, e.g. rake test:integration
At the moment, I have the requires for test_helper.rb specified like this:
require File.dirname(__FILE__) + '/../test_helper'
This works fine when running them from the terminal but obviously when it is ran from rake, the directory is different and the process cannot find the test_helper file.
I think I want to add to this to my $load_path but I am not sure how to add it when running only in the test environment.
Can anyone help me out?
You can revert to just require 'test_helper' (the default for integration tests, at least with Rails 2.3.x). This will allow tests to run from a rake task, and as long as you cd to the test directory within your rails app, you can run tests via the terminal with ruby integration/your_test.rb.

Resources