Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[review] delayed_cron_job を入れ替える rake タスク #33

Merged
merged 4 commits into from
Dec 13, 2023

Conversation

morikiyo
Copy link
Contributor

@morikiyo morikiyo commented Nov 24, 2023

参照: #19

@morikiyo morikiyo changed the title delayed_cron_job を入れ替える rake タスク [review] delayed_cron_job を入れ替える rake タスク Nov 24, 2023
end

def cron_settings
@cron_settings ||= Rails.application.config_for('delayed_cron_jobs')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delayed_cron_jobs.yamlのテンプレートは作成しない?

上記のテンプレートを作成するかどうかの設定化もやった方がよさそう。
そして、READMEにも追加したいね。

まだ検証フェーズということであればあとでやるでもよさそう。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

テンプレートジェネレートした方が良さそう。
追加しておきます!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private

def destroy_cron_jobs!
DelayedCronJobUtility.cron_jobs.find_each do |delayed_job|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この条件だけだと、現在進行系で動いてるジョブのレコードを消すことにならない?と思いました。(ちょうどいい属性があるかは未確認)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

実行中のジョブもキューデータが消されます。消えてもワーカーとしては問題なく動作することを確認してます。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

懸念があるとすると
処理が長引いて、本来はスキップされるはずの次のスケジュールに新しくジョブが追加されて、別のワーカーが部分的に並列実行しちゃうかも

でも、これは opsworks でも起きてるはずなので(そもそもスケジュールスキップされないはず)問題ではなさそう

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ジョブが成功する場合はいいとして、例外などで失敗した場合も問題ないですか?
ジョブが失敗したことを記録する先がなくなって、期待しない動作にならないかなーと。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

考えてなかった!
処理用の別のジョブを産んでくれればよいのにな delayed_cron_job ...

delayed_cron_job → rake task → delayed_job という処理順にしてもらうしかないのかな

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

エラー情報が紛失する?
=> bugsnag に残るはず

リトライを期待してたら困る?
=> 現状 (OpsWorks) の cron はリトライされないからその想定はないはず

Copy link
Contributor Author

@morikiyo morikiyo Dec 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

異常系の実機検証してみた。

cron job でエラーが発生したら jobmon, bugsnag, cloudwatch logs にエラー情報が残る
DB (delayed_jobs) からは deploy でエラー情報が消えてしまうが、↑の3つのエラー情報があるから問題はなさそう。

cron job の処理実行中にデプロイによる refresh タスクが実行されたら
コンテナが停止され
jobmon => running のままで通知が来る
bugsnag => "rake aborted!"
cloudwatch logs => SIGTERM のログあり

[Worker(host:ip-10-1-0-74.ap-northeast-1.compute.internal pid:41)] Job Jobmon::TaskJob [030e6b50-6b26-410c-8287-76f61d6bb786] from DelayedJob(default) with arguments: [{"task"=>"cron:long_process_test", "estimate_time"=>1800, "_aj_ruby2_keywords"=>["task", "estimate_time"]}] (id=728) (queue=default) FAILED with SignalException: SIGTERM
E, [2023-12-01T16:39:25.166443 #41] ERROR -- : 2023-12-01T16:39:25+0900: [Worker(host:ip-10-1-0-74.ap-northeast-1.compute.internal pid:41)] Job Jobmon::TaskJob [030e6b50-6b26-410c-8287-76f61d6bb786] from DelayedJob(default) with arguments: [{"task"=>"cron:long_process_test", "estimate_time"=>1800, "_aj_ruby2_keywords"=>["task", "estimate_time"]}] (id=728) (queue=default) FAILED with SignalException: SIGTERM

@@ -1,4 +1,5 @@
require 'sg_fargate_rails/adjust_cloudfront_headers'
require 'sg_fargate_rails/delayed_cron_job_utility'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delayed_jobが有効な場合のみrequireするほうが良さそう。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ですよね。

defined?(::DelayedCronJob) は、このタイミングではクラスロードの関係か、まだ使えませんでした。
その場合どんな判定処理をするのがよさそうでしょう?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

必要になったタイミングでrequireするほうがエコだし、refresh_delayed_cron_jobsのrakeタスク内でrequireすれば良さそう!


https://github.com/SonicGarden/jobmon_ruby/blob/b8867774137fc5e1ddd58366bdd3aa4c23a9caab/lib/tasks/sidekiq_queue_monitor.rake#L9

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

そうか!ここで require するものと完全に思い込んでました!

Rails.logger.info('[refresh_delayed_cron_jobs] refresh begin...')
SgFargateRails::DelayedCronJobUtility.refresh_cron_jobs!
Rails.logger.info('[refresh_delayed_cron_jobs] refresh end.')
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delayed_jobが有効な場合のみrakeタスクを定義するほうが良さそう。

@morikiyo morikiyo merged commit de86324 into dev Dec 13, 2023
3 checks passed
@morikiyo morikiyo deleted the feat/refresh-delayed-cron branch December 13, 2023 05:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants