Skip to content

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

feat: streak load

Nevo David273c7b504cb6
diff --git a/apps/orchestrator/src/activities/email.activity.ts b/apps/orchestrator/src/activities/email.activity.ts
index 8ecfc236e..377c2fe30 100644
--- a/apps/orchestrator/src/activities/email.activity.ts
+++ b/apps/orchestrator/src/activities/email.activity.ts
@@ -30,4 +30,9 @@ export class EmailActivity {
async setStreak(organizationId: string, type: 'start' | 'end') {
return this._organizationService.setStreak(organizationId, type);
}
+
+ @ActivityMethod()
+ async getLastPublishDate(organizationId: string) {
+ return this._organizationService.getLastPublishDate(organizationId);
+ }
}
diff --git a/apps/orchestrator/src/activities/post.activity.ts b/apps/orchestrator/src/activities/post.activity.ts
index ace280485..419f61e0d 100644
--- a/apps/orchestrator/src/activities/post.activity.ts
+++ b/apps/orchestrator/src/activities/post.activity.ts
@@ -395,11 +395,12 @@ export class PostActivity {
try {
await this._temporalService.client
.getRawClient()
- .workflow.start('streakWorkflow', {
+ .workflow.start('streakWorkflowV2', {
args: [{ organizationId: integration.organizationId }],
workflowId: `streak_${integration.organizationId}`,
taskQueue: 'main',
- workflowIdConflictPolicy: 'TERMINATE_EXISTING',
+ // Keep a running streak, it checks for newer posts before it ends
+ workflowIdConflictPolicy: 'USE_EXISTING',
typedSearchAttributes: new TypedSearchAttributes([
{
key: organizationId,
diff --git a/apps/orchestrator/src/workflows/streak.workflow.ts b/apps/orchestrator/src/workflows/streak.workflow.ts
index 28845e4d3..e4f30bad8 100644
--- a/apps/orchestrator/src/workflows/streak.workflow.ts
+++ b/apps/orchestrator/src/workflows/streak.workflow.ts
@@ -1,13 +1,21 @@
-import { patched, proxyActivities, sleep } from '@temporalio/workflow';
+import {
+ continueAsNew,
+ makeContinueAsNewFunc,
+ patched,
+ proxyActivities,
+ sleep,
+} from '@temporalio/workflow';
import { EmailActivity } from '@gitroom/orchestrator/activities/email.activity';
-const { sendEmailAsync, getUserOrgs, setStreak } =
+const { sendEmailAsync, getUserOrgs, setStreak, getLastPublishDate } =
proxyActivities<EmailActivity>({
startToCloseTimeout: '10 minute',
taskQueue: 'main',
cancellationType: 'ABANDON',
});
+const DAY = 86400000;
+
export async function streakWorkflow({
organizationId,
}: {
@@ -33,6 +41,17 @@ export async function streakWorkflow({
await sleep(7200000);
+ // Posts no longer restart this workflow, hand the streak over to
+ // streakWorkflowV2 if the organization posted since it started
+ if (patched('streak-last-post')) {
+ const lastPost = await getLastPublishDate(organizationId);
+ if (lastPost && lastPost + DAY > Date.now()) {
+ return await makeContinueAsNewFunc<typeof streakWorkflowV2>({
+ workflowType: 'streakWorkflowV2',
+ })({ organizationId, lastPost });
+ }
+ }
+
if (patched('reminder')) {
for (const user of userOrgs.users) {
if (!user.user.sendStreakEmails) {
@@ -48,3 +67,52 @@ export async function streakWorkflow({
}
await setStreak(organizationId, 'end');
}
+
+// One run per organization that posts don't restart: it wakes up when the
+// last post is a day old and ends the streak if nothing newer was published
+export async function streakWorkflowV2({
+ organizationId,
+ lastPost,
+}: {
+ organizationId: string;
+ lastPost?: number;
+}) {
+ if (lastPost === undefined) {
+ // Started by a publish
+ await setStreak(organizationId, 'start');
+ lastPost = Date.now();
+ }
+
+ for (let check = 0; check < 30; check++) {
+ await sleep(Math.max(1000, lastPost + DAY - Date.now()));
+ lastPost = Math.max(
+ lastPost,
+ (await getLastPublishDate(organizationId)) || 0
+ );
+
+ if (lastPost + DAY > Date.now()) {
+ continue;
+ }
+
+ const userOrgs = await getUserOrgs(organizationId);
+ for (const user of userOrgs.users) {
+ if (!user.user.sendStreakEmails) {
+ continue;
+ }
+ await sendEmailAsync(
+ user.user.email,
+ 'Streak Ended',
+ "<p>Your streak has ended! You didn't post anything in the last 24 hours. Schedule a post now to start a new streak!</p>",
+ 'bottom'
+ );
+ }
+ await setStreak(organizationId, 'end');
+ return;
+ }
+
+ // Keep the history small for organizations that post every day
+ return await continueAsNew<typeof streakWorkflowV2>({
+ organizationId,
+ lastPost,
+ });
+}
diff --git a/libraries/nestjs-libraries/src/database/prisma/organizations/organization.repository.ts b/libraries/nestjs-libraries/src/database/prisma/organizations/organization.repository.ts
index ad0690173..f70f71c9d 100644
--- a/libraries/nestjs-libraries/src/database/prisma/organizations/organization.repository.ts
+++ b/libraries/nestjs-libraries/src/database/prisma/organizations/organization.repository.ts
@@ -528,6 +528,32 @@ export class OrganizationRepository {
} catch (err) {}
}
+ async getLastPublishDate(organizationId: string) {
+ const org = await this._organization.model.organization.findUnique({
+ where: {
+ id: organizationId,
+ },
+ select: {
+ post: {
+ where: {
+ state: 'PUBLISHED',
+ },
+ orderBy: {
+ publishDate: 'desc',
+ },
+ take: 1,
+ select: {
+ publishDate: true,
+ },
+ },
+ },
+ });
+
+ const publishDate = org?.post?.[0]?.publishDate;
+ // Never in the future, the streak workflow sleeps until a day after it
+ return publishDate ? Math.min(publishDate.getTime(), Date.now()) : null;
+ }
+
async getTeam(orgId: string) {
return this._organization.model.organization.findUnique({
where: {
diff --git a/libraries/nestjs-libraries/src/database/prisma/organizations/organization.service.ts b/libraries/nestjs-libraries/src/database/prisma/organizations/organization.service.ts
index dae6677e4..c6cf15bf0 100644
--- a/libraries/nestjs-libraries/src/database/prisma/organizations/organization.service.ts
+++ b/libraries/nestjs-libraries/src/database/prisma/organizations/organization.service.ts
@@ -92,6 +92,10 @@ export class OrganizationService {
return this._organizationRepository.setStreak(organizationId, type);
}
+ async getLastPublishDate(organizationId: string) {
+ return this._organizationRepository.getLastPublishDate(organizationId);
+ }
+
getOrgByCustomerId(customerId: string) {
return this._organizationRepository.getOrgByCustomerId(customerId);
}