Skip to content

Conversation

arafubeatbox
Copy link
Contributor

@arafubeatbox arafubeatbox commented Oct 1, 2025

What

bulk export job が expire した際、expire した旨の通知が届かないことがある事象を修正。

原因

実行を開始した readable stream をメモリ上に記録しておき、expire した際はそれを BulkExportJobExpiredError で destroy し、そのエラーを handleError で処理して notifyExportResultAndCleanUp に通知を任せる方針になっていた。

これだと、readable stream の処理が軽い場合、次の expire した後の次の cleanup cron 実行までに完了していることがあり、通知がされない。

対応内容

そもそも通知が stream の存在に依存して行われていることがよくないため、

  • cleanup cron 内で明示的に通知を出すように修正
  • stream を export job cleanup で destroy する際に通知と cleanup が二重に実行されないように変更

また、readable stream のみが cleanup されており、後段の writable stream が cleanup されていなかったため、writable stream もメモリ上に保管しておき、cleanup 対象とする。

task

https://redmine.weseek.co.jp/issues/172103

@arafubeatbox arafubeatbox changed the title fix bulk export cleanup and notification on expire fix: Bulk export cleanup and notification occasionally not working on job expire Oct 1, 2025
pageBulkExportJobCronService,
),
const cleanUp = async (job: PageBulkExportJobDocument) => {
await pageBulkExportJobCronService?.notifyExportResultAndCleanUp(
Copy link
Contributor Author

@arafubeatbox arafubeatbox Oct 1, 2025

Choose a reason for hiding this comment

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

cleanUpAndDeleteBulkExportJobs ではなく notifyExportResultAndCleanUp を実行し、明示的に期限切れの通知を実行。

// The key is the id of a PageBulkExportJob, value is array of streams.
private streamInExecutionMemo: {
[key: string]: Readable;
[key: string]: (Readable | Writable)[];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

readable と writable stream 両方をメモリ上に記録して置けるように変更。

// prevent overlapping cleanup
if (!(err instanceof BulkExportJobStreamDestroyedByCleanupError)) {
this.handleError(err, pageBulkExportJob);
}
Copy link
Contributor Author

@arafubeatbox arafubeatbox Oct 1, 2025

Choose a reason for hiding this comment

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

元々 cleanup で投げた BulkExportJobRestartedError, BulkExportJobExpiredError もここでキャッチし、handleError を実行することによって通知と、重複した cleanup 処理を実行していた。
今回 cleanup の destroy で生じたエラーは処理しないように変更。

(重複した cleanup を実行していたのは、destroy によって stream の処理が終わる前に cleanup が完了してしまう場合をカバーするためだった。これをカバーするために cleanup 関数側で stream close を待つように変更 1d807d0)

*/
async cleanUpExportJobResources(
pageBulkExportJob: PageBulkExportJobDocument,
restarted = false,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

restarted かどうかは stream のエラーを handleError によって処理するときの場合分けに必要だったが、cleanup 時は handleError しなくなったため、不要となった。

@arafubeatbox arafubeatbox marked this pull request as ready for review October 3, 2025 00:24
@mergify mergify bot added the queued label Oct 3, 2025
mergify bot added a commit that referenced this pull request Oct 3, 2025
mergify bot added a commit that referenced this pull request Oct 3, 2025
@mergify mergify bot merged commit 15e0ecd into master Oct 3, 2025
33 checks passed
@mergify mergify bot deleted the fix/172102-172103-bulk-export-cleanup-notif-on-expire branch October 3, 2025 07:37
@mergify mergify bot removed the queued label Oct 3, 2025
@github-actions github-actions bot mentioned this pull request Oct 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants