【问题标题】:How to properly use Sidekiq to process background tasks in RailsRails中如何正确使用Sidekiq处理后台任务
【发布时间】:2018-04-04 23:46:59
【问题描述】:

因此,我使用 https://github.com/Shopify/shopify_app 生成了一个 rails 应用程序 - 并且大多数情况下,该应用程序都按预期工作 - 它的目标是从外部库存管理 API 获取产品数量,然后更新变体数量Shopify 使用该库存管理系统中的最新数量。

我的问题是,对外部 API 的初始 POST 请求会响应大量产品 - 这有时需要 15 秒以上。除此之外,我的应用程序的另一部分随后会接收此响应,并且对于响应中也存在于 Shopify 中的每个产品,它将向 Shopify 发出 PUT 请求以更新变体数量。与初始请求一样,这也需要 10-15 秒以上的时间。

我的问题是我在 Heroku 上托管应用程序,结果我达到了他们 30 秒的请求超时限制。因此,我需要使用后台工作人员将上述请求中的至少一个(可能两者)偏移到工作队列。我使用了广受推荐的 Sidekiq gem - https://github.com/mperham/sidekiq - 很容易设置。

我的问题是我不知道如何从完成的 Sidekiq 工人作业中获取结果,然后在 Controller 中再次使用它 - 我也不知道这是否是最佳实践(我有点Rails/App 开发新手)。

我已经包含了我的控制器(在将其分解为工作人员之前),该控制器当前运行下面的应用程序 - 我想我只需要一些建议 - 我这样做是否正确 - 如果其中一些逻辑位于模型中,并且如果是这样,该模型将如何与控制器通信,然后 Sidekiq 将如何适应所有这些。

感谢任何建议或帮助,谢谢。

class StockManagementController < ShopifyApp::AuthenticatedController

require 'uri'
require 'net/http'
require 'json'
require 'nokogiri'
require 'open-uri'
require 'rexml/document'

def new
    @token = StockManagementController.new
end

def get_token

    url = URI('https://external.api.endpoint/api/v1/AuthToken')
    http = Net::HTTP.new(url.host, url.port)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE

    @HEROKU_ENV_USERNAME = ENV['HEROKU_ENV_USERNAME']
    @HEROKU_ENV_PASSWORD = ENV['HEROKU_ENV_PASSWORD']

    request = Net::HTTP::Post.new(url)
    request['content-type'] = 'application/x-www-form-urlencoded'
    request['cache-control'] = 'no-cache'
    request.body = 'username=' + @HEROKU_ENV_USERNAME + '&password=' + @HEROKU_ENV_PASSWORD + '&grant_type=password'
    response = http.request(request)
    responseJSON = JSON.parse(response.read_body)
    session[:accessToken] = responseJSON['access_token']

    if session[:accessToken]
        flash[:notice] = 'StockManagement token generation was successful.'
        redirect_to '/StockManagement/product_quantity'
    else
        flash[:alert] = 'StockManagement token generation was unsuccessful.'
    end
end

