Should this go in a background task? Are there any better options?
I'm a Rails noob and I'm trying to plan out the architecture of a little app I'm building to familiarize myself with OOP concepts.
Basically, it's a podcast fetcher.
My question regards this sample user story:
User is a listener of podcast "Buzz Out Loud", which is a daily podcast.
User goes to the page in my app for "Buzz Out Loud", which lists the ten most recent episodes.
User notices that today's podcast, which was published about an hour ago, is not listed on my site.
I've had a couple ideas about how to handle this but I feel like I'm missing a major piece of the equation here.
I could:
Send the "podcast_id" parameter to my method that updates the list of podcast episodes when the user loads the page, before anything renders, however, it 开发者_StackOverflow社区seems wasteful to do this so many times, especially when this method might potentially write to the database.
I could run a background task all day that collects episodes constantly. This seems wasteful of the CPU.
This must be a common issue with a well-defined strategy. Can somebody please educate me?
Thanks,
Harris
This is something you would want to run as a background job since running it on every request is too heavy and useless, you can use a background runner gem such as http://railscasts.com/episodes/128-starling-and-workling or look at another option http://railscasts.com/episodes/243-beanstalkd-and-stalker.
Other options are also discussed in the comments section of these screencasts. You can also use a cron job that will run a script to check for new podcasts every 2 hours or whatever time delay you need.
精彩评论