-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
130 additions
and
18 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
apps/egghead/src/inngest/functions/sync-posts-to-egghead-lessons.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { eggheadPgQuery } from '@/db/eggheadPostgres' | ||
import { | ||
getAllPostIds, | ||
getPost, | ||
writePostUpdateToDatabase, | ||
} from '@/lib/posts-query' | ||
import { z } from 'zod' | ||
|
||
import { inngest } from '../inngest.server' | ||
|
||
export const SYNC_POSTS_TO_EGGHEAD_LESSONS_EVENT = | ||
'posts/sync-to-egghead-lessons' | ||
|
||
export type SyncPostsToEggheadLessonsEvent = { | ||
name: typeof SYNC_POSTS_TO_EGGHEAD_LESSONS_EVENT | ||
data: {} | ||
} | ||
|
||
export const syncPostsToEggheadLessons = inngest.createFunction( | ||
{ | ||
id: 'sync-posts-to-egghead-lessons', | ||
name: 'Sync ALL lesson posts to egghead lessons', | ||
}, | ||
{ event: SYNC_POSTS_TO_EGGHEAD_LESSONS_EVENT }, | ||
async ({ event, step }) => { | ||
const postIds = await step.run('Get post ids', async () => { | ||
return await getAllPostIds() | ||
}) | ||
|
||
// Process each post ID sequentially to avoid race conditions | ||
for (const postId of postIds) { | ||
const post = await step.run(`load-post-${postId}`, async () => { | ||
return await getPost(postId) | ||
}) | ||
|
||
if (!post) { | ||
continue | ||
} | ||
|
||
await step.run(`sync-post-${postId}`, async () => { | ||
const eggheadLessonId = post.fields.eggheadLessonId | ||
|
||
if (eggheadLessonId) { | ||
await writePostUpdateToDatabase({ | ||
postUpdate: post, | ||
action: 'save', | ||
updatedById: 'egghead', | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
return { success: true } | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters