running delayed_job under monit with ubuntu - ruby-on-rails-3

I'm struggling to get delayed_job working under rails 3.0.9 (ruby 1.9.2). The only way I have succeeded to run is to tape manualy the command rake jobs:work.
But I want that to be automatically started when the rails application is starting.
I have installed monit under ubuntu and I configured it to launch a file located in my app. This fails looks like:
check process delayed_job with pidfile /home/me/myapp/tmp/pids/delayed_job.pid
start program = "/home/me/myapp/script/delayed_job start"
stop program = "/home/me/myapp/script/delayed_job stop"
And I added the environment setting in the delayed_job script file:
#!/usr/bin/env ruby
ENV['RAILS_ENV'] = "development"
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
require 'delayed/command'
Delayed::Command.new(ARGV).daemonize
When I run the command "sudo monit start delayed_job" I get the following error:
/usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- bundler/setup (LoadError)
So I guess it is because sudo is using a wrong version of ruby environment
I tried then the solution of:
rvm monit delayed_job
by adding rvm -S in the start program / stop program lines.
But it still failing with the error : rvm command not found
my rvm dir is located in my home dir /home/me/.rvm
I tried to find workarounds in (sudo changes PATH - why?) to change the PATH environment variable by adding
/usr/bin/env PATH=/home/me/.rvm/bin:$PATH
The command "sudo monit start delayed_job" succeeded! and the worker started.
But the issue is: When I launch sudo /etc/init.d/monit start and when I look to the syslog I still get 'delayed_job' failed to start
So I don't know how to investigate more, how to get more verbose errors for monit.

I have finally succeeded to solve this issue.
I modified the monit file like this:
check process delayed_job with pidfile /home/me/myapp/tmp/pids/delayed_job.pid
start program = "/bin/su - me -c 'cd /home/me/myapp/; script/delayed_job start'"
stop program = "/bin/su - me -c 'cd /home/me/myapp/; script/delayed_job stop'"
I have also downgraded the daemons gem because it seems that there are problems with the latest version. So I'm using now daemons v 1.0.10
I also modified the rights of the log file /home/me/myapp/log/delayed_job.log, because it seems that is was created before my root and my user had no access to it (I had problems to test the command "script/delayed_job start" with "me" user)

This i s the only line that worked for me that read the ENV properly
start program = "/usr/local/rvm/bin/rvm-shell -c 'cd /var/www/[APP]/current/; RAILS_ENV=production bundle exec bin/delayed_job start'"
Hope it helps!

Related

Problems getting Singularity Compose to work

I wrote a small test project for Singularity Compose, consisting of a small server application, with the following YAML file:
version: "1.0"
instances:
server:
build:
context: ./server
recipe: server.recipe
ports:
- 9999:9999
When I call singularity-compose build, it successfully builds server.sif. Calling singularity-compose up also seemingly works without error, and calling singularity-compose ps results in something that looks just fine:
+ singularity-compose ps
INSTANCES NAME PID IMAGE
1 server 4176911 server.sif
However, the server application does not work, calling my test client results in it saying that there is no answer from the server.
But if I run server.sif directly without compose, everything works just fine.
Also, I tripple checked, my test application listens to port 9999, thus should be reachable from the outside.
What did I do wrong?
Edit:
I also checked whether there actually is any process listening at port 9999 by calling sudo lsof -i -P -n | grep LISTEN, this is not the case. Only when I manually start server.sif without compose it shows me the process listening.
Edit:
I went into the Singularity Compose shell and tried to start the Server application directly in there, just as a test, and it resulted in Permission denied. Not sure if that means anything.
Edit:
I now gave the application execution rights within the shell and called in there, this works. Am now trying to add execution rights in the recipe. If that works, it would be kind of strange, as the executable was build right there, and thus should already have execution rights.
Edit:
I added chmod +x in my recipe both after building Server and before executing it. Doesn't work either.
Also checked whether any bridges exist using brctl show, this is not the case.
Edit: My recipe, adjusted by the input of tsnowlan in his answer below:
Bootstrap: docker
From: ubuntu:20.04
%files
connection.cpp
connection.h
main.cpp
server.cpp
server.h
server.pro
%post
# get some basics
apt update
apt-get install -y wget
apt-get install -y software-properties-common
# get C++ compiler
apt-get install -y g++
apt-get install -y build-essential
apt-get install -y build-essential cmake
# get Qt
apt-get install -y qt5-default
# compile
qmake
make
ls
%runscript
/Server
%startscript
/Server
Again, note that the application works just fine both when compiled and startet normally and when started within a Singularity image (but without Singularity Compose).
The ls at the end of the %post block is used to verify that the Server application was build successfully.
Please share the server.recipe, as it is difficult to identify should be/is happening without it.
Without having that, my guess is that you have a %runscript in your definition file, but no %startscript. When the image is executed directly or via singularity run image.sif, the contents of %runscript determine what happens. To emulate the docker-compose style, the singularity images are started as persistent instances. In this case, the %startscript block determines what runs. If it is empty, it will just start up and sit there doing nothing. This would explain why when run by hand it works but not when using compose.

"file too short" on bundle install

I'm getting occasional "file too short" messages when running bundle exec rake:
rake aborted!
/var/lib/jenkins/.rvm/gems/ruby-1.9.3-p327/bundler/gems/amatch-0f95f4ce269f/lib/amatch_ext.so: file too short - /var/lib/jenkins/.rvm/gems/ruby-1.9.3-p327/bundler/gems/amatch-0f95f4ce269f/lib/amatch_ext.so
Is there a way to make bundler more fault-tolerant and try to re-run when it encounters these spurious failures?
Why might they be happening in the first place? Multiple processes may be executing rake tasks simultaneously - can this corrupt rvm's gem repository, and if so how do I avoid the problem?
if you use it in multiple processes then use bundle --standalone - assuming every process is ran from different path - if they all use the same path then you could try bundle --path /path/for/gems$$/ the $$ will be replaced with process pid - but --path is recorded option and this will not help as only the last run will be visible in this directory.
best would be to limit amount of runs that are performed at the same time.
other option would be modifying GEM_HOME at runtime, but this can get complicated with jenkins so most likely this would not work:
OLD_GEM_HOME=$GEM_HOME
GEM_HOME=$( mktemp -d )
cp -r $OLD_GEM_HOME/ $GEM_HOME/
bundle install
# other commands
rm -rf $GEM_HOME/
GEM_HOME=$OLD_GEM_HOME

apn_sender gem daemon not running

Im using apn_sender for rails 3 and i have been able to install the gem and get it working just fine by using
rake apn:sender
I have tried to get it started in production mode on a ubuntu box by starting the daemon and it does not seem to work. When i type
script/apn_sender --environment=production --verbose start
I dont see anything. No log present.
when i try to type
script/apn_sender status
It returns with
apn_sender: no instances running
Just trying to understand why it is not running.
i just solved this problem. Try to create a file called 'script' in generators/apn_sender/templates . .
Put this in your script file
# !/usr/bin/env ruby
# Daemons sets pwd to /, so we have to explicitly set RAILS_ROOT
RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
require 'rubygems'
require 'apn'
require 'apn/sender_daemon'
APN::SenderDaemon.new(ARGV).daemonize
bash 'rails g apn_sender' in your terminal and will create 'script/apn_server' with same content as above
After that bash this code
./script/apn_server --environment=production --verbose start
it will create log/apn_sender.log . Try running
APN.notify('token',{:alert => '#' , :badge => 1})
or else in rails c to confirm if it works or not , and of course
rake apn:sender
Hope it will help :)
EDIT
You have to install redis and configure

Guard complaints about running outside of Bundler, but only on 2nd terminal window onwards

I am following Michael Hartl's tutorial and ran this 2 blocks of code
$ rvm get head && rvm reload
$ chmod +x $rvm_path/hooks/after_cd_bundler
$ cd ~/rails_projects/sample_app
$ bundle install --without production --binstubs=./bundler_stubs
now when I run Guard on my first terminal window everything is fine, but when I open another terminal window and run the exact same command, it complaints that I am running Guard outside of Bundler. Why is that so?
Still can't post images but here is the screenshot of the 2 separate terminal windows
terminal 1
terminal 2
Thanks!
Ryan
tests
rvm current - is the proper ruby selected
echo $PATH - first position should be path to .../bundler_stubs
solutions
for both errors it might work again with cd . - but this might be problematic
you need to make sure that RVM was loaded properly, and that proper ruby was loaded in session:
make sure you use gnome-terminal with login session enabled: https://rvm.beginrescueend.com/integration/gnome-terminal/
run rvm get head --auto - keep a gist of it in case
restart your computer - it's required in some cases
make sure you do not overwrite PATH after RVM was loaded in your RC scripts

Rails 3 / whenever cron not firing

I am using whenever to fire a rake task every 5 minutes for my app.
schedule.rb:
every 5.minutes do
rake "audit",
:environment => 'development'
end
"whenever" in console:
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /Users/john/Sites/rambler && RAILS_ENV=development bundle exec rake audit --silent'
"rake audit" in console works properly.
So all looks good .... except it doesn't work. Nothing happens every five minutes.
Is this because I am trying to run it in development / local?
Thanks!
You need to update your cron file every time you change it.
After you have addded your cron job do this:
whenever --update-crontab 'project_name'
Also I only found whenever working fine in production mode only.
UPDATE:
I have found that we can use whenever in development mode also. Just add
set :environment, "development"
set :output, {:error => "log/error.log", :standard => "log/cron.log"}
to your scehdule.rb file. ( The log one is optional but still you can use that for testing purpose)
Finally I have solved how to run the gem Whenever. It's working good on production, but not in development mode (I think that to working good in dev mode you must do some tricks).
see this answer for working in dev mode: Cron not working in Whenever gem
Then, these are the processes to do:
install the gem
write your scheduler.rb file
push to the remote server
login to the remote server (for example with ssh)
see if whenever is good uploaded by running in terminal: whenever
update whenever crontab by running: whenever --update-crontab
restart the server crontab (for example in ubuntu): sudo service cron restart
check if crontab are good implemented on the server: crontab -l
That is!