@learnpack/learnpack 5.0.352 → 5.0.354
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/commands/publish.js +6 -0
- package/lib/commands/serve.js +85 -259
- package/lib/models/creator.d.ts +6 -0
- package/lib/scripts/descriptionsGcsBackfill.d.ts +1 -0
- package/lib/scripts/descriptionsGcsBackfill.js +141 -0
- package/lib/scripts/descriptionsS3Backfill.d.ts +1 -0
- package/lib/scripts/descriptionsS3Backfill.js +277 -0
- package/lib/scripts/descriptionsSweep.d.ts +1 -0
- package/lib/scripts/descriptionsSweep.js +142 -0
- package/lib/utils/api.d.ts +7 -0
- package/lib/utils/api.js +8 -1
- package/lib/utils/awsCredentials.d.ts +20 -0
- package/lib/utils/awsCredentials.js +43 -0
- package/lib/utils/creatorUtilities.js +2 -1
- package/lib/utils/descriptionHash.d.ts +66 -0
- package/lib/utils/descriptionHash.js +173 -0
- package/lib/utils/descriptions/backfillEvents.d.ts +60 -0
- package/lib/utils/descriptions/backfillEvents.js +107 -0
- package/lib/utils/descriptions/gcsStorage.d.ts +16 -0
- package/lib/utils/descriptions/gcsStorage.js +60 -0
- package/lib/utils/descriptions/generateCourseDescriptions.d.ts +66 -0
- package/lib/utils/descriptions/generateCourseDescriptions.js +176 -0
- package/lib/utils/descriptions/mirrorDescriptions.d.ts +49 -0
- package/lib/utils/descriptions/mirrorDescriptions.js +109 -0
- package/lib/utils/descriptions/publishStage.d.ts +69 -0
- package/lib/utils/descriptions/publishStage.js +245 -0
- package/lib/utils/descriptions/resumePublication.d.ts +36 -0
- package/lib/utils/descriptions/resumePublication.js +128 -0
- package/lib/utils/descriptions/s3Storage.d.ts +30 -0
- package/lib/utils/descriptions/s3Storage.js +141 -0
- package/lib/utils/descriptions/workList.d.ts +75 -0
- package/lib/utils/descriptions/workList.js +177 -0
- package/lib/utils/gcsBucketName.d.ts +10 -0
- package/lib/utils/gcsBucketName.js +19 -0
- package/lib/utils/packageManifest.d.ts +22 -0
- package/lib/utils/packageManifest.js +57 -5
- package/lib/utils/publishEvents.d.ts +66 -0
- package/lib/utils/publishEvents.js +111 -0
- package/lib/utils/publishJournal.d.ts +119 -0
- package/lib/utils/publishJournal.js +275 -0
- package/lib/utils/repair/legacyPackageRepair.d.ts +131 -0
- package/lib/utils/repair/legacyPackageRepair.js +492 -0
- package/lib/utils/repair/repairStorage.d.ts +68 -0
- package/lib/utils/repair/repairStorage.js +89 -0
- package/lib/utils/rigoActions.d.ts +44 -0
- package/lib/utils/rigoActions.js +75 -1
- package/lib/utils/s3/packageManifestBackfill.d.ts +2 -0
- package/lib/utils/s3/packageManifestBackfill.js +5 -8
- package/lib/utils/s3/packageSourcesAudit.d.ts +75 -0
- package/lib/utils/s3/packageSourcesAudit.js +184 -0
- package/lib/utils/syllabusSync.d.ts +71 -0
- package/lib/utils/syllabusSync.js +273 -0
- package/package.json +3 -1
- package/src/commands/publish.ts +7 -0
- package/src/commands/serve.ts +144 -335
- package/src/models/creator.ts +9 -0
- package/src/scripts/README.md +244 -0
- package/src/scripts/descriptionsGcsBackfill.ts +193 -0
- package/src/scripts/descriptionsS3Backfill.ts +376 -0
- package/src/scripts/descriptionsSweep.ts +185 -0
- package/src/ui/_app/app.css +1 -1
- package/src/ui/_app/app.js +142 -140
- package/src/ui/app.tar.gz +0 -0
- package/src/utils/api.ts +9 -0
- package/src/utils/awsCredentials.ts +57 -0
- package/src/utils/creatorUtilities.ts +2 -1
- package/src/utils/descriptionHash.ts +196 -0
- package/src/utils/descriptions/backfillEvents.ts +152 -0
- package/src/utils/descriptions/gcsStorage.ts +67 -0
- package/src/utils/descriptions/generateCourseDescriptions.ts +311 -0
- package/src/utils/descriptions/mirrorDescriptions.ts +191 -0
- package/src/utils/descriptions/publishStage.ts +394 -0
- package/src/utils/descriptions/resumePublication.ts +217 -0
- package/src/utils/descriptions/s3Storage.ts +214 -0
- package/src/utils/descriptions/workList.ts +283 -0
- package/src/utils/gcsBucketName.ts +19 -0
- package/src/utils/packageManifest.ts +62 -5
- package/src/utils/publishEvents.ts +181 -0
- package/src/utils/publishJournal.ts +383 -0
- package/src/utils/repair/legacyPackageRepair.ts +731 -0
- package/src/utils/repair/repairStorage.ts +168 -0
- package/src/utils/rigoActions.ts +130 -0
- package/src/utils/s3/packageManifestBackfill.ts +771 -776
- package/src/utils/s3/packageSourcesAudit.ts +311 -0
- package/src/utils/syllabusSync.ts +390 -0
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
# Descriptions scripts (`src/scripts/`)
|
|
2
|
+
|
|
3
|
+
Ops scripts for the **step descriptions** of published courses. They are dev/ops
|
|
4
|
+
entry points, not oclif commands: they never appear under `learnpack <something>`
|
|
5
|
+
and are run with `node` against the compiled output.
|
|
6
|
+
|
|
7
|
+
| Script | What it does | Calls the LLM |
|
|
8
|
+
| ------------------------- | ----------------------------------------------------------------------- | ------------- |
|
|
9
|
+
| `descriptionsS3Backfill` | Generates descriptions for **published** courses (S3) | Yes |
|
|
10
|
+
| `descriptionsGcsBackfill` | Copies existing descriptions from S3 into the **draft** (GCS) | No |
|
|
11
|
+
| `descriptionsSweep` | Finishes publications that stalled mid-flight; runs on Heroku Scheduler | Yes |
|
|
12
|
+
|
|
13
|
+
All three delegate the per-course work to the same service,
|
|
14
|
+
[`generateCourseDescriptions`](../utils/descriptions/generateCourseDescriptions.ts),
|
|
15
|
+
which is also what the post-publish stage uses. They differ only in **which**
|
|
16
|
+
courses they run it on and **what else** they do around it.
|
|
17
|
+
|
|
18
|
+
## Build and run
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm run tsc
|
|
22
|
+
node lib/scripts/<script>.js [flags]
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
They run against `lib/`, not `src/`, so recompile after editing. There is no
|
|
26
|
+
`--help`: the flags are documented here and in each script's header.
|
|
27
|
+
|
|
28
|
+
## Environment
|
|
29
|
+
|
|
30
|
+
Copy from [`.env.example`](../../.env.example) into `learnpack-cli/.env`. These
|
|
31
|
+
scripts read `process.env` directly — they do **not** load the `.env` themselves,
|
|
32
|
+
so export it (`set -a; . ./.env; set +a`) or pass the variables inline.
|
|
33
|
+
|
|
34
|
+
| Variable | Needed by |
|
|
35
|
+
| ------------------------------------------ | ------------------------------------------------------------------------------- |
|
|
36
|
+
| `RIGOBOT_SYSTEM_TOKEN` | S3 backfill, sweep (generation) |
|
|
37
|
+
| `BREATHECODE_SYSTEM_TOKEN` | `--emit-events`, sweep (pending events) |
|
|
38
|
+
| `AWS_ACCESS_KEY_ID` / `_SECRET_ACCESS_KEY` | anything reading the published bucket |
|
|
39
|
+
| `AWS_REGION` | optional, default `us-east-1` |
|
|
40
|
+
| `S3_PACKAGES_BUCKET` | optional in the backfills (default `learnpack-paquetes`), required by the sweep |
|
|
41
|
+
| `CLOUDFRONT_DISTRIBUTION_ID` | optional; without it manifests are re-projected but not invalidated |
|
|
42
|
+
| `GCP_CREDENTIALS_JSON` | GCS backfill, sweep, `--mirror-draft` |
|
|
43
|
+
| `GCP_BUCKET_NAME` | same; no default on purpose |
|
|
44
|
+
|
|
45
|
+
Descriptions are always generated with the Rigobot **service** account, never
|
|
46
|
+
with a user token: they are platform enrichment, not something a creator is
|
|
47
|
+
billed for.
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
51
|
+
## Mode 1 — targeted run (a few slugs, on demand)
|
|
52
|
+
|
|
53
|
+
The everyday case: a course was published before descriptions existed, or one
|
|
54
|
+
needs remediation. Three flags make the S3 backfill reproduce per course
|
|
55
|
+
everything a publication does, in one process:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
node lib/scripts/descriptionsS3Backfill.js --slug my-course \
|
|
59
|
+
--reproject-manifest --mirror-draft --emit-events --concurrency 2
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
| Flag | Without it |
|
|
63
|
+
| ---------------------- | --------------------------------------------------------------------------------------- |
|
|
64
|
+
| `--reproject-manifest` | `package-manifest.json` keeps the old descriptions until a later pass |
|
|
65
|
+
| `--mirror-draft` | the draft has no fingerprints, so the **next publication regenerates the whole course** |
|
|
66
|
+
| `--emit-events` | breathecode is never told the manifest changed |
|
|
67
|
+
|
|
68
|
+
All three are off by default, so the catalogue-wide run below is unaffected.
|
|
69
|
+
|
|
70
|
+
Run it once per slug. Do a `--dry-run` first if you want to see the scope —
|
|
71
|
+
it reports what it would generate and mirror without writing anything.
|
|
72
|
+
|
|
73
|
+
**During high-traffic hours**, lower `--concurrency` (default 5 completions in
|
|
74
|
+
flight per course). A single completion takes ~15s.
|
|
75
|
+
|
|
76
|
+
Notes:
|
|
77
|
+
|
|
78
|
+
- `--emit-events` **requires** `--reproject-manifest`. The event reports a
|
|
79
|
+
manifest update, so emitting it without projecting would state something
|
|
80
|
+
untrue. It is required rather than implied on purpose: writing to S3 and the
|
|
81
|
+
CDN as a side effect of another flag is the kind of surprise a backfill should
|
|
82
|
+
not have.
|
|
83
|
+
- `--emit-events` is rejected together with `--dry-run`: there is no dry run of
|
|
84
|
+
an event breathecode already received.
|
|
85
|
+
- `--mirror-draft` is independent of the other two — it touches the draft, not
|
|
86
|
+
the manifest.
|
|
87
|
+
- Prefer `--slug` over `--limit 4` when you care about _which_ courses are
|
|
88
|
+
touched: `--limit` counts **processed** courses (already-settled ones are
|
|
89
|
+
skipped without counting) and walks the catalogue by recency.
|
|
90
|
+
|
|
91
|
+
### What each course does, in order
|
|
92
|
+
|
|
93
|
+
1. Generate the missing descriptions and write them into the published
|
|
94
|
+
`initialSyllabus.json` (S3).
|
|
95
|
+
2. `--reproject-manifest`: rebuild `package-manifest.json` from that syllabus
|
|
96
|
+
(the same `processPackage` the manifest backfill runs) and invalidate its
|
|
97
|
+
CloudFront path.
|
|
98
|
+
3. `--mirror-draft`: copy the descriptions back into the GCS draft, guarded by
|
|
99
|
+
the README content hash — anything the teacher edited since publishing is
|
|
100
|
+
counted as `missed` and waits for the next publication.
|
|
101
|
+
4. `--emit-events`: send `package_manifest_updated` to breathecode.
|
|
102
|
+
|
|
103
|
+
A failure in step 3 does not change the event status: the published package and
|
|
104
|
+
its manifest are already correct, only the draft lags behind. A failure in step 2
|
|
105
|
+
**does** — see below.
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## Mode 2 — catalogue backfill (fases 0a / 0b)
|
|
110
|
+
|
|
111
|
+
The one-off sweep over the ~400 published courses and ~800 drafts. Here the
|
|
112
|
+
defaults are the right ones: manifests are cheaper to project in a single later
|
|
113
|
+
pass than course by course, and the draft is seeded by copy rather than by
|
|
114
|
+
re-generating.
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
# Fase 0a — descriptions for the published catalogue, 50 courses per run.
|
|
118
|
+
# Re-run until it stops reporting processed courses.
|
|
119
|
+
node lib/scripts/descriptionsS3Backfill.js --limit 50
|
|
120
|
+
|
|
121
|
+
# Then project every manifest in one pass (cheaper than per course).
|
|
122
|
+
npm run backfill:package-manifest
|
|
123
|
+
|
|
124
|
+
# Fase 0b — seed the drafts by copying from S3. Never calls the LLM.
|
|
125
|
+
node lib/scripts/descriptionsGcsBackfill.js --limit 200
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Order matters: 0b reads the **published** syllabus, so 0a must have written it
|
|
129
|
+
first. Whatever 0b cannot reuse (drafts edited since their last publication) is
|
|
130
|
+
left for the next publication of that course, or for a single-slug 0a run.
|
|
131
|
+
|
|
132
|
+
No events are emitted in this mode. If breathecode needs to be told about the
|
|
133
|
+
catalogue, that is a separate decision — adding `--emit-events` to a 400-course
|
|
134
|
+
run would deliver 400 events.
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
## Flag reference
|
|
139
|
+
|
|
140
|
+
### `descriptionsS3Backfill`
|
|
141
|
+
|
|
142
|
+
| Flag | Default | Description |
|
|
143
|
+
| ---------------------- | -------------------- | ----------------------------------------------------------------- |
|
|
144
|
+
| `--slug <slug>` | — | single course (remediation / on-demand) |
|
|
145
|
+
| `--limit <n>` | 50 | courses **processed** per run, ordered by syllabus recency |
|
|
146
|
+
| `--s3-bucket <name>` | `learnpack-paquetes` | published bucket |
|
|
147
|
+
| `--gcs-bucket <name>` | `GCP_BUCKET_NAME` | draft bucket, for `--mirror-draft` |
|
|
148
|
+
| `--region <region>` | `us-east-1` | AWS region |
|
|
149
|
+
| `--concurrency <n>` | 5 | completions in flight per course |
|
|
150
|
+
| `--target-words <n>` | 25 | words per description |
|
|
151
|
+
| `--dry-run` | off | preview; no writes, no completions |
|
|
152
|
+
| `--force` | off | regenerate courses already settled at the current prompt version |
|
|
153
|
+
| `--no-reconcile` | off | skip the additive syllabus reconciliation |
|
|
154
|
+
| `--reproject-manifest` | off | rebuild + invalidate `package-manifest.json` per course |
|
|
155
|
+
| `--mirror-draft` | off | copy the descriptions into the GCS draft |
|
|
156
|
+
| `--emit-events` | off | send `package_manifest_updated` (requires `--reproject-manifest`) |
|
|
157
|
+
|
|
158
|
+
### `descriptionsGcsBackfill`
|
|
159
|
+
|
|
160
|
+
| Flag | Default | Description |
|
|
161
|
+
| --------------------- | -------------------- | -------------------------------- |
|
|
162
|
+
| `--slug <slug>` | — | single course |
|
|
163
|
+
| `--limit <n>` | 50 | courses per run |
|
|
164
|
+
| `--s3-bucket <name>` | `learnpack-paquetes` | source (published) bucket |
|
|
165
|
+
| `--gcs-bucket <name>` | `GCP_BUCKET_NAME` | target (draft) bucket |
|
|
166
|
+
| `--region <region>` | `us-east-1` | AWS region |
|
|
167
|
+
| `--dry-run` | off | preview without writing |
|
|
168
|
+
| `--no-reconcile` | off | skip the syllabus reconciliation |
|
|
169
|
+
|
|
170
|
+
### `descriptionsSweep`
|
|
171
|
+
|
|
172
|
+
No flags — it is a scheduled job, configured by env:
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
node lib/scripts/descriptionsSweep.js
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Two sources of work:
|
|
179
|
+
|
|
180
|
+
1. **Publish journals** (`publish-journal/` in GCS), the primary one. Every
|
|
181
|
+
publication that stalled is resumed: missing descriptions generated, draft
|
|
182
|
+
mirrored, and the owed `package_manifest_updated` emitted. Journals are
|
|
183
|
+
dropped once complete and abandoned after a few attempts.
|
|
184
|
+
2. **A deep scan** of published courses (`DESCRIPTIONS_SWEEP_FULL=1`), for
|
|
185
|
+
packages that never got a journal at all. Expensive — it reads every course —
|
|
186
|
+
and off by default. It re-projects manifests but does **not** emit events.
|
|
187
|
+
|
|
188
|
+
Tuning: `DESCRIPTIONS_SWEEP_LIMIT` (courses per deep scan, default 25),
|
|
189
|
+
`DESCRIPTION_STALE_AFTER_MS` (how long a publication may look stalled before it
|
|
190
|
+
is resumed, default 15 min).
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
## The `package_manifest_updated` event
|
|
195
|
+
|
|
196
|
+
Emitted by `--emit-events`, and by the sweep for the publications it finishes.
|
|
197
|
+
Built in [`publishEvents.ts`](../utils/publishEvents.ts), sent to the breathecode
|
|
198
|
+
telemetry endpoint with `BREATHECODE_SYSTEM_TOKEN`.
|
|
199
|
+
|
|
200
|
+
Breathecode processes it **independently of `package_published`**, so a backfill
|
|
201
|
+
event with no preceding publication is fine.
|
|
202
|
+
|
|
203
|
+
**`publish_id` in a backfill** is `backfill-{runId}-{slug}`:
|
|
204
|
+
|
|
205
|
+
- the `backfill-` prefix makes the origin readable in the webhook log and
|
|
206
|
+
discriminable in code (`isBackfillPublishId`); real publish ids are bare
|
|
207
|
+
uuids, so the two spaces cannot collide;
|
|
208
|
+
- `{slug}` keeps it unique per event;
|
|
209
|
+
- `{runId}` is one uuid per **invocation**, shared by every course it touches, so
|
|
210
|
+
"every event from the run I fired at 15:40" is a single substring query. It is
|
|
211
|
+
printed at start and in the closing summary.
|
|
212
|
+
|
|
213
|
+
Note that running four separate `--slug` commands produces four run ids. Use
|
|
214
|
+
`--limit` if you want one id for the batch — at the cost of not choosing the
|
|
215
|
+
slugs.
|
|
216
|
+
|
|
217
|
+
**Status is tied to the real outcome**, not to the script finishing:
|
|
218
|
+
|
|
219
|
+
| Outcome | `status` |
|
|
220
|
+
| ---------------------------------------- | --------- |
|
|
221
|
+
| Descriptions written, manifest projected | `success` |
|
|
222
|
+
| Generation failed outright | `failed` |
|
|
223
|
+
| Manifest projection failed | `failed` |
|
|
224
|
+
| Course unchanged (`skipped`) | no event |
|
|
225
|
+
| Course threw | no event |
|
|
226
|
+
|
|
227
|
+
A failed manifest projection is not a failed run — the syllabus, which is the
|
|
228
|
+
source of truth, is already saved — but this event reports the manifest, so it
|
|
229
|
+
cannot claim success over one that was never rewritten. That is what
|
|
230
|
+
`GenerateCourseDescriptionsResult.manifestProjected` exists for.
|
|
231
|
+
|
|
232
|
+
The last row is deliberate: unlike a publication, a backfill never _announced_ an
|
|
233
|
+
event, so it owes none. The course is simply re-run.
|
|
234
|
+
|
|
235
|
+
---
|
|
236
|
+
|
|
237
|
+
## Related
|
|
238
|
+
|
|
239
|
+
- [`../utils/descriptions/`](../utils/descriptions/) — the shared service, the
|
|
240
|
+
mirror, the post-publish stage and the resume logic
|
|
241
|
+
- [`../../scripts/README.md`](../../scripts/README.md) — the package-manifest S3
|
|
242
|
+
backfill (`npm run backfill:package-manifest`)
|
|
243
|
+
- [`../utils/publishJournal.ts`](../utils/publishJournal.ts) — why the sweep can
|
|
244
|
+
know an event is owed
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/* eslint-disable no-await-in-loop */
|
|
2
|
+
/**
|
|
3
|
+
* Fase 0b — GCS descriptions backfill (reuse from S3, no LLM).
|
|
4
|
+
*
|
|
5
|
+
* Seeds the GCS (draft) syllabus with descriptions already generated for the
|
|
6
|
+
* published S3 syllabus (Fase 0a) — a pure COPY, guarded by the content hash,
|
|
7
|
+
* performed by the shared mirror (`mirrorDescriptionsToDraft`). It NEVER calls
|
|
8
|
+
* the LLM: the ~800 GCS drafts are mostly unused, so anything not reusable from
|
|
9
|
+
* S3 is left for the next publication of that course (or a single-slug 0a run).
|
|
10
|
+
* Run AFTER 0a has completed.
|
|
11
|
+
*
|
|
12
|
+
* Batched like 0a: --limit courses per run (default 50), ordered by
|
|
13
|
+
* initialSyllabus modification time (newest first).
|
|
14
|
+
*
|
|
15
|
+
* Secrets/infra via env: GCP_CREDENTIALS_JSON (required), plus
|
|
16
|
+
* S3_PACKAGES_BUCKET / GCP_BUCKET_NAME / AWS_REGION as fallbacks for the
|
|
17
|
+
* corresponding flags.
|
|
18
|
+
*
|
|
19
|
+
* Per-run flags:
|
|
20
|
+
* --s3-bucket <name> published S3 bucket to reuse from (or env S3_PACKAGES_BUCKET)
|
|
21
|
+
* --gcs-bucket <name> GCS bucket (or env GCP_BUCKET_NAME, which is required)
|
|
22
|
+
* --region <region> AWS region (default us-east-1, or env AWS_REGION)
|
|
23
|
+
* --slug <slug> process a single course
|
|
24
|
+
* --limit <n> courses per run (default 50)
|
|
25
|
+
* --dry-run preview without writing
|
|
26
|
+
* --no-reconcile skip the additive syllabus reconciliation
|
|
27
|
+
*
|
|
28
|
+
* Examples:
|
|
29
|
+
* node lib/scripts/descriptionsGcsBackfill.js --limit 200
|
|
30
|
+
* node lib/scripts/descriptionsGcsBackfill.js --slug my-course
|
|
31
|
+
*/
|
|
32
|
+
import { parseArgs } from "node:util"
|
|
33
|
+
import { Bucket, Storage } from "@google-cloud/storage"
|
|
34
|
+
import { S3Client } from "@aws-sdk/client-s3"
|
|
35
|
+
import { Syllabus } from "../models/creator"
|
|
36
|
+
import { synchronizeSyllabusWithBucket } from "../utils/syllabusSync"
|
|
37
|
+
import {
|
|
38
|
+
AwsClient,
|
|
39
|
+
fetchJsonObject,
|
|
40
|
+
} from "../utils/s3/packageManifestBackfill"
|
|
41
|
+
import { requireGcsBucketName } from "../utils/gcsBucketName"
|
|
42
|
+
import { createGcsDescriptionsStorage } from "../utils/descriptions/gcsStorage"
|
|
43
|
+
import { mirrorDescriptionsToDraft } from "../utils/descriptions/mirrorDescriptions"
|
|
44
|
+
import { syllabusKey as s3SyllabusKey } from "../utils/descriptions/s3Storage"
|
|
45
|
+
|
|
46
|
+
const GCS_SYLLABUS_PATTERN =
|
|
47
|
+
/^courses\/([^/]+)\/\.learn\/initialSyllabus\.json$/
|
|
48
|
+
|
|
49
|
+
/** Courses (by slug) ordered by initialSyllabus.json update time, newest first. */
|
|
50
|
+
async function listCoursesByRecency(bucket: Bucket): Promise<string[]> {
|
|
51
|
+
const [files] = await bucket.getFiles({ prefix: "courses/" })
|
|
52
|
+
const mtimes = new Map<string, number>()
|
|
53
|
+
for (const file of files) {
|
|
54
|
+
const match = file.name.match(GCS_SYLLABUS_PATTERN)
|
|
55
|
+
if (match) {
|
|
56
|
+
const updated = (file.metadata as { updated?: string } | undefined)
|
|
57
|
+
?.updated
|
|
58
|
+
mtimes.set(match[1], updated ? new Date(updated).getTime() : 0)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return [...mtimes.entries()]
|
|
63
|
+
.sort((a, b) => b[1] - a[1])
|
|
64
|
+
.map(entry => entry[0])
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function main(): Promise<void> {
|
|
68
|
+
const { values } = parseArgs({
|
|
69
|
+
options: {
|
|
70
|
+
"s3-bucket": { type: "string" },
|
|
71
|
+
"gcs-bucket": { type: "string" },
|
|
72
|
+
region: { type: "string" },
|
|
73
|
+
slug: { type: "string" },
|
|
74
|
+
limit: { type: "string" },
|
|
75
|
+
"dry-run": { type: "boolean", default: false },
|
|
76
|
+
"no-reconcile": { type: "boolean", default: false },
|
|
77
|
+
},
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
const s3Bucket =
|
|
81
|
+
values["s3-bucket"] ||
|
|
82
|
+
process.env.S3_PACKAGES_BUCKET ||
|
|
83
|
+
"learnpack-paquetes"
|
|
84
|
+
|
|
85
|
+
const credentialsEnv = process.env.GCP_CREDENTIALS_JSON
|
|
86
|
+
if (!credentialsEnv) {
|
|
87
|
+
console.error("[gcs-backfill] GCP_CREDENTIALS_JSON (env) is required")
|
|
88
|
+
process.exit(1)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const bucket = new Storage({
|
|
92
|
+
credentials: JSON.parse(credentialsEnv),
|
|
93
|
+
}).bucket(values["gcs-bucket"] || requireGcsBucketName())
|
|
94
|
+
|
|
95
|
+
const s3 = new S3Client({
|
|
96
|
+
region: values.region || process.env.AWS_REGION || "us-east-1",
|
|
97
|
+
}) as unknown as AwsClient
|
|
98
|
+
|
|
99
|
+
const dryRun = values["dry-run"] ?? false
|
|
100
|
+
const reconcile = !(values["no-reconcile"] ?? false)
|
|
101
|
+
const limit = Number.parseInt(values.limit || "50", 10) || 50
|
|
102
|
+
const storage = createGcsDescriptionsStorage(bucket)
|
|
103
|
+
|
|
104
|
+
const singleSlug = values.slug
|
|
105
|
+
const slugs = singleSlug ? [singleSlug] : await listCoursesByRecency(bucket)
|
|
106
|
+
|
|
107
|
+
console.log(
|
|
108
|
+
`[gcs-backfill] Starting${
|
|
109
|
+
singleSlug ? ` (slug=${singleSlug})` : ` (limit=${limit})`
|
|
110
|
+
}${dryRun ? " (dry-run)" : ""} over ${slugs.length} candidate course(s)`
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
let processed = 0
|
|
114
|
+
let copied = 0
|
|
115
|
+
let missed = 0
|
|
116
|
+
|
|
117
|
+
for (const slug of slugs) {
|
|
118
|
+
if (!singleSlug && processed >= limit) {
|
|
119
|
+
break
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
try {
|
|
123
|
+
const publishedSyllabus = await fetchJsonObject<Syllabus>(
|
|
124
|
+
s3,
|
|
125
|
+
s3Bucket,
|
|
126
|
+
s3SyllabusKey(slug)
|
|
127
|
+
)
|
|
128
|
+
if (!publishedSyllabus) {
|
|
129
|
+
continue
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (reconcile && storage.syllabusSyncStorage) {
|
|
133
|
+
const draft = await storage.readSyllabus(slug)
|
|
134
|
+
if (draft) {
|
|
135
|
+
try {
|
|
136
|
+
const sync = await synchronizeSyllabusWithBucket(
|
|
137
|
+
storage.syllabusSyncStorage,
|
|
138
|
+
slug,
|
|
139
|
+
draft,
|
|
140
|
+
{ prune: false }
|
|
141
|
+
)
|
|
142
|
+
if (sync.changed && !dryRun) {
|
|
143
|
+
await storage.writeSyllabus(slug, sync.syllabus)
|
|
144
|
+
}
|
|
145
|
+
} catch (error) {
|
|
146
|
+
console.error(
|
|
147
|
+
`[gcs-backfill] Reconciliation failed for "${slug}":`,
|
|
148
|
+
(error as Error).message
|
|
149
|
+
)
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const result = await mirrorDescriptionsToDraft(
|
|
155
|
+
storage,
|
|
156
|
+
slug,
|
|
157
|
+
publishedSyllabus,
|
|
158
|
+
{ dryRun }
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
if (result.copied === 0 && result.missed === 0) {
|
|
162
|
+
continue
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
processed += 1
|
|
166
|
+
copied += result.copied
|
|
167
|
+
missed += result.missed
|
|
168
|
+
|
|
169
|
+
console.log(
|
|
170
|
+
`[gcs-backfill] "${slug}": copied ${result.copied}, ${
|
|
171
|
+
result.missed
|
|
172
|
+
} miss(es)${dryRun ? " (dry-run)" : ""}`
|
|
173
|
+
)
|
|
174
|
+
} catch (error) {
|
|
175
|
+
processed += 1
|
|
176
|
+
console.error(
|
|
177
|
+
`[gcs-backfill] Failed processing "${slug}":`,
|
|
178
|
+
(error as Error).message
|
|
179
|
+
)
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
console.log(
|
|
184
|
+
`[gcs-backfill] Done. ${processed} course(s), ${copied} description(s) copied, ${missed} miss(es).`
|
|
185
|
+
)
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
main()
|
|
189
|
+
.then(() => process.exit(0))
|
|
190
|
+
.catch(error => {
|
|
191
|
+
console.error("[gcs-backfill] Fatal error:", error)
|
|
192
|
+
process.exit(1)
|
|
193
|
+
})
|