我有 Sidekiq 作業人員SyncProductsWorker,它在控制器操作中呼叫:
class ProductsController < BaseApiController
def fetch_all
::Imports::FetchAllProductsWorker.perform_async
head :ok
end
end
并且 Sidekiq 作業與里面的調度作業FetchAllProductsJob具有相同的代碼SyncProductsWorkersidekiq.yml
sidekiq.yml
:queues:
- imports_fetch_all
:dynamic: true
:schedule:
imports_fetch_all_job:
cron: '0 0 * * *' # at midnight
class: Imports::FetchAllProductsJob
queue: imports_fetch_all
所以基本上兩個類,工人和作業看起來幾乎一樣:
# worker
module Imports
class FetchAllProductsWorker
include Sidekiq::Worker
sidekiq_options queue: 'imports_fetch_all'
def perform
puts 'test'
end
end
end
# schedule job
module Imports
class FetchAllProductsJob < ApplicationJob
sidekiq_options queue: 'imports_fetch_all'
def perform
puts 'test'
end
end
end
如何避免代碼重復沒有兩個檔案具有相同的代碼?
uj5u.com熱心網友回復:
沒有理由直接在一個地方與 Sidekiq 對話,而在另一個地方與 ActiveJob 對話。這兩個地方都可以直接與 ActiveJob 作業對話。洗掉您的 Sidekiq 作業人員,并將您對作業人員的呼叫替換為對作業的perform_async呼叫perform_later:
def fetch_all
::Imports::FetchAllProductsJob.perform_later
head :ok
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/489076.html
