Clockwork Ruby Gem: How to restart? - ruby

How does one restart the Ruby clockwork gem?
After reading the Wiki, it seems you can only start it, not stop or restart it.
I don't want to manually kill the process and run it again.

The modern syntax to restart clockworkd is:
bin/clockworkd -c periodic-jobs.rb reload
If you've bundled your gems, as you should:
bundle exec bin/clockworkd -c periodic-jobs.rb reload
…where periodic-jobs.rb is your clockwork jobs config file.
Full options:
bin/clockworkd help
Usage: clockworkd -c FILE [options] start|stop|restart|run
--pid-dir=DIR Alternate directory in which to store the process ids. Default is /Users/jm3/Code/soakcity/tmp.
-i, --identifier=STR An identifier for the process. Default is clock file name.
-l, --log Redirect both STDOUT and STDERR to a logfile named clockworkd[.<identifier>].output in the pid-file directory.
--log-dir=DIR A specific directory to put the log files into (default location is pid directory).
-m, --monitor Start monitor process.
-c, --clock=FILE Clock .rb file. Default is /Users/jm3/Code/soakcity/clock.rb.
-d, --dir=DIR Directory to change to once the process starts
-h, --help Show this message
Learn more in the Demonization section of the clockwork source on GitHub. Hope that helps!

Assuming you started it as a daemon, then 'clockworkd -c YOUR_CLOCK.rb stop' should do the trick.

Related

Running process in .conf file

I have a simple file server_config.conf with below content
[program:flaskapp_server]
command=gunicorn app:app
numprocs=1
directory=/home/ubuntu/flaskapp/
user=ubuntu
And I have one more file start_server_config.sh
I want to run the program flaskapp_server in background from this bash script. How can I do that.
I saw an answer somewhere that It has be copied in some directory conf.d and supervisorctl will read it. But I want to execute and kill it from the bash script. How can I achieve that
After lot of searching I got a solution
this command can run processes in .conf
supervisord -c /home/ubuntu/flaskapp/super.conf

Listen gem and monitoring for filesystem changes

I've just come across the listen gem which watches for file system events (or polls) and can execute your ruby code when a file changes or is added.
It looks really nice, but if I need to catch any change/addition to the files in a directory, is there a way for me to catch any files that were changed since the script was last run?
ie. is there any way to have Listen work with persistence?
If directory change events order matters, then you probably cannot get that order after your script has stopped working, as it lost and cannot be retrieved from the filesystem.
Otherwise I would utilize git repo to collect changes after previous commit, making commits before script stops working.
Try on console with below command to fix the issue
echo fs.inotify.max_user_watches=524288 | sudo tee -a /etc/sysctl.conf && sudo sysctl -p

init.d script generator for ruby processes?

I have some ruby processes, and for development I've been using foreman to manage the processes, now in production I want to export the processes and run it in /etc/init.d directory.
Is there any tool that can help with that, by creating the scripts and the related PID to /var/run ? I tried to use foreman export feature to do so, but it didn't work to do that, since it can do something for inittab or upstart but not init.d.
Any help would be highly appreciated
mm it seems no tool to create that , foreman can export inittabs, upstart but not init.d, the only way is to modify the file :
/etc/init.d/skeleton
You will need to slightly modify it, and then :
chmod +x /etc/init.d/process_name
sudo update-rc.d process_name defaults
sudo /etc/init.d/process_name (start| stop| reload )
Done . :)
Another Answer could be with using https://github.com/ghazel/daemons gem .
require 'daemons'
require 'process'
Daemons.run('process.rb')
Once the script is done, just call it from your command line:
ruby process.rb run|start|stop|restart
This config will generate a "process.pid" file under "/var/run" and you can use monit to watch over the process by using this file.

Monit + RVM + Thin on OSX / Linux

