王筝的博客
ruby学习

此处接上一条博客

vim Gemfile
source "https://rubygems.org"

gem "sidekiq"gem 'rack-protection'
gem "sinatra"

 

vim config.ru
require 'sidekiq'
require 'rack-protection'


Sidekiq.configure_client do |config|
  config.redis = { db: 1 } 
end

require 'sidekiq/web'
run Sidekiq::Web

执行

(1)方法1
bundle exec sidekiq 
打开页面当前项目下启动http://localhost:3000/sidekiq
(2)
方法2
$ rackup

打开管理页面就可以看到了所有的任务了

http://localhost:9292/

 

可以在sidekiq启动的时候存入任务进程号

$bundle exec sidekiq -r ./worker.rb -P ~/tmp/sidekiq.pid
$cat ~/tmp/sidekiq.pid
1198
$ ps -ax | grep sidekiq
 1198 pts/28   Sl+    0:00 sidekiq 4.1.1  [0 of 25 busy]                                                             
 1259 pts/33   S+     0:00 grep --color=auto sidekiq

 

可以使用sidekiqctl关闭服务

$ sidekiqctl --help
sidekiqctl - stop a Sidekiq process from the command line.

Usage: sidekiqctl <command> <pidfile> <kill_timeout>
 where <command> is either 'quiet' or 'stop'
       <pidfile> is path to a pidfile
       <kill_timeout> is number of seconds to wait until Sidekiq exits
       (default: 10), after which Sidekiq will be KILL'd

Be sure to set the kill_timeout LONGER than Sidekiq's -t timeout.  If you want
to wait 60 seconds for jobs to finish, use `sidekiq -t 60` and `sidekiqctl stop
 path_to_pidfile 61`
$ sidekiqctl stop ~/tmp/sidekiq.pid 
Sidekiq shut down gracefully.

 

参考页面

https://github.com/mperham/sidekiq

https://github.com/mperham/sidekiq/wiki/Getting-Started

强烈推荐这个视频

 

$mkdir  sidekiq_playground
$cd sidekiq_playground/

$vim Gemfile

添加一个gem

source "https://rubygems.org"

gem "sidekiq"

新增worker.rb

vim worker.rb
require 'sidekiq'

Sidekiq.configure_client do |config|
  config.redis = { db: 1 } 
end

Sidekiq.configure_server do |config|
  config.redis = { db: 1 } 
end

class OurWorker
  include Sidekiq::Worker

  def perform(complexity)
    case complexity
    when "super_hard"
      sleep 20
      puts "super hard! Really took quite a bit of effort"
    when "hard"
      sleep 10
      puts "hard! That was o bit of work"
    else
      sleep 1
      puts "That wasn't a lot of effort"
    end 
  end 
end

安装redis,启动sidekiq

$sudo apt-get install redis-server
$ bundle exec sidekiq -r ./worker.rb

执行一下

$ bundle exec irb -r ./worker.rb 
irb(main):001:0> OurWorker.perform_async("easy")
=> "d40dc832d93f50c00afd2ab4"
irb(main):002:0> OurWorker.perform_async("hard")   //立刻执行
=> "33356c6a0c7cd55047d40670"

irb(main):003:0> OurWorker.perform_in(5,”easy”)   //5秒后执行
=> “7a517f4305ff6105aa408b4f”

 

查看启动页面里的输出

$ bundle exec sidekiq -r ./worker.rb
2016-03-31T07:11:15.792Z 30945 TID-gnktzwzl4 INFO: Booting Sidekiq 4.1.1 with redis options {:db=>1, :url=>nil}


         m,
         `$b
    .ss,  $$:         .,d$
    `$$P,d$P'    .,md$P"'
     ,$$$$$bmmd$$$P^'
   .d$$$$$$$$$$P'
   $$^' `"^$$$'       ____  _     _      _    _
   $:     ,$$:       / ___|(_) __| | ___| | _(_) __ _
   `b     :$$        \___ \| |/ _` |/ _ \ |/ / |/ _` |
          $$:         ___) | | (_| |  __/   <| | (_| |
          $$         |____/|_|\__,_|\___|_|\_\_|\__, |
        .d$$                                       |_|

2016-03-31T07:11:15.792Z 30945 TID-gnktzwzl4 INFO: Running in ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-linux]
2016-03-31T07:11:15.792Z 30945 TID-gnktzwzl4 INFO: See LICENSE and the LGPL-3.0 for licensing details.
2016-03-31T07:11:15.792Z 30945 TID-gnktzwzl4 INFO: Upgrade to Sidekiq Pro for more features and support: http://sidekiq.org
2016-03-31T07:11:15.793Z 30945 TID-gnktzwzl4 INFO: Starting processing, hit Ctrl-C to stop
2016-03-31T07:13:17.062Z 30945 TID-gnku2xt9s OurWorker JID-d40dc832d93f50c00afd2ab4 INFO: start
That wasn't a lot of effort
2016-03-31T07:13:18.065Z 30945 TID-gnku2xt9s OurWorker JID-d40dc832d93f50c00afd2ab4 INFO: done: 1.003 sec
2016-03-31T07:14:08.887Z 30945 TID-gnktztls4 OurWorker JID-33356c6a0c7cd55047d40670 INFO: start
hard! That was o bit of work
2016-03-31T07:14:18.887Z 30945 TID-gnktztls4 OurWorker JID-33356c6a0c7cd55047d40670 INFO: done: 10.0 sec

 

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

https://github.com/javan/whenever

(1)首先安装这个gem

$ gem install whenever

(2)将这个gem添加到Gemfile里

Or with Bundler in your Gemfile.
gem 'whenever', :require => false

(3)运行一下,This will create an initial config/schedule.rb file for you.
$ wheneverize .

(4)编辑定时任务schedule.rb
every 1.day, at: '00:01 am' do
  command "cd /opt/app/ruby/cms/current && RAILS_ENV=#{@environment} bundle exec rails runner script/fetch_video.rb", output: 'log/fetch.log'
end
以上含义是在每天晚上12:01执行脚本,

(5)想以contab的形式查看schedule.rb脚本,执行以下命令
$ whenever
在script里的脚本可以只写功能语句,好处就是与执行时间分离开了,更加达到解偶的效果.

(6)如果想立即执行脚本,也不需要去该fetch_video.rb里的代码,直接运行命令:
RAILS_ENV=production rails runner script/fetch_videos.rb 
本地环境可以省略前面的RAILS_ENV配置,因为默认就是development环境

(7)部署的时候需要注意在  "config/deploy.rb" file加上:
require "whenever/capistrano"
set :whenever_command, "bundle exec whenever"

(8)最后,部署完在服务器上查看一下
$ crontab -l