Skip to content

πŸ“¨ The ultimate agentic social media scheduling tool πŸ€–

feat: fix workflows without continue as new

Nevo David185044d5004a
diff --git a/apps/orchestrator/src/workflows/autopost.workflow.ts b/apps/orchestrator/src/workflows/autopost.workflow.ts
index 62afe9825..8aa025747 100644
--- a/apps/orchestrator/src/workflows/autopost.workflow.ts
+++ b/apps/orchestrator/src/workflows/autopost.workflow.ts
@@ -1,4 +1,10 @@
-import { proxyActivities, sleep } from '@temporalio/workflow';
+import {
+ continueAsNew,
+ patched,
+ proxyActivities,
+ sleep,
+ workflowInfo,
+} from '@temporalio/workflow';
import { AutopostActivity } from '@gitroom/orchestrator/activities/autopost.activity';
const { autoPost } = proxyActivities<AutopostActivity>({
@@ -18,6 +24,7 @@ export async function autoPostWorkflow({
id: string;
immediately: boolean;
}) {
+ let loops = 0;
while (true) {
try {
if (immediately) {
@@ -26,5 +33,16 @@ export async function autoPostWorkflow({
} catch (err) {}
immediately = true;
await sleep(3600000);
+
+ // Keep the history small, runs started before this patch continue-as-new on their first live loop
+ if (
+ patched('autopost-continue-as-new') &&
+ (++loops >= 24 || workflowInfo().historyLength > 1000)
+ ) {
+ return await continueAsNew<typeof autoPostWorkflow>({
+ id,
+ immediately: true,
+ });
+ }
}
}
diff --git a/apps/orchestrator/src/workflows/missing.post.workflow.ts b/apps/orchestrator/src/workflows/missing.post.workflow.ts
index 17babfd23..427a1b71b 100644
--- a/apps/orchestrator/src/workflows/missing.post.workflow.ts
+++ b/apps/orchestrator/src/workflows/missing.post.workflow.ts
@@ -1,4 +1,10 @@
-import { proxyActivities, sleep } from '@temporalio/workflow';
+import {
+ continueAsNew,
+ patched,
+ proxyActivities,
+ sleep,
+ workflowInfo,
+} from '@temporalio/workflow';
import { PostActivity } from '@gitroom/orchestrator/activities/post.activity';
const { searchForMissingThreeHoursPosts } = proxyActivities<PostActivity>({
@@ -11,9 +17,19 @@ const { searchForMissingThreeHoursPosts } = proxyActivities<PostActivity>({
});
export async function missingPostWorkflow() {
+ let loops = 0;
await searchForMissingThreeHoursPosts();
while (true) {
await sleep('1 hour');
+
+ // Keep the history small, the new run searches immediately so the hourly cadence is kept
+ if (
+ patched('missing-post-continue-as-new') &&
+ (++loops >= 24 || workflowInfo().historyLength > 1000)
+ ) {
+ return await continueAsNew<typeof missingPostWorkflow>();
+ }
+
await searchForMissingThreeHoursPosts();
}
}