Is there a way to use Resque to dynamically load Ruby libraries? - ruby-on-rails

I have a Reporter worker for Resque in my Rails 3.2.8 application. I frequently add new reports for users, or fix bugs in existing reports.
Reports are deployed as Ruby modules whose methods are called by the Resque reporter worker.
Every time I deploy new code, I have to restart Resque. During that time, there are often one or more reports out there that are then killed, left with a status of "Running". What I want to find out is, is there a way to get Resque to reload the ruby modules that it uses to run the reports?

Instead of reloading you could stop the resque workers with a kill -s QUIT. That will cause the workers to finish running their reports before shutting down.
More info on using signals with resque is here, https://github.com/defunkt/resque#signals.

Related

Sidekiq process failing

I have a rails 4.1 application with Sidekiq 3.3.2 in production mode with default config.
Everything works fine, but sometimes, I see in Sidekiq admin panel that Sidekiq process, not listed in the "busy" in the panel and does not exist in the list of processes.
That seems like a sidekiq's process has been fail.
In the log there is no error entries.
How can I make that stable?
This is known problem. Hard loaded Sidekiq worker is disappear sometimes from busy list but still working fine.
I known issue marked as fixed but seems like it appear again and again. Just don't worry about it and use ps aux | grep sidekiq to check running Sidekiq workers in this case.

Ruby on rails scheduled tasks

This is my first time scheduling a task and I am not sure of the best implementation (or the proper implementation).
My Goal:
I have a ruby on rails 4 app setup with twilio and deployed on Heroku. I want the app to automatically text all of my users once a week with a customized text message (which is written and created by information in the database).
From research I have come down to the following Gems: Whenever and Rufus-Scheduler.
I believe that both these gems can get the Job done, but upon reading on the Rufus' docs: "please note: rufus-scheduler is not a cron replacement" I got stuck trying to understand if what I want is indeed a cron job or a "Rufus-Scheduler".
I am left with the following questions: What is a cron job and when is the appropriate time to use it? Why is Rufus-Scheduler not a cron replacement and what does it do differently? Which one should I use?
About Cron:
Cron is name of program which does scheduled tasks on nix systems. what Scheduled Tasks are in Windows, Cron does something similar for Linux at the conceptual level.Cron is one of the most useful tool in Linux or UNIX like operating systems. The cron service (daemon) runs in the background and constantly checks the /etc/crontab file, and /etc/cron./ directories. It also checks the /var/spool/cron/ directory.
For Scheduling tasks on Heroku
Good news is that on Heroku there is a thing called Scheduler which is an add-on for running jobs on your app at scheduled time intervals, much like cron in a traditional server environment. so you really don't need to fiddle/player with cron or gems like whenever. just use the Scheduler addon on Heroku.
For More info see: https://devcenter.heroku.com/articles/scheduler
A cron job is a program run on an automated time schedule, using the cron software.
Rufus-Scheduler is different from cron because it runs inside of Ruby processes.
For what you're describing I believe either would be fine.
rufus-scheduler is not a cron replacement
can be expanded to "rufus-scheduler was never written as a drop-in replacement for cron". The bigger message is "you are a developer and as a developer you should know the environment you inherit and the tools it comes with. You should know when to use them, when to imitate them, when to replace them."
Rufus-scheduler understands the "* * * * *" syntax of Cron. This has led some people to say "rufus-scheduler tries to be a Cron replacement". It could better be formulated as "some people have abused rufus-scheduler instead of thinking (knowing) that old faithful Cron would have been better in that situation".
To become a good developer you should think seriously about some *nix sysadmin skills, else you'll be a pain to work with. It can be as easy as "install, run and manage a linux box in a VirtualBox in your system".
A rufus-scheduler schedule runs in a Ruby process. In a vanilla world, one runs rufus-scheduler in the same Ruby process that services http requests (Rails or Sinatra web application), and, oh, oops, the schedules don't run when the application doesn't run.
Cron is a service provided by your *nix operating system. Other applications and services on your host rely on it. Cron reads its crontab and runs the script indicated in it at the given times. Thanks to the excellent Whenever, Cron can be told to run scripts in your Rails application.
This might interest you as well: https://devcenter.heroku.com/articles/clock-processes-ruby
whenever allows you to write ruby code that would be transformed in a crontab file, which is a file specifying a set of commands and a frequency for each command. This file is used by cron.
rufus-scheduler is pure ruby, you write ruby and jobs are running in ruby, for example inside your application loop calling scheduler.join, or in another thread calling ruby your/rufus_scheduler_script.rb.
In my opinion they do the same things, with cron you are using a linux command, but I don't see any other difference.
There are other options too, in my experience I have had problems with rufus-scheduler, and whenever (they did not find classes in my Rails app, but maybe was just a quirk), on the other side clockwork worked for me.

Foreman auto restart

In my production server, I'm using ruby foreman to run multiple processes, I just want my application to keep working, even if one of the processes down, I want my processes to keep working even if one down , is there any tricky way to restart the process or even not to stop all the processes in case one went down ? I mean in production level I want the solution to be stable enough, is that possible without Upstart ? thanks in advance
You should not be using foreman itself for production - it is only intended as a development tool. Instead, you can use something like god with my foreman_god gem in production.
Alternatively, you can use foreman to export config files for other process monitoring systems, for example upstart.
You can monitor your foreman process with another program like http://mmonit.com/monit/. But somehow you'll find that monitoring a process which monitor other processes is kinda strange.

Can you reload a Rails app on Passenger in the same seamless way as you can reload one on Unicorn?

With Unicorn, you can restart and reload a Rails app with kill -USR2 [master process], which doesn't kill the process immediately, but starts a new master process + slave processes in the background. When the new master is ready, you can shut off the old master with kill -QUIT. This lets you restart your website without having any visitors notice a slowdown in request handling.
But with Passenger, you restart the Rails app with touch tmp/restart.txt, which as far as I can tell, causes the Rails app to become unresponsive for the few seconds it takes to restart the Rails application.
Is there a way to use Passenger, but also have the Rails app restart seamlessly?
Rolling restarts are available in Phusion Passenger Enterprise.
This is the "licensed version" klochner talked about, but it wasn't released until August. Phusion Passenger Enterprise fully automates rolling restarts (Unicorn requires some manual scripting to make rolling restarts behave in a good way). It also includes a bunch of other useful features such as deployment error resistance, live IRB console, etc.
No. [now yes - see hongli's response]
You're asking for rolling restarts, where the new server processes are brought up before the old ones are killed. Passenger (the free version) won't drop requests, but they will get queued and delayed whenever you deploy.
Rolling restarts has supposedly already been implemented and is available in the licensed version, but not yet released for the free version. I've been unable to figure out how to get the licensed version.
Follow this google groups thread for more info:
https://groups.google.com/forum/#!msg/phusion-passenger/hNvU-ZE7_WY/gOF9XWmhHy0J
You could try running two standalone passenger processes and manually bring one down while the other stays up, but I don't think that's the answer you were looking for.

How to make/monitor/deploy daemon processes in JRuby

I'm currently porting a Rails App currently using REE to JRuby so I can offer an easy-to-install JRuby alternative.
I've bundled the app into a WAR file using Bundler which I'm currently deploying to GlassFish. However, this app has a couple of daemon processes and it would be ideal if these could be part of the WAR file, and potentially monitored by Glassfish (if possible).
I've looked at QuartzScheduler, and while meets my needs for a couple of things, I have a daemon process that must execute every 20 seconds as it's polling the database for any delayed mail to send.
If anyone can provide any insight as to how best to set up daemon processes in a JRuby/Java/Glassfish environment any help will be greatly appreciated! :)
One way to daemonize a JRuby process is to use akuma framework (on *nix) or others.
I would rather use cronjobs (schedulling) rather than daemons as they are less error-proned, daemons can leak memory, can stop on errors etc. Check jruby-quartz and quartz_scheduler
EDIT
If one uses Torquebox it offers support for services and scheduling.

Resources