LoginSignup
6
0

More than 3 years have passed since last update.

goworkerがCPU使用率100%になってしまう問題

Last updated at Posted at 2019-06-14

TL; DR

  • goworker.WorkerSettings.Intervalの値は無効です
  • goworker.WorkerSettings.IntervalFloatを設定しましょう
  • IntervalFloatの単位はです

goworkerについて

  • https://github.com/benmanns/goworker
  • Goでバックグランドジョブをキューに積みながら実行できるライブラリ

    • サーバーでレスポンスだけ返して処理は非同期実行したいときなどに役立つ
  • RubyのResque互換らしい

今回発生した問題

  • 公式のGetting Startedにしたがってプログラムを実行すると、goworkerのCPU使用率が100%で高止まりすることがある
  • 既知の問題らしくgoworker本家にも2016年にissueが立っている

原因

  • goworkerにはワーカーの設定を記述するためのgoworker.WorkerSettingsという構造体が定義されている
  • このうち、Jobのpolling間隔を指定するWorkerSettings.Intervalの値がIntervalFloatの値で常に上書きされている
  • そのため、IntervalFloatの指定がないとInterval=0となり、polling関数で遅延無しの無限ループが発生するため、CPU使用率が100%になる

参考:https://github.com/benmanns/goworker/blob/5c718f01cbd12ca8f3bd93bdcbb154264e3d89c0/flags.go#L140

解決法

  • goworker.WorkerSettings.IntervalFloatにpollingの間隔を指定してやれば良い。
  • IntervalFloatの単位は

Before

https://github.com/benmanns/goworker#getting-started から引用

worker.go
func init() {
    settings := goworker.WorkerSettings{
        URI:            "redis://localhost:6379/",
        Connections:    100,
        Queues:         []string{"myqueue", "delimited", "queues"},
        UseNumber:      true,
        ExitOnComplete: false,
        Concurrency:    2,
        Namespace:      "resque:",
        Interval:       5.0,
    }
    goworker.SetSettings(settings)
    goworker.Register("MyClass", myFunc)
}

After

worker.go
func init() {
    settings := goworker.WorkerSettings{
        URI:            "redis://localhost:6379/",
        Connections:    100,
        Queues:         []string{"myqueue", "delimited", "queues"},
        UseNumber:      true,
        ExitOnComplete: false,
        Concurrency:    2,
        Namespace:      "resque:",
-       Interval:       5.0,
+       IntervalFloat:  5.0, // 5.0 sec
    }
    goworker.SetSettings(settings)
    goworker.Register("MyClass", myFunc)
}
6
0
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
6
0