Rails下的sidekiq/sidetiq

1. sidekiq

    为Rails提供了一个高效的消息队列系统,简单说就是可用于处理异步任务

    sidekiq通过redis保存job与数据,需要先配置redis

gem  'redis'

gem 'sidekiq'

bundle install

    创建config/initializers/sidekiq.rb(sidekiq所依赖的redis位置,必须同时定义sidekiq的server和client) 

    [选填:namespace]

Sidekiq.configure_server do |config|

        config.redis = { url: "redis://#{redis_server}:#{redis_port}/#{redis_db_num}", namespace: redis_namespace }

  end

  Sidekiq.configure_client do |config|

      config.redis = { url: "redis://#{redis_server}:#{redis_port}/#{redis_db_num}", namespace: redis_namespace }

  end

    配置config/sidekiq.yml文件

:concurrency: 2 # 并发数

:pidfile: tmp/pids/sidekiq.pid

:logfile: ./log/sidekiq.log # 输出的日志地址

staging:

  :concurrency: 2

production:

  :concurrency: 2

:queues:

      - async_request

      - default

      - aliyun

2. sidetiq

    作为定时器,配合sidekiq使用

    在app文件夹下新建workers文件夹,代码如下

class xxxWorker

      include Sidekiq::Worker

      include Sidetiq::Schedulable

      recurrence do

        hourly

      end

      def perform

            if Time.now.hour == 20 # 每晚八点

        end

    end

end

参考资料

https://www.jianshu.com/p/7ea473097023

你可能感兴趣的:(Rails下的sidekiq/sidetiq)