After trying for hours (and also trying God and Bluepill) I decided to ask my question here because I am completely clueless how to solve this issue.
I have a Rails app. I want to use Thin as my app server. I want to use Monit to monitor my Thin instances. I use RVM to manage my Ruby versions as my local user.
I have the following monit file set up that would assumably do what I want it to do, but doesn't:
check process thin-81
with pidfile /Users/Michael/Desktop/myapp/tmp/pids/thin.81.pid
start program = "/Users/Michael/.rvm/gems/ruby-1.9.2-p180/bin/thin start -c /Users/Michael/Desktop/myapp -e production -p 81 -d -P tmp/pids/thin.81.pid"
stop program = "/Users/Michael/.rvm/gems/ruby-1.9.2-p180/bin/thin stop -c /Users/Michael/Desktop/myapp -P tmp/pids/thin.81.pid"
if totalmem is greater than 150.0 MB for 2 cycles then restart
If I simply copy/paste the start program in to the command line (outside of Monit) it works. Same goes for the stop program to afterwards stop the Thin instance. Running it via Monit however, does not seem to work.
Running it in -v verbose mode yields the following:
monit: pidfile '/Users/Michael/Desktop/myapp/tmp/pids/thin.81.pid' does not exist
Which leads me to believe that Thin never initializes. Does Monit run as root or something? Cause if it does then it obviously won't have the correct gems installed since I'm using RVM and not the "system" Ruby. I am currently on OSX (but will deploy to Linux eventually) - does anyone know what the cause of this might be? And if Monit is run via root, how could I make it use RVM regardless? Or could I tell Monit to execute the start/stop programs as Michael:staff (I assume it would be on OSX?)
Any help is much appreciated!
monit clears out the environment and also doesn't run a shell for your command (let alone an interactive one). I find I have to do something like:
/usr/bin/bash -c 'export rvm_path=/home/foo/.rvm; . $rvm_path/scripts/rvm; cd my_ruby_app_path; $rvm_path/bin/rvm rvmrc load; ./my_ruby_app'
as the monit start command.
another option which I found in the RVM google group is as follows:
start program = "/bin/su - myuser -c '/path/to/myscript.rb start' "
su - user runs the user's shell as a login shell, so if the
user's shell is bash, it will cause ~/.bash_profile to be run so the
environment variables should be the same as just after that user
logged in.
We need the path for su, otherwise, monitrc would not able to find the su executable.
A better way would be to use an RVM wrapper to create a custom executable for thin. It will create the correct environment variables to use the right ruby and gems, and then launch thin. Read more about it using it with god here : https://rvm.io/integration/god/. It should work the same with monit
To create the wrapper:
rvm wrapper ruby#gemset bootup thin
Then change start program and stop program to use the executable you just created.

Setting path for whenever in cron so it can find ruby

My ruby is in /usr/local/bin. whenever can't find it, and setting PATH at the top of my cron file doesn't work either, I think because whenever is running the command inside of a new bash instance.
# this does not work
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin
# Begin Whenever generated tasks for: foo
0 * * * * /bin/bash -l -c 'cd /srv/foo/releases/20110429110637 && script/rails runner -e production '\''ActiveRecord::SessionStore::Session.destroy_recent(15)'\'''
# End Whenever generated tasks for: foo
How can I tell whenever where my ruby binary is? Making a symbolic link from /usr/bin seems messy to me, but I guess that might be the only option.
This question offers env :PATH, "..." in schedule.rb as a solution, but (a) I can't find any documentation of that feature anywhere in the docs (b) it doesn't seem to have solved the asker's problem (unfortunately it takes non-trivial turnaround time for me to just try it).
update actually it is in the bottom of this page, i'll try it now.
more info
I can't modify the cron command because it's generated by whenever
i verified that if I make a new bash shell with bash -l, /usr/bin/env finds ruby just fine
I just tried the exact command in cron, starting with /bin/bash, from the command line of that user, and it worked.
so, this is very mysterious...
The solution is to put this in schedule.rb:
env :PATH, ENV['PATH']
Here's a little guide I put together on the topic.
rewrite your crontab as
0 * * * * { PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/sbin ; export PATH ;/bin/bash -l -c 'cd /srv/foo/releases/20110429110637 && script/rails runner -e production '\''ActiveRecord::SessionStore::Session.destroy_recent(15)'\''' ; }
Or you should try to figure out why your BASH shell is not picking the PATH=... that is almost certainly in your .profile or .bash_profile.
I hope this helps.
As John Bachir pointed out, you can do it via env. But let me add more input. I am deploying on AWS Opsworks. Unfortunately they do not have a ruby manager (RVM, Rbenv, etc) installed by default.
The first thing I needed to do was SSH into the instance and figure out which ruby I was using. This was easy enough by executing the which ruby command in a terminal.
$ which ruby
/usr/local/bin/ruby
Cron was using ruby located at /usr/bin/ruby. This needed to be changed.
In schedule.rb, I have:
set :env_path, ''
env :PATH, #env_path if #env_path.present?
In local, env_path doesn't need to be set. For most users, the only thing to do is execute whenever as such:
bundle exec whenever --set 'environment=development' --update-crontab
On a staging / production environment, ruby may be installed elsewhere. So running this may be more appropriate:
bundle exec whenever --set 'environment=staging&env_path=/usr/bin/local' --update-crontab
You will need to replace /usr/bin/local with the output of echo $PATH.
In Opsworks, however, I needed to create a custom Chef recipe that looked like:
node[:deploy].each do |application, deploy|
execute 'whenever' do
user 'deploy'
group 'nginx'
cwd "#{deploy[:deploy_to]}/current"
command "bundle exec whenever --set 'environment=#{deploy[:environment_variables][:RAILS_ENV]}&env_path=#{ENV['PATH']}' --update-crontab"
end
end
I hope the information here is clear enough.

Resources