def product_quantity

    REXML::Document.entity_expansion_text_limit = 1_000_000

    @theToken = session[:accessToken]

    if @theToken

        url = URI('https://external.api.endpoint/api/v1/ProductQuantity')
        http = Net::HTTP.new(url.host, url.port)
        http.use_ssl = true
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE

        request = Net::HTTP::Post.new(url)
        request['authorization'] = 'bearer ' + @theToken + ''
        request['content-type'] = 'application/xml'
        request['cache-control'] = 'no-cache'

        response = http.request(request)
        responseBody = response.read_body
        finalResponse = Hash.from_xml(responseBody).to_json
        resultQuantity = JSON.parse finalResponse

        @connectionType = resultQuantity['AutomatorResponse']['Type']
        @successResponse = resultQuantity['AutomatorResponse']['Success']
        @errorResponse = resultQuantity['AutomatorResponse']['ErrorMsg']

        productQuantityResponse = resultQuantity['AutomatorResponse']['ResponseString']
        xmlResponse = Hash.from_xml(productQuantityResponse).to_json
        jsonResponse = JSON.parse xmlResponse

        @fullResponse = jsonResponse['StockManagement']['Company']['InventoryQuantitiesByLocation']['InventoryQuantity']

        # This hash is used to store the final list of items that we need in order to display the item's we've synced, and to show the number of items we've sycned successfully.
        @finalList = Hash.new

        # This array is used to contain the available products - this is used later on as a way of only rendering
        @availableProducts = Array.new

        # Here we get all of the variant data from Shopify.
        @variants = ShopifyAPI::Variant.find(:all, params: {})

        # For each peace of variant data, we push all of the available SKUs in the store to the @availableProducts Array for use later
        @variants.each do |variant|
            @availableProducts << variant.sku
        end

        #Our final list of products which will contain details from both the Stock Management company and Shopify - we will use this list to run api calls against each item
        @finalProductList = Array.new

        puts "Final product list has #{@fullResponse.length} items."
        puts @fullResponse.inspect

        # We look through every item in the response from Company
        @fullResponse.each_with_index do |p, index|

            # We get the Quantity and Product Code
            @productQTY = p["QtyOnHand"].to_f.round
            @productCode = p["Code"].upcase

            # If the product code is found in the list of available products in the Shopify store...
            if @availableProducts.include? @productCode
                @variants.each do |variant|
                    if @productCode === variant.sku
                        if @productQTY != 0
                            @finalProductList << {
                                "sku" => variant.sku,
                                "inventory_quantity" => variant.inventory_quantity,
                                "old_inventory_quantity" => variant.old_inventory_quantity,
                                "id" => variant.id,
                                "company_sku" => @productCode,
                                "company_qty" => @productQTY
                            }
                        end
                    end
                end
            end
        end

        # If we get a successful response from StockManagement, proceed...
        if @finalProductList
            flash[:notice] = 'StockManagement product quantity check was successful.'

            puts "Final product list has #{@finalProductList.length} items."
            puts @finalProductList

            @finalProductList.each do |item|

                @productSKU = item["sku"]
                @productInventoryQuantity = item["inventory_quantity"]
                @productOldInventoryQuantity = item["old_inventory_quantity"]
                @productID = item["id"]
                @companySKU = item["company_sku"]
                @companyQTY = item["company_qty"]

                url = URI("https://example.myshopify.com/admin/variants/#{@productID}.json")

                http = Net::HTTP.new(url.host, url.port)
                http.use_ssl = true
                http.verify_mode = OpenSSL::SSL::VERIFY_NONE
                request = Net::HTTP::Put.new(url)
                request["content-type"] = 'application/json'
                request["authorization"] = 'Basic KJSHDFKJHSDFKJHSDFKJHSDFKJHSDFKJHSDFKJHSDFKJHSDFKJHSDFKJHSDF'
                request["cache-control"] = 'no-cache'
                request.body = "{\n\t\"variant\": {\n\t\t\"id\": #{@productID},\n\t\t\"inventory_quantity\": #{@companyQTY},\n\t\t\"old_inventory_quantity\": #{@productOldInventoryQuantity}\n\t}\n}"

                # This is the line that actually runs the put request to update the quantity.
                response = http.request(request)

                # Finally, we populate the finalList has with response information.
                @finalList[@companySKU] = ["","You had #{@productOldInventoryQuantity} in stock, now you have #{@companyQTY} in stock."]

            end

        else
            # If the overall sync failed, we flash an alert.
            flash[:alert] = 'Quantity synchronisation was unsuccessful.'

        end

        # Lastly we get the final number of items that were synchronised.
        @synchronisedItems = @finalList.length

        # We flash this notification, letting the user known how many products were successfully synchronised.
        flash[:notice] = "#{@synchronisedItems} product quantities were synchronised successfully."

        # We then pretty print this to the console for debugging purposes.
        pp @finalList

    else

        flash[:alert] = @errorResponse

    end
end
end

【问题讨论】:

    标签: ruby-on-rails heroku sidekiq shopify-app


    【解决方案1】:

    首先,您的product_quantity 方法太长了。你应该把它分成更小的部分。 2、http.verify_mode = OpenSSL::SSL::VERIFY_NONE 不应该在生产中使用。您提供的示例以及您的问题太复杂了,因此难以回答。听起来您需要对设计模式有基本的了解,这不是一个特定的 ruby​​ 问题。

    如果您的应用需要在控制器内进行实时 API 调用,这是一个糟糕的设计。您不想让任何类型的请求最多等待超过几秒钟。您应该首先考虑为什么需要提出这些请求。如果是您需要快速访问的数据,您应该编写后台作业以按计划抓取数据并将其存储在您自己的数据库中。

    如果您应用的用户发出需要等待 API 响应的请求,您可以编写一个工作程序来处理获取 API 数据并最终可能使用actioncable 向用户的浏览器发送响应。

    对于您的常量定义,您可能应该在初始化程序中执行此操作,您将保留在 my_app_root/config/initializers/constants.rb 中,该初始化程序会在运行时加载到您的应用程序中。您可以使用 te ENV[] 语法在需要的地方调用它们,但如果您更喜欢更简单的常量,请删除 @,因为 ruby​​ 中的命名约定是实例对象。

    #app_root/config/initializers/constants.rb
    HEROKU_ENV_USERNAME = ENV['HEROKU_ENV_USERNAME']
    HEROKU_ENV_PASSWORD = ENV['HEROKU_ENV_PASSWORD']
    

    【讨论】:

    • 嗨@lacostenycoder - 感谢您的反馈 - 我绝对打算编写工作人员来处理获取 API 数据 - 我想我的问题的症结是 - 如果我写工作人员 - 我将如何从工作人员的响应中获取数据返回到用户的浏览器 - 您是在暗示这可以通过 actioncable 实现吗?编辑:详细说明-我计划添加一个工人来获得初始令牌。然后另一个工人获取产品数量,另一个工人更新 Shopify 中的数量。
    • 是的,这可能是 Rails 最简单的方法。否则,您将不得不对作业状态进行某种 javascript 轮询,并在作业完成时执行某些操作。
    • 只是想对 actioncable 和设计模式建议表示感谢 - 将应用程序分解为模型,从控制器中获取逻辑,并使用 sidekiq 工作人员与 actioncable 消息传递来实现我想要的.
    • @Marko 很高兴这对您有所帮助。如果您还没有,您可能想看看使用 OAUTH 进行 api 身份验证,而不是自己滚动。 help.shopify.com/api/getting-started/authentication/oauth
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 2023-01-17
    相关资源
    最近更新 更多