5

我正在合并我的 Rails 应用程序的一些更改,其中包括添加延迟作业 gem(看起来很棒)。为了测试它,我:

  • 跑了rails g delayed_job
  • ran rake db:migrate,它创建了delayed_jobs 表
  • 重新启动rails server(以防万一)
  • 重新启动 postgresql 服务(以防万一)
  • 跑了scripts/delayed_job start
  • 执行了一些任务以累积延迟的工作

但是什么也没发生:/我的工作在延迟工作表中,但locked_at为空(原样last_error)。

所以我尝试rake jobs:work了,它给出了一个奇怪的 Postgres 错误:
PGError: ERROR: SELECT FOR UPDATE/SHARE is not allowed in subqueries 和一个跟踪:

$ rake jobs:work
[Worker(host:robert-vaio pid:21217)] Starting job worker
rake aborted!
PGError: ERROR:  SELECT FOR UPDATE/SHARE is not allowed in subqueries
: UPDATE "delayed_jobs" SET locked_at = '2013-02-13 19:53:46.803085', locked_by = 'host:robert-vaio pid:21217' WHERE id IN (SELECT  id FROM "delayed_jobs"  WHERE ((run_at <= '2013-02-13 19:53:46.792619' AND (locked_at IS NULL OR locked_at < '2013-02-13 15:53:46.792639') OR locked_by = 'host:robert-vaio pid:21217') AND failed_at IS NULL) ORDER BY priority ASC, run_at ASC LIMIT 1 FOR UPDATE) RETURNING *
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:1153:in `async_exec'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:1153:in `exec_no_cache'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:662:in `block in exec_query'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-3.2.12/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:661:in `exec_query'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:1248:in `select'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/database_statements.rb:18:in `select_all'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/querying.rb:38:in `block in find_by_sql'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/explain.rb:33:in `logging_query_plan'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-3.2.12/lib/active_record/querying.rb:37:in `find_by_sql'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job_active_record-0.4.1/lib/delayed/backend/active_record.rb:63:in `reserve'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/worker.rb:258:in `reserve_and_run_one_job'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/worker.rb:187:in `block in work_off'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/worker.rb:186:in `times'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/worker.rb:186:in `work_off'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/worker.rb:151:in `block (4 levels) in start'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/worker.rb:150:in `block (3 levels) in start'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/lifecycle.rb:60:in `call'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/lifecycle.rb:60:in `block in initialize'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/lifecycle.rb:65:in `call'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/lifecycle.rb:65:in `execute'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/lifecycle.rb:38:in `run_callbacks'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/worker.rb:149:in `block (2 levels) in start'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/worker.rb:148:in `loop'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/worker.rb:148:in `block in start'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/plugins/clear_locks.rb:7:in `call'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/plugins/clear_locks.rb:7:in `block (2 levels) in <class:ClearLocks>'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/lifecycle.rb:78:in `call'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/lifecycle.rb:78:in `block (2 levels) in add'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/lifecycle.rb:60:in `call'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/lifecycle.rb:60:in `block in initialize'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/lifecycle.rb:78:in `call'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/lifecycle.rb:78:in `block in add'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/lifecycle.rb:65:in `call'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/lifecycle.rb:65:in `execute'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/lifecycle.rb:38:in `run_callbacks'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/worker.rb:147:in `start'
/home/robert/.rvm/gems/ruby-1.9.3-p327/gems/delayed_job-3.0.5/lib/delayed/tasks.rb:9:in `block (2 levels) in <top (required)>'
Tasks: TOP => jobs:work
(See full trace by running task with --trace)

我不知道出了什么问题——更令人惊讶的是,我能找到其他人问这个问题......!

谢谢你的帮助!

4

4 回答 4

6

我有同样的问题。我通过将延迟作业活动记录的 Gemfile 锁定到版本 0.3.3 解决了这个问题:

宝石'delayed_job_active_record','0.3.3'

不工作的 gem 是 0.4.1 版,于 2012 年 2 月 12 日发布。

于 2013-02-20T18:42:26.210 回答
5

更新 postgres 也为我解决了这个问题——这似乎比锁定回旧版本更可取。并不是说这是我的答案,但是当 rmosolgo 回答了他自己的问题时,看起来这个问题确实可以正确地进入“已接受的答案”状态。

于 2013-02-21T18:37:50.470 回答
2

这是延迟作业的已知问题,请参阅https://github.com/collectiveidea/delayed_job_active_record/issues/33

有一个delayed_job_active_record 的分支可以在postgres 8.4 中正常工作,并且与rails 4.x 兼容

gem 'delayed_job_active_record', :git => 'git@github.com:panter/delayed_job_active_record.git'

它通过使用没有失败查询的 postgres sql 调用来解决问题。

于 2014-04-25T08:35:28.427 回答
1

我尝试使用 bt Neil Woods 提供的解决方案,但它显示了一些身份验证问题,然后我尝试在下面添加一些相同的更改。

gem 'delayed_job_active_record', :git => 'git://github.com/panterch/delayed_job_active_record.git'

这个解决了,如果有人遇到同样的问题,请尝试这个。

谢谢。

于 2015-08-11T05:59:08.623 回答