how to update db on midnight everyday on rails3 - ruby-on-rails

i have a job to update some information in db every midnight ,so i want to find a way can use rails model to update information to db,
what's the common soluation?

Use a cron job which then runs a rake task. Define the task like this in _lib/tasks/your_thing.rake_:
task :your_thing => :environment do
# do stuff
end

Delayed Job https://github.com/tobi/delayed_job

Resque https://github.com/blog/542-introducing-resque

Related

Ruby on Rails filling database with scraped data daily

I'm trying to setup a process which will scrape web data from a set of websites on a set schedule (maybe monthly, daily...etc). I want it to then fill the database tables. What would be the best way to do this? Would it be best to create a ruby script outside of rails, and then use a cron task on my own schedule to fill the database? Or is there a way I can do this within the rails framework?
Step 1: Create a rake task
ie: lib/tasks/scrapping.rake
namespace :scrapping do
desc "Fetches new data from websites"
task scrap_websites: :environment do
# Call your scrapping classes/jobs/whatever code here
end
end
Step 2: Create a CRON task calling your rake task
You can use a gem like whenever for this: https://github.com/javan/whenever
For instance, your config/schedule.rb could look like this:
every 1.day, at: '4:00am' do
rake 'scrapping:scrap_websites'
end

Rails - How to auto-transfer records from table to table at a specific time?

I am pre-storing records in a table A and I want to transfer these records from table A to table B automatically at a specific time, lets say on every evening at 08:00 PM.
Any ideas on how to solve this little problem?
You could create rake task to implement your job, and then schedule it with cron, default *nix time manager. Its syntax is difficult to remember, so I prefer to use Ruby wrapper around it, gem whenever.
You can use whenever gem to run cron jobs ...for example job that runs every 5 mins
in schedule.rb
every 5.minutes do
rake "transfer_data:send_data"
end
lib/tasks/send_data.rake
#!/usr/bin/env ruby
namespace :transfer_data do
desc "Rake task to transfer data
task :send_data => :environment do
## code to transfer data from one table to other table
end
end
Execute the task using bundle exec rake transfer_data:send_data

Rails rufus-scheduler jobs returning nil

I'm using rufus-scheduler to schedule jobs at a certain date through the following code:
job = Rufus::Scheduler.singleton.schedule_at #post.read_attribute(:parse_time).to_s do
end
I then save the id of that job in my post class
#post.update_attribute(:job_id, job.id)
However, if I try and access that job again by calling:
Rufus::Scheduler.singleton.job(#post.read_attribute(:job_id)).unschedule
I get an error because the job is nil. If I try and look at the jobs of the Scheduler by calling:
Rufus::Scheduler.singleton.jobs
I get a blank array. Can anyone explain why my jobs aren't saving properly kept / being tracked?
Here's my initialization file for the scheduler. Do I have to do anything to enable singleton though? Or does it come with rails automatically:
require 'rufus-scheduler'
# Create singleton rufus scheduler
s = Rufus::Scheduler.singleton
rufus-scheduler doesn't keep triggered jobs around.
Your job has probably triggered and is gone.

How to add numeric value to variable each month?

I have an instance variable #balance = 300 that I would like to automatically add 300 to its value each month (or 30 days). How would I go about doing this in rails?
You can use a scheduler, sidekiq or cron job, if you are using heroku then there is an easy scheduler call heroku scheduler.
Just make a rake task with code like this:
if Time.now.day == 1
#balance += 300
end
And set the scheduler to use this task everyday. If you are not using heroku, then you will have to implement a cron job.
I suggest storing it somewhere in the database, using a gem like
sidekiq
Railscast for it
or
https://www.ruby-toolbox.com/categories/Background_Jobs
Any of these.

Scheduling in ruby on rails involving database access

I want to schedule daily reports to subscribed users via email.
For that I have written action in reports_controller that fetch data from database & convert it into pdf using pdfkit/wkhtmltopdf.The action works fine when called from get request.But when converted so that be defined like
def self.dailymail
ac = ActionController::Base.new()
kit = PDFKit.new #retrieve data from db
pdf = kit.to_pdf
ReportMailer.send_reports(ac.send_data(pdf)).deliver
end
It raises exception at send_data call when used with rufus scheduler:
RackDelegation#content_type= delegated to #_response.content_type=, but #_response is nil: #<ActionController::Base:0x206b068 #_routes=nil, #_action_has_layout=true, #_headers={"Content-Type"=>"text/html"}, ...
so, my question is what how can I solve this problem or Is there any alternate scheduler in rails that work fair on both Windows and Linux?
I wish to know any scheduler that can be helpful to send reports fetched from database.
I agree with claasz regarding the rake task. Check out the whenever gem https://github.com/javan/whenever
There is no suport for windows Task Scheduler, but it does support creating cron jobs.
Check out the documentation for the details, but esentially the gem creates cron jobs based on what you configure in the schedule.rb file that is created when you install the gem.
sample content of schedule.rb:
every 3.hours do
runner "MyModel.some_process"
rake "my:rake:task"
command "/usr/bin/my_great_command"
end
This would be like running bundle exec rake my:rake:task every 3 hours
After creating the schedule.rb you will need to run the whenever command from the console in order to add your schedule to cron. If you run whenever without arguments, the output shows you the contents of the schedule.rb. There is an argument you need to provide that I can't remember off the top of my head, just pass --help and I think you'll get the answer.
Hope this helps
EDIT:The argument is -w to write to cron-tab
As willglynn already points out, you should get rid of any controller interaction. There's simply no need here and it makes things unnecessarily complicated. So your code should look more like
def self.dailymail
kit = PDFKit.new #retrieve data from db
pdf = kit.to_pdf
ReportMailer.send_reports(pdf).deliver
end
If you got problems with the rufus scheduler (which I don't know), you could create a rake task to send out your mails and use the OS scheduler (e.g. cron on Linux) to call the task. Having the rake task would be also convenient for testing.

Resources