Skip to content

Commit 637de01

Browse files
committed
rework polling
1 parent 71f2df3 commit 637de01

2 files changed

Lines changed: 62 additions & 45 deletions

File tree

main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ const logsCommand = new Command<{ endpoint: string }>()
159159
let onceConnected = false;
160160

161161
// deno-lint-ignore no-explicit-any
162-
const sub = (trpcClient.apps as any).logs.subscribe({
162+
const sub = await (trpcClient.apps as any).logs.subscribe({
163163
org: gottenApp.org,
164164
app: gottenApp.app,
165165
start: (options.start ? new Date(options.start) : new Date())

publish.ts

Lines changed: 61 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Spinner } from "@std/cli/unstable-spinner";
66
import { join, relative, resolve } from "@std/path";
77
import { green, yellow } from "@std/fmt/colors";
88
import { type Config, writeConfig } from "./config.ts";
9-
import { authedFetch } from "./auth.ts";
9+
import { authedFetch, createTrpcClient } from "./auth.ts";
1010
import { error } from "./util.ts";
1111

1212
const SEPARATOR_PATTERN = Deno.build.os === "windows" ? "\\\\" : "/";
@@ -105,47 +105,61 @@ export async function publish(
105105
hashesSpinner.stop();
106106
console.log(`${green("✔")} Generated hashes`);
107107

108-
const initiatedBuildRes = await authedFetch(deployUrl, "api/initiate_cli_build", {
109-
method: "POST",
110-
headers: {
111-
"content-type": "application/json",
108+
const initiatedBuildRes = await authedFetch(
109+
deployUrl,
110+
"api/initiate_cli_build",
111+
{
112+
method: "POST",
113+
headers: {
114+
"content-type": "application/json",
115+
},
116+
body: JSON.stringify({
117+
org,
118+
app,
119+
production: prod,
120+
manifest,
121+
}),
112122
},
113-
body: JSON.stringify({
114-
org,
115-
app,
116-
production: prod,
117-
manifest,
118-
}),
119-
});
123+
);
120124

121-
const { revisionId }: { revisionId: string; } = await initiatedBuildRes.json();
122-
123-
let missingHashes: string[];
124-
125-
const s = Date.now();
126-
while (true) {
127-
await new Promise(resolve => setTimeout(resolve, 1000));
128-
const maybeHashesRes = await authedFetch(deployUrl, `api/diffsync/${org}/${app}/${revisionId}`, {});
129-
if (maybeHashesRes.status !== 202) {
130-
if (maybeHashesRes.ok) {
131-
missingHashes = await maybeHashesRes.json();
132-
break;
133-
} else {
134-
const err = await maybeHashesRes.json();
135-
error(`Failed getting file hashes: ${err.message}`, maybeHashesRes);
125+
const { revisionId }: { revisionId: string } = await initiatedBuildRes.json();
126+
127+
const missingHashesPromise = Promise.withResolvers<string[]>();
128+
129+
const trpcClient = createTrpcClient(deployUrl);
130+
131+
// deno-lint-ignore no-explicit-any
132+
const sub = await (trpcClient.revisions as any).watchUntilReady.subscribe({
133+
org,
134+
app,
135+
revision: revisionId,
136+
}, {
137+
onData: (data: { labels: Record<string, string> }) => {
138+
if ("deno.diffsync.missing_hashes" in data.labels) {
139+
missingHashesPromise.resolve(
140+
JSON.parse(data.labels["deno.diffsync.missing_hashes"]),
141+
);
142+
sub.unsubscribe();
136143
}
137-
}
144+
},
145+
onError: (err: unknown) => {
146+
sub.unsubscribe();
147+
error(Deno.inspect(err));
148+
},
149+
onStopped: () => {
150+
sub.unsubscribe();
151+
},
152+
});
138153

139-
if ((Date.now() - s) >= 30 * 1000) {
140-
error(`Failed getting file hashes`, maybeHashesRes);
141-
}
142-
}
154+
const missingHashes = await missingHashesPromise.promise;
143155

144156
if (missingHashes.length > 0) {
145157
const skippedFilesCount = total - missingHashes.length;
146158

147159
if (skippedFilesCount > 0) {
148-
console.log(`Found ${skippedFilesCount} already uploaded files, which will be skipped from uploading`);
160+
console.log(
161+
`Found ${skippedFilesCount} already uploaded files, which will be skipped from uploading`,
162+
);
149163
}
150164

151165
const progress = new ProgressBar({
@@ -202,17 +216,21 @@ export async function publish(
202216
.pipeThrough(new TarStream())
203217
.pipeThrough(new CompressionStream("gzip"));
204218

205-
const resp = await authedFetch(deployUrl, `api/diffsync/${org}/${app}/${revisionId}`, {
206-
method: "POST",
207-
headers: {
208-
"x-meta": JSON.stringify({
209-
org,
210-
app,
211-
production: prod,
212-
}),
219+
const resp = await authedFetch(
220+
deployUrl,
221+
`api/diffsync/${org}/${app}/${revisionId}`,
222+
{
223+
method: "POST",
224+
headers: {
225+
"x-meta": JSON.stringify({
226+
org,
227+
app,
228+
production: prod,
229+
}),
230+
},
231+
body: tarball,
213232
},
214-
body: tarball,
215-
});
233+
);
216234

217235
await progress.stop();
218236

@@ -237,5 +255,4 @@ export async function publish(
237255
// TODO: print out the preview url
238256

239257
await writeConfig(configContent, rootPath, org, app);
240-
241258
}

0 commit comments

Comments
 (0)