The Rails Command Line

제 71회 Biweekly Lecture

2014. 9. 30

Lucius Choi

- ROR Lab. Season 4 -

Content

Rake

Runner

Command line

Scheduler

Commands

  • rails
  • rake
  • bundle

Rake

  • Make program for Ruby
  • .rake
$ bin/rake --tasks

Custom Rake Tasks

  • Rails.root/lib/tasks
  • .rake
desc "custom tasks"
task :custom_task1 do
  # ...
end


namespace :custom do
  desc "custom tasks"
  task :task1 do
    # ...
  end
end


$ rake -T
...
rake custom:task1                       # custom tasks
rake custom_task1                       # custom tasks
...
# task dependency

desc "인사말 출력하기"
task :rorlab_greeting do
    puts "Hello!"
end

desc "여러분"
task :rorlab_to => :rorlab_greeting do
    puts "Everyone~"
end

# grouping tasks

namespace :rorlab do
    desc "인사말 출력하기"
    task :greeting do 
        puts "Hello!"
    end
    
    desc "누구에게"
    task :to, [:who] => :greeting do | t, args |
        puts "#{args[:who]}~"
    end

    desc "누구에게 인사말 출력하기"
    task :all => [ :greeting, :to ]
end

Custom Rake Task 작성하기 

Rails Runner

  • Runner runs Ruby code in the context of Rails non-interactively.
  • http://apidock.com/ruby/FileUtils/cp
  • Copy db/development.sqlite3 per 1 minute

Cron jobs

using "whenever"

  • crontab -l
  • gem 'whenever'
  • wheneverize .
  • config/schedule.rb

config/schedule.rb

set :environment, :development

# path-to-bundler를 자신의 경로로 변경해야 함.
set :bundler_path, "/Users/hyo/.rbenv/shims"

set :output, {:error => 'log/error.log', :standard => 'log/cron.log'}

job_type :runner, "cd :path && :bundler_path/bundle exec rails runner -e :environment ':task' :output"

every 1.minute do
  runner "Cron::TestRunner.copy"
end

"whenever" 젬으로 cron job을 편리하게...

The End

The Rails Command Line

By Hyoseong Choi

The Rails Command Line

제 71회 Biweekly Lecture

  • 1,412