fix: keep Temporal workflow when a scheduled post is edited without a time change#1690
fix: keep Temporal workflow when a scheduled post is edited without a time change#1690giladresisi wants to merge 1 commit into
Conversation
… time change Editing a future scheduled post (dashboard or public API) sent type 'schedule', which always terminated and recreated the post's Temporal workflow - even when only content or settings changed and the publish time stayed the same. At 100k+ sleeping workflows that is needless churn, and it diverges from the agent's updatePostTool, which already leaves the workflow alone for non-time changes. Decide it server-side in createPost: capture the post's current publish time before the upsert, and only (re)start the workflow when it's a new post or an existing QUEUE post whose time actually moved. Content and settings are re-read from the DB at publish time, so an unchanged time needs no restart; a genuine time change still restarts it. Done in the service rather than the edit modal so it's authoritative and covers every caller (dashboard, public API), and can't be fooled by client-side date state. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Contribution-checker quality warning Heuristics that flagged:
If this is a genuine contribution, please add detail to your PR description and tighten the diff scope before reviewers look at it. |
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
| content: removeLinks ? stripLinks(updateContent[i]) : updateContent[i], | ||
| })); | ||
|
|
||
| const scheduledDate = |
There was a problem hiding this comment.
Bug: On non-UTC servers, creating a scheduledDate with dayjs().format() and parsing it with dayjs.utc() causes an incorrect time comparison for posts with type: 'now'.
Severity: MEDIUM
Suggested Fix
Ensure timezone consistency. When generating the scheduledDate for type === 'now', use dayjs.utc() to create a UTC-based time string. Replace dayjs().format('YYYY-MM-DDTHH:mm:00') with dayjs.utc().format('YYYY-MM-DDTHH:mm:00') to align the generated time with the UTC-based comparison logic used later.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.
Location: libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts#L903
Potential issue: A timezone mismatch occurs when editing a post with `body.type ===
'now'` on a server not running in the UTC timezone. The code generates a `scheduledDate`
using `dayjs().format()`, which creates a string representing the server's local time
but lacks a timezone offset. This string is later parsed by `dayjs.utc()`, which
incorrectly assumes the time is in UTC. This discrepancy causes the time comparison
against the existing `publishDate` to fail, leading to an unnecessary workflow restart
even when the scheduled time has not been modified.
Did we get this right? 👍 / 👎 to inform future reviews.
Problem
Editing a future scheduled post — via the dashboard or the public API (
POST /public/v1/postswith the existing ids) — senttype: 'schedule', which always terminated and recreated the post's Temporal workflow, even when only the content or settings changed and the publish time stayed the same. At 100k+ sleeping workflows that is needless churn (terminate + start per edit), and it diverged from the agent'supdatePostTool, which already leaves the workflow alone for non-time changes.Fix
Decide it server-side in
PostsService.createPost: capture the post's current publish time before the upsert overwrites it, and only (re)start the workflow when it's a new post or an existingQUEUEpost whose time actually moved. Content and settings are re-read from the DB at publish time, so an unchanged time needs no restart; a genuine time change still restarts it.Done in the service (not the edit modal) so it's authoritative and covers every caller — dashboard and public API — and can't be fooled by client-side date state.
Testing (repro + validate, both surfaces)
Counted Temporal runs for the post before/after an identical time-unchanged edit:
POST /public/v1/posts(same ids, same time)A real date change still recreates the workflow (
TERMINATE_EXISTING, one new run) in both cases. Decision logic also spot-checked against real stored dates (unchanged → skip, moved → restart).🤖 Generated with Claude Code