2
1

More than 5 years have passed since last update.

Rails抜きで script/delayed_job (w/ ActiveRecord) を使う

Last updated at Posted at 2013-04-04

とりあえず起動するようになったのでやったことメモ。

Delayed::Worker 初期化時に何カ所か Rails モジュールを参照してるので、そこを上手いことだましてやる必要がある。ちょうど config/environment.rb をrequire しているので、そこに最低限のメソッドを定義したダミーを放り込んでやる。例えばこんなの。

config/environment.rb
module Rails
    # backend が ActiveRecord の時のみ必要? (mongoid の場合は未検証)
    def self.env
       ENV["RAILS_ENV"]
    end

    def self.root
       File.join(File.dirname(__FILE__), "..")
    end
end

さらに ActiveRecord の接続情報は ENV["DATABASE_URL"] に url で設定するか hash 形式なら ActiveRecord::Base.configurations に持たせておく必要がある模様。こうしないと daemonize後の再接続がうまくいかず、無言で死ぬ。

config/environment.rb
require 'yaml'

config = YAML.load_file(File.join(Rails.root, "config", "database.yml"))
ActiveRecord::Base.configurations = config

あとはデフォルトのログの吐き場所 log/ と pid ファイル置き場 tmp/pids/ を掘るだけ。普通に script/delayed_job start すれば ワーカーが起動する。

ここまでやって飽きたのでちゃんと動いているかどうかの確認は棚上げ。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1