Read It Later ã® RSS ã«ã¿ã¤ãã«ã¨æ¬æã¤ãã Web ãµã¼ãã¹ã®è£å´
ãã®ãµã¼ãã¹ã¯ Heroku ã®ä¸ã§ Sinatra ãåããã¦ããã
Heroku + Sinatra ã®çµã¿åãããæ¡å¤æ
å ±ãå°ãªãã£ãã®ã§
ã·ã§ã¢ããããã®ã¨ã³ããªã
Heroku + Sinatra + ActiveRecord (migrate) + cron ãªäººã®åèã«ãªãããã
å
¨ã½ã¼ã¹ã³ã¼ãã¯
http://github.com/mallowlabs/read-it-later-rss ã«ä¸ãã¦ããã
ã©ã¤ã»ã³ã¹ã¯ MIT Lisence ã§ã
Heroku + Sinatra + ActiveRecord
ã¾ãã¯ã¤ã³ã¹ãã¼ã«
.gems ã«ä»¥ä¸ã®ããã«è¨è¿°
sinatra activerecord
ãã®ç¶æ 㧠push ããã° gem ãèªåã§ã¤ã³ã¹ãã¼ã«ãããã便å©ï¼
Deploying Rack-based Apps | Heroku Dev Center ãèªãã¨
require 'activerecord' dbconfig = YAML.load(File.read('config/database.yml')) ActiveRecord::Base.establish_connection dbconfig['production']
ã¨æ¸ãã°ããã¨æ¸ãã¦ãããå®éã¯ããã
require 'logger' require 'active_record' ActiveRecord::Base.logger = Logger.new(STDOUT) dbconfig = YAML.load(File.read('config/database.yml')) ActiveRecord::Base.establish_connection dbconfig['production']
active_record ã® "_" ã¯å
¥ããªãã¨è¦åãåºãã
require ã®ã¨ã㯠"_" ãå
¥ãããã©ã.gems ã«ã¯å
¥ããããã¡ã
ã¾ããLogger ãè¨å®ããªã㨠AR ãèµ°ã£ãã¨ãã«ã³ã±ãã
Heroku + Sinatra + ActiveRecord (migrate)
æ®éã« migrate ãã¡ã¤ã«ãä½ãã
db/migrate/001_create_base.rb
class CreateBase < ActiveRecord::Migration def self.up create_table :pages do |t| t.text :url t.text :title t.text :content t.timestamps end add_index :pages, :url end def self.down remove_index :pages, :url drop_table :pages end end
Rakefile ã«ä»¥ä¸ã追å ã
namespace :db do desc "migrate database" task :migrate do ActiveRecord::Migrator.migrate('db/migrate', ENV["VERSION"] ? ENV["VERSION"].to_i : nil) end end
Sinatra ã¢ããªã®ã©ããã§ä»¥ä¸ãå¼ã¶ã
çè
ã®å ´å㯠lib/boot.rb
Dir::glob("#{File.dirname(__FILE__)}/models/*.rb") do |f| load f end
Model ç¨ã¯ã©ã¹ãä½ãã
lib/models/page.rb
class Page < ActiveRecord::Base end
è¨å®ãã¡ã¤ã«ãå¿ããã«ä½ãã
ãã ãããã®ãã¡ã¤ã«ã¯ Heroku å´ã« push ãã¦ã¯ãããªãã
config/database.yml(.example)
ãã¼ã«ã«ã® DB ã«åæ ããå ´å㯠Rails ã¨ä¸ç·ã§
$ rake db:migrate
Heroku å´ã® DB ã«åæ ããå ´åã«ã¯
$ heroku rake db:migrate
Web ã調ã¹ã㨠taps ã使ãã¨ããè¨è¿°ãã¿ããããã©
ãã¼ãã«ä½ãã ããªãä¸è¨ã³ãã³ãã§ååã
Heroku ä¸ã§ã¯ config/database.yml ãèªåã§ä½ããã¦
PostgreSQL ã®ãã¼ã¿ãã¼ã¹ã使ããã¨ãã§ããã
config/database.yml ã push ãã¦ã¯ãã¡ã¨ããã®ã¯ããããæå³ã
Heroku + Sinatra + cron
ã¾ã大åæã¨ãã¦
- ç¡æãã©ã³ã§ã¯ daily cron ãã使ããªã
- daily cron ã使ãããã«ã¯ã¯ã¬ã¸ããã«ã¼ãæ å ±ã®ç»é²ãå¿ è¦
ã¨ãããã¨ã«æ³¨æã
Heroku Scheduler | Heroku Dev Center ãèªãã¨
cron ã¿ã¹ã¯ã¯ environment ã«ä¾åãã¦ããã©
Sinatra ã¢ããªã«ã¯ãããªãã®ã¯ãªãã®ã§åç´ã«åé¤ã
Rakefile ã«ä»¥ä¸ã追å
task :cron do Page.delete_all(["created_at < ?", Time.now - 30.days]) end
Heroku ã®ç¡æãã©ã³ã 㨠DB 㯠5MB ãã使ããªãã®ã§
30æ¥ä»¥ä¸å¤ããã¼ã¿ãåé¤ããããã«ãã¦ããã
以ä¸ã®ã³ãã³ã㧠daily cronæå¹ã«ããã
$ heroku addons:add cron:daily
cron ãä»»æã®ã¿ã¤ãã³ã°ã§å®è¡ããã«ã¯
$ heroku rake cron
cron ã®å®è¡ãã°ãè¦ãã«ã¯
$ heroku logs:cron
ããããããªãã¤ã³ãã¯ä»¥ä¸ã
ããããªãé¨åããã£ããã½ã¼ã¹ãèªãã§ãã ããã