ruby-on-rails Rails Runner或Rake任务无法触发活动作业作业

kkih6yb8  于 2023-01-22  发布在  Ruby
关注(0)|答案(1)|浏览(131)

我已经设置了一个运行在Docker中的Rails7项目。使用wherewhy gem(使用cron),我尝试执行
1.一项铁轨赛跑任务
1.耙式任务
两者应做同样的事情:调用类方法WebpageChangeCheck.check_all,类方法WebpageChangeCheck.check_all本身调用模型的方法,然后创建一个活动作业作业,但在创建作业的最后一步都失败了:

app/cron_jobs/webpage_change_check.rb:

class WebpageChangeCheck

  def self.check_all
    Webpage.all.each do |page|
      if page.checking_active
        page.check_for_change
      end
    end
  end

end


app/models/webpage.rb:

def check_for_change
  self.update(counter: self.counter += 1)
  UpdateOffersHashJob.perform_later(self)
end



update_offers_hash_job.rb:

class UpdateOffersHashJob < ApplicationJob
  queue_as :default

  require 'nokogiri'
  require 'open-uri'
  require 'net/http'

  after_perform do |job|
    compare_hashes(job.arguments.first)
  end

  def perform(page)
    page.update(offers_hash_old: page.offers_hash_new)
    all_offers = ""
    doc = Nokogiri::HTML(URI.open(page.url))
    doc.css(page.selector).each do |offer|
      all_offers += offer.to_s
    end
    page.update(offers_hash_new: all_offers.delete(" \t\r\n\ "))
  end

 private
  def compare_hashes(page)
    ...
  end
end

有效措施:

  • 从rails控制台调用相同的类方法会创建预期的活动作业。
irb(main):002:0> WebpageChangeCheck.check_all
  Webpage Load (1.2ms)  SELECT "webpages".* FROM "webpages"
  Webpage Update All (3.9ms)  UPDATE "webpages" SET "counter" = COALESCE("counter", 0) + $1 WHERE "webpages"."id" = $2  [["counter", 1], ["id", 1]]
Enqueued UpdateOffersHashJob (Job ID: 707e164d-b8b9-407b-aa35-4b23c37b4f07) to Async(default) with arguments: #<GlobalID:0x00007f2e0b7c4878 @uri=#<URI::GID gid://my_rails_app/Webpage/1>>
=> 
[#<Webpage:0x00007f2e0b7bded8
  id: 1,
  title: "example.com",
  url: "https://www.example.com",
  user_id: 1,
  created_at: Wed, 18 Jan 2023 14:22:51.904097000 CET +01:00,
  updated_at: Fri, 20 Jan 2023 00:12:02.749748000 CET +01:00,
  interval: 1,
  checking_active: true,
  selector: ".headline_content",
  counter: 386>]
  Webpage Load (0.6ms)  SELECT "webpages".* FROM "webpages" WHERE "webpages"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Performing UpdateOffersHashJob (Job ID: 707e164d-b8b9-407b-aa35-4b23c37b4f07) from Async(default) enqueued at 2023-01-20T07:26:02Z with arguments: #<GlobalID:0x00007f2e092e1088 @uri=#<URI::GID gid://my_rails_app/Webpage/1>>
  TRANSACTION (0.2ms)  BEGIN
User Load (0.8ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
irb(main):003:0>   TRANSACTION (1.0ms)  COMMIT
  TRANSACTION (0.4ms)  BEGIN
  Webpage Load (0.6ms)  SELECT "webpages".* FROM "webpages" WHERE "webpages"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Sim Create (1.4ms)  INSERT INTO "sims" ("to", "time", "api_response", "success", "webpage_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["to", 49123456789], ["time", nil], ["api_response", nil], ["success", nil], ["webpage_id", 1], ["created_at", "2023-01-20 07:26:03.108189"], ["updated_at", "2023-01-20 07:26:03.108189"]]
  TRANSACTION (3.5ms)  COMMIT
  TRANSACTION (0.4ms)  BEGIN
  Sim Update (1.0ms)  UPDATE "sims" SET "time" = $1, "api_response" = $2, "success" = $3, "updated_at" = $4 WHERE "sims"."id" = $5  [["time", "Fri, 20.01.23 - 08h26 03s"], ["api_response", "100\nVerbucht: 0\nPreis: 0.075\nGuthaben: 0.35\nText: ALERT\nSMS-Typ: direct\nFlash SMS: false\nEncoding: gsm\nGSM0338: true\nDebug: true"], ["success", true], ["updated_at", "2023-01-20 07:26:03.295980"], ["id", 22]]
  TRANSACTION (3.8ms)  COMMIT
Performed UpdateOffersHashJob (Job ID: 707e164d-b8b9-407b-aa35-4b23c37b4f07) from Async(default) in 585.89ms

我从bash(从rake或rails runner)中看到,活动作业已创建,但它不会执行:

root@f77855c949a8:/opt/app# rake debug check_all_pages
  Webpage Load (1.5ms)  SELECT "webpages".* FROM "webpages"
  ↳ app/cron_jobs/webpage_change_check.rb:4:in `check_all'
  Webpage Update All (2.9ms)  UPDATE "webpages" SET "counter" = COALESCE("counter", 0) + $1 WHERE "webpages"."id" = $2  [["counter", 1], ["id", 1]]
  ↳ app/models/webpage.rb:9:in `check_for_change'
[ActiveJob] Enqueued UpdateOffersHashJob (Job ID: f4495cb8-868f-4ed5-9f03-7f4407b5efa4) to Async(default) with arguments: #<GlobalID:0x00007fd55771ece0 @uri=#<URI::GID gid://my_rails_app/Webpage/1>>
root@f77855c949a8:/opt/app#

这里你也可以使用rake任务:

my_rails_app/lib/tasks/checker_task.rake:

  desc "checks all Webpages for changes. Called from cronjob."
  task check_all_pages: :environment do
    WebpageChangeCheck.check_all
  end

结论:

  • 看起来环境加载正确,包括环境变量。我可以访问我的类和模型。
  • 然而,似乎存在阻止工作被执行的差异。
  • 问题的一部分可能是没有足够的日志来调试它,所以这也可以改进!

有什么主意吗?多谢!

s5a0g9ez

s5a0g9ez1#

您需要在其他进程中处理作业队列,例如

rake jobs:work

相关问题