-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
ac6357f
commit 8c78fb7
Showing
11 changed files
with
216 additions
and
119 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,55 @@ | ||
import type { ExecutionContext, ScheduledController } from "@cloudflare/workers-types" | ||
import type { D1Database, ExecutionContext, R2Bucket, ScheduledController } from "@cloudflare/workers-types" | ||
import type { Environment } from "@env" | ||
import type { Gear } from "@schemas/strava" | ||
import { GearService, PostService } from "@services" | ||
import { parseMarkdown } from "@utils" | ||
import * as Sentry from "@sentry/cloudflare" | ||
|
||
const GearRegex = /gear\/([b0-9]+).meta.json/ | ||
const PostsRegex = /posts\/([A-Za-z0-9\-]+).md/ | ||
|
||
export default { | ||
async scheduled(controller: ScheduledController, env: Environment, ctx: ExecutionContext) { | ||
const gear = await env.STRAVA.list({ prefix: "gear" }) | ||
for (const object of gear.objects) { | ||
const key = object.key | ||
const id = GearRegex.exec(key)?.[1] | ||
if (!id) { | ||
console.log("Unable to extract id from key", key) | ||
continue | ||
} | ||
const body = await env.STRAVA.get(key) | ||
if (!body) { | ||
console.log("This should never happen...", key) | ||
continue | ||
} | ||
const gear = await body.json<Gear>() | ||
const service = GearService(env.DB) | ||
await service.upsert(gear) | ||
async function processBuckets(blog: R2Bucket, strava: R2Bucket, db: D1Database) { | ||
const posts = await blog.list({ prefix: "posts" }) | ||
for (const object of posts.objects) { | ||
const key = object.key | ||
const slug = PostsRegex.exec(key)?.[1] | ||
if (!slug) { | ||
console.log("Unable to extract slug from key", key) | ||
continue | ||
} | ||
|
||
const posts = await env.BUCKET.list({ prefix: "posts" }) | ||
for (const object of posts.objects) { | ||
const key = object.key | ||
const slug = PostsRegex.exec(key)?.[1] | ||
if (!slug) { | ||
console.log("Unable to extract slug from key", key) | ||
continue | ||
} | ||
const body = await env.BUCKET.get(key) | ||
const raw = await body!.text() | ||
const post = parseMarkdown(raw, slug) | ||
const service = PostService(env.DB) | ||
await service.upsert(post) | ||
const body = await blog.get(key) | ||
const raw = await body!.text() | ||
const post = parseMarkdown(raw, slug) | ||
const service = PostService(db) | ||
await service.upsert(post) | ||
} | ||
|
||
const gear = await strava.list({ prefix: "gear" }) | ||
for (const object of gear.objects) { | ||
const key = object.key | ||
const id = GearRegex.exec(key)?.[1] | ||
if (!id) { | ||
console.log("Unable to extract id from key", key) | ||
continue | ||
} | ||
const body = await strava.get(key) | ||
if (!body) { | ||
console.log("This should never happen...", key) | ||
continue | ||
} | ||
const gear = await body.json<Gear>() | ||
const service = GearService(db) | ||
await service.upsert(gear) | ||
} | ||
} | ||
|
||
export default Sentry.withSentry(function(env) { | ||
return { | ||
dsn: env.SENTRY_DSN | ||
} | ||
}, { | ||
async scheduled(controller: ScheduledController, env: Environment, ctx: ExecutionContext) { | ||
const db = Sentry.instrumentD1WithSentry(env.DB) | ||
ctx.waitUntil(processBuckets(env.BLOG, env.STRAVA, db)) | ||
} | ||
} | ||
}) |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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