basic use of sidekiq

参考页面

https://github.com/mperham/sidekiq

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

强烈推荐这个视频

https://www.youtube.com/watch?v=bfPb1zD91Rg&index=1&list=PLjeHh2LSCFrWGT5uVjUuFKAcrcj5kSai1

$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
时间: 2024-11-05 13:04:06

basic use of sidekiq的相关文章

basic use of sidekiq (2)

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 's

[C++] Variables and Basic Types

Getting Started compile C++ program source $ g++ -o prog grog1.cc run C++ program $ ./prog The library, iostream, define four IO object: cin, cout, cerr, clog. std::cout << "hello world" << std::endl; The result of the output operato

Click Models for Web Search(1) - Basic Click Models

这篇文章主要是介绍一些基本的click model,这些不同的click model对用户与搜索结果页的交互行为进行不同的假设. 为了定义一个model,我们需要描述出observed variables,hidden variables,以及它们之间的关联,以及它们对model parameters的依赖关系.当我们获取了model parameters之后,我们便可以进行CTR 预估,或者计算数据的最大似然估计. 1. RANDOM CLICK MODEL (RCM) 这是最简单的一个mod

LeetCode OJ:Basic Calculator(基础计算器)

Implement a basic calculator to evaluate a simple expression string. The expression string may contain open ( and closing parentheses ), the plus + or minus sign -, non-negative integers and empty spaces . You may assume that the given expression is

Introduction and Basic concepts

1 Network Edge The device such as computers and mobiles connect to the Internet. So they are referred as end systems(who run the application programs) sitting at the edge of the Internet. And we use host and end system interchangeably, that is host=e

Visual Basic快捷教程——流程控制

美籍匈牙利数学家冯·诺依曼于1946年提出"程序存储,顺序执行"的观念,为现代计算机奠定了重要基础.这一观点认为:应该把程序本身当作数据来对待,程序和该程序处理的数据用同样的方式储存.电子计算机的数制宜采用二进制:计算机应该按照程序顺序执行.所以现在开发人员在编写的程序代码时,其实都隐含地认为计算机将来会一行一行按顺序来执行这些指令.但是在顺序执行的过程中,有时我们希望计算机根据条件判断来选择性地执行(或者不执行)一些代码--这时就需要用到选择结构(或称分支结构).另外一些时候,我们希

Nginx下配置Http Basic Auth保护目录

nginx basic auth指令 语法:     auth_basic string | off;默认值:     auth_basic off;配置段:     http, server, location, limit_except 默认表示不开启认证,后面如果跟上字符,这些字符会在弹窗中显示. 语法:     auth_basic_user_file file;默认值:     -配置段:     http, server, location, limit_except 1. 下载这个

NHibernate官方文档中文版--基础ORM(Basic O/R Mapping)

映射声明 对象/关系映射在XML文件中配置.mapping文件这样设计是为了使它可读性强并且可修改.mapping语言是以对象为中心,意味着mapping是围绕着持久化类声明来建立的,而不是围绕数据表. 要注意的是,尽管很多NHibernate使用者选择手动定义XML文件,但是仍然有很多工具可以用来生成mapping文件,包括NHibernate.Mapping.Attributes 库和各种各样基于模板的代码生成工具(CodeSmith, MyGeneration). 让我们用一个mappin

QML学习【一】Basic Types

  QML入门教程(1) QML是什么? QML是一种描述性的脚本语言,文件格式以.qml结尾.语法格式非常像CSS(参考后文具体例子),但又支持javacript形式的编程控制.它结合了QtDesigner UI和QtScript的优点.QtDesigner可以设计出.ui界面文件,但是不支持和Qt原生C++代码的交互.QtScript可以和Qt原生代码进行交互,但是有一个缺点,如果要在脚本中创建一个继承于QObject的图形对象非常不方便,只能在Qt代码中创建图形对象,然后从 QtScrip