Last active
August 29, 2015 14:03
-
-
Save maebeale/fae7da6c8013e12a95fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
how to create rake task for importing from md (on web) | |
1. add new *.rake file to lib/tasks folder | |
2. add new task(s) via task method & pass it a block | |
a. add dependencies to show order, like this: | |
task :taskname1 do | |
dsfdfs | |
end | |
task :taskname2 => taskname1 do | |
dsfdfs | |
end | |
3. write outline of namespace | |
————————————————————— | |
—————————————————————- | |
#FYI: rake can do all kinds of things! http://www.jbarnette.com/2009/08/27/on-rake.html | |
#environment runs the rails env ck our http://jasonseifer.com/2010/04/06/rake-tutorial | |
#desc makes this descrip show up in rake -T #FYI info for user | |
#other awesome regexp sites: | |
#http://office.microsoft.com/en-us/word-help/find-and-replace-text-by-using-regular-expressions-advanced-HA102350661.aspx | |
#http://www.regular-expressions.info/charclass.html | |
—————————————————————— | |
#below runs this rake task every night on production server! so awesome!!! | |
15 * * * * cd /data/my_app/current && /usr/bin/rake RAILS_ENV=production books:importnew | |
#below is run w "rake books:importnew". line 37 will be a whole BUNCH of code for parsing instructions. | |
#see this for an example of parsing md into html.https://github.com/jjyr/minidown/blob/master/lib/minidown/document.rb | |
#we'll be parsing into hashes to update db??? | |
namespace :books do | |
desc "Rake task to get books data” | |
task :importnew => :environment do | |
books = Book.nba_search #not sure what nba_search is. needs to be replaced but leaving for now | |
books.each do |i| | |
i.each do |hash| | |
@book = Book.new({ | |
# Code to instantiate a book | |
}) | |
@book.save | |
end | |
end | |
puts "#{Time.now} - Success!" #not sure we want this exactly, but prob a "completed" or something? | |
end | |
end | |
#at end, we need a test for it… http://blog.jayfields.com/2006/11/ruby-testing-rake-tasks.html | |
#here are a couple rake tests that are cool and good reference for later. | |
#i don't think we're going to need to access ActiveRecord or anything, but maybe we do to paste in data? | |
class RakeBooksImporter < Test::Unit::TestCase | |
def test_rake_imports_books | |
ActiveRecord::Base.expects(:connection).returns(connection=mock) | |
connection.expects(:tables).returns(['table1','table2']) | |
connection.expects(:execute).with("drop table table1") | |
connection.expects(:execute).with("drop table table2") | |
DatabaseFacade.clean | |
end | |
end | |
class RakeBooksImporterDupes < Test::Unit::TestCase | |
def test_rake_imports_books_with_no_dupes ActiveRecord::Base.expects(:connection).returns(connection=mock) | |
connection.expects(:tables).returns(['table1','table2']) | |
connection.expects(:execute).with("drop table table1") | |
connection.expects(:execute).with("drop table table2") | |
DatabaseFacade.clean | |
end | |
end | |
——————————————————————— | |
task :importlist do | |
dsfdsf | |
end | |
task :parsetext do | |
sdfd | |
end | |
desc “import md to books” | |
task :syncmdlist => [:importlist, :parsetext, …] do | |
sldfjdskf | |
end | |
—————————— | |
http://codedecoder.wordpress.com/2013/05/03/custom-rake-task-in-rails/ | |
desc "count number of records in given model table" | |
task :count_records, [:model] => :environment do |t, args| # task with both arguments and dependency | |
args.with_defaults(:model => "User" ) | |
model_class_name = Object.const_get(args.model) # this is very important as when you#pass argument it come as a string, so you must convert it to model class | |
puts "No of records is #{model_class_name.count}" | |
end | |
——————————— | |
http://eewang.github.io/blog/2013/03/12/how-to-schedule-tasks-using-whenever/ | |
namespace :events do | |
desc "Rake task to get events data" | |
task :fetch => :environment do | |
events = Event.nba_search | |
events.each do |item| | |
item.each do |hash| | |
@event = Event.new({ | |
# Code to instantiate an event | |
}) | |
@event.save | |
end | |
end | |
puts "#{Time.now} - Success!" | |
end | |
end | |
—————————— | |
task :fetch_and_save => :environment do | |
loop do | |
Model.method() # Do whatever you need to do here | |
end | |
end | |
——————————- | |
for looping a rake task w cron… | |
*/12 * * * * rake your task | |
## your rake task - valid for 10 mins -- 5 x 2 mins sleep worth. | |
@loopcheck = true | |
1.upto(5) do | |
begin | |
....(code here)... | |
rescue | |
Exception => e | |
@loopcheck = false | |
end | |
break if not @loopcheck | |
sleep(120) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment