0

I have a stock portfolio app that gets financial data from Yahoo Finance. I want to set up a feature that will calculate the portfolio's value (which will involve sending a get request to yahoo for all of the relevant stock prices and calculating the price * quantity and save that in the amount attribute of a portfolio object). I have a valuation model (that belongs_to a portfolio) that will be used to create valuation instances that will store the portfolio amount and date every day. I will then plot the portfolio's valuations on a graph to track the performance of it's stock picks.

I only want to run this once a day at the end of the day. As it stands now, I am thinking of simply using the whenever gem to schedule a task and run the above actions to fetch and calculate prices. Could this be a good use case for a background processing framework like Resque?

Coder_Nick
  • 781
  • 1
  • 8
  • 25
  • u can use this answer for reference http://stackoverflow.com/questions/40994581/rails-5-scheduler-to-update-database-once-a-day/40995192#40995192 – abhi110892 Dec 11 '16 at 09:37

1 Answers1

0

There are many ways to complete your task. One of the way is to write rake task and run them once per day. For scheduling you can use whenever gem. It provides convenience way to describe cron schedule.

Whenever docs:

Whenever is a Ruby gem that provides a clear syntax for writing and deploying cron jobs.

# We will run rake task every day at some time
every :day, :at => '12:20am' do
  rake 'calculate_averages'
end

Example of task:

require 'rake'

task :calculate_averages => :environment do
  products = Product.all

  products.each do |product|
    puts "Calculating average rating for #{product.name}..."
    product.update_attribute(:average_rating, product.reviews.average("rating"))
  end
end

To start read whenever docs and how to write rake tasks. How to write tasks #2.

Farkhat Mikhalko
  • 3,565
  • 3
  • 23
  • 37