I recently made a Ruby gem called dvi_scrape for a new Rails web site that I'm working on. The dvi_scrape gem downloads web pages, scrapes financial data from them, process the data, and outputs the results to a Postgres database.
I'm able to get the Ruby command Dvi_scrape.dopeler to work as expected when executed manually. However, I'm unable to get it to work as expected when executed through cron.
How do I get this to work from cron on my WebFaction account?
The source code of the Rails site I'm working on this . The source code of the dvi_scrape gem is at this place .
I understand that config/schedule.rb is where you specify what scripts need to be run and at what time intervals. config/environment.rb is where you specify the environment. config/deploy.rb is where you specify what happens when you enter "cap deploy".
Are there any good examples of scripts that execute commands from certain gems at regular intervals? Please point me to some good examples on GitHub.
I don't have any "good examples on GitHub", but...
Assuming you have the gem installed into some specific web application (ie, you followed WebFaction's documentation for installing gems), you probably need to set some environment variables to make it usable when running from cron.
Try setting them in your cron job, like this (my example uses a 10-minute cron interval, change it to whatever you need):
*/10 * * * * GEM_HOME=$HOME/webapps/yourapp/gems RUBYLIB=$HOME/webapps/yourapp/lib PATH=$HOME/webapps/yourapp/bin your_command_here
Hope that helps!
Related
I want to automatically send a list of orders to all my suppliers from monday to friday at 11:00am. How can I do that ?
I am using Rails 5.1
I know that ActiveJobs has the Notifier.deliver_later function but if I'm right, it only works with relative times (eg: 10 hours from now). I actually need emails to be sent at 11:00 precise.
You can use whenever gem for this task. It provides a clear syntax for writing and deploying cron jobs.
Install this gem or add in gemfile. Go to your project directory and run whenevrize ..
$ cd /apps/my-great-project
$ wheneverize .
It creates a config/schedule.rb file in your project. Now you can add your task in this file and the gem will take care of everything.
For your case it'll look something like this.
every :weekday, at: '11:00 am' do
runner "MyModel.task_to_run_at_eleven_in_the_morning_on_weekdays"
end
I'd suggest you use Sidekiq with Sidekiq-cron this gem can work on high loaded projects, has Sidekiq UI (from Sidekiq), you can observe and relaunch jobs by your self from Sidekiq UI side.
Sidekiq more flexible and more useful can works with concurrency, has queues with privileges etc. (all advantages of Sidekiq)
I am trying to run a cron-task using Rails schedule.rb file. The task invokes a function written in ruby. The function runs perfectly fine. However when trying to run as a cron I get this error.
Starting Spring server with `/home/ubuntu/.rvm/gems/ruby-2.4.0/gems/spring-2.0.2/bin/spring server --background` timed out after 20 seconds
Spring(2.0.2) is installed and working perfectly.
Any idea how to solve this?
If the command works outside of cron, but not in the crontab, the problem is almost certainly that the command isn't picking up some necessary environment variable setting. There are several ways to get around the problem, but the simplest and best is to wrap your command in a shell script.
For initial testing, you can simply source your login environment:
. ~/.bash_profile
But eventually you'll want to just set the variables you need and not include anything extra. For more information, see Define your own job types.
My intention is to install Redmine on Heroku.
On redmine.org, there are two docs that I came across:
http://www.redmine.org/projects/redmine/wiki/RedmineInstall http://www.redmine.org/projects/redmine/wiki/HowTo_Install_Redmine_on_Heroku.
I know the second doc is self-explanatory in its title but I want to know if I follow the first doc's instructions, would I be able to still deploy Redmine to Heroku. OR is it better that I follow the second doc's instructions?
A noob at this, any feedback would be appreciated. Thanks in advance.
==============
Following the Heroku specific instructions (step 5), I tried to run rake generate_secret_token using Ruby's CMD however I get back 'Please configure your config/database.yml first'. There are two related files in two different locations. C:\Users\\redmine\config AND from C:\Users\\Desktop\\redmine-2.5.1\config. Which database.yml do I use? The config file on the second path has only database.yml.example. Do I make the change there and save it as 'database.yml'? Or do I make the change in the first location, cd to the first location and run rake generate_secret_token?
Follow the Heroku-specific instructions. The first document explains how to install Redmine on a server that you have full (root) control over. It entails logging into the machine and running various commands to install software.
Heroku does not give you a full server instance in the way the first document requires. Instead, you work on your application on your own machine and push it from there to Heroku. Things like database setup are configured through Heroku addons. You do not get access to the filesystem, which is in fact read-only.
The first document wouldn’t work for installing on Heroku.
I need some code executed once per day. Can be more than once a day and missing a day isn't the end of the world. That code will make sure users get some bonus points based on certain criteria. I'll keep track if they've already received the bonus points so it doesn't double up..
Some simple cron job calling a particular controller once in a while is perfect:
curl http://localhost/tasks/pulse
Of course a real crontab entry works great. Or is there an internal mechanism for this kind of thing in Rails? I'm using the latest stable Rails (currently 3.2.9).
The only wrinkle is this needs to work in Heroku too.
I just noticed Heroku's Scheduler. Looks great for Heroku. I can just run those tasks in my dev/test environment manually. Is this the best way to handle pulses/cron jobs in Rails? With rake tasks? Easy to incorporate running rake tasks in tests?
The Heroku Scheduler works great and is easy to set up!
You could check out this gem called whenever its a Ruby gem that provides a clear syntax for writing and deploying cron jobs. It's well maintained, not used it on Heroku myself but worth a look.
You can do loads of stuff like
every 3.hours do
runner "MyModel.some_process"
rake "my:rake:task"
command "/usr/bin/my_great_command"
end
I'm using rufus-scheduler for handling cron jobs for a Rails 3.2.x app. The root worker.rb is being fired off by foreman (actually upstart on this particular server) and therefore when it starts off it does not have the Rails context in which to operate. Obviously when I attempt to call logger or Rails.logger it will fail each time.
I'm using log4r as a replacement for the default Rails Logger, which is quite nice, but I am wondering what the proper solution for this problem would be:
1) Do I need to give the script the Rails context at startup (it is simply running rake tasks right now so it ends up getting the Rails environment when the worker script hands off to the rake task and I fear giving the script the Rails env before running the timed task would be overkill since it takes so long to fire up the env)? or
2) Should I just set up log4r as one would do in a non-Rails Ruby script that simply reads in the same log4r.yml config that the Rails app is using and therefore participate in the same log structure?
3) Or some other option I'm not thinking of?
Additionally, I would appreciate either an example or the steps that I should consider with the recommended implementation.
For reference, I followed "How to configure Log4r with Rails 3.0.x?" and I think this could be helpful when integrated with the above: "Properly using Log4r in Ruby Application?"
I think this might be what you're looking for..
Use this within the worker itself, and create a custom named log file
require 'log4r'
logger = Log4r::Logger.new('test')
logger.outputters << Log4r::Outputter.stdout
logger.outputters << Log4r::FileOutputter.new('logtest', :filename => 'logtest.log')
logger.info('started script')
## You're actual worker methods go here
logger.info('finishing')