使用延迟作业时出现Postgres错误

时间:2015-08-25 22:05:25

标签: ruby-on-rails postgresql delayed-job

我一直在尝试创建一个延迟作业,用于检查Etsy上的产品是否仍然可用。每当我使用 bin / delayed_job start 运行它时会出现错误(尽管由于某种原因它可以正常运行 rake jobs:work )。我得到的错误是:

Job CheckInventoryStatus (id=1174) FAILED (0 prior attempts) with ActiveRecord::StatementInvalid: PG::ConnectionBad: PQconsumeInput() server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
            FROM pg_attribute a LEFT JOIN pg_attrdef d
              ON a.attrelid = d.adrelid AND a.attnum = d.adnum
           WHERE a.attrelid = '"spree_stock_items"'::regclass
             AND a.attnum > 0 AND NOT a.attisdropped
           ORDER BY a.attnum

我认为违规行是首先尝试进行SQL查找的地方:

etsy_location = variant.stock_locations.where(vendor_type: 2).first

完整的代码如下。

CheckInventoryStatus = Struct.new(:empty) do
  def perform
    Delayed::Backend::ActiveRecord::Job.after_fork
    Spree::Variant.where('spree_variants.inventory_link != ?',"").each do |variant|
      begin
        page = Nokogiri::HTML(open(variant.inventory_link))
        etsy_location = variant.stock_locations.where(vendor_type: 2).first
        unless etsy_location.nil?
          if !page.at_xpath('//meta[@itemprop="availability"]/@content').nil?
            if etsy_location.count_on_hand(variant) <= 10
              etsy_location.restock(variant, 10-etsy_location.count_on_hand(variant))
            end
          else
            etsy_location.unstock(variant, etsy_location.count_on_hand(variant))
          end
        end
      rescue => error
        Delayed::Worker.logger.info "Error for "+variant.inventory_link
        Delayed::Worker.logger.info error.backtrace
      end
    end 
  end
  ...
end

0 个答案:

没有答案
相关问题