Skip to content

Commit

Permalink
websites-integration: polish
Browse files Browse the repository at this point in the history
  • Loading branch information
cometkim committed Jun 7, 2024
1 parent 930ef7d commit 9c0ddb3
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 38 deletions.
8 changes: 4 additions & 4 deletions _workers/websites-integration/deployment-awaiter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ if (!initResponse.ok) {

console.log(`Deployment(id: ${initData.id}) initialized`);

let state = initData.state;
let bound = false;

const checkUrl = new URL(initData.check_url);
const artifactUrl = new URL(initData.artifact_url);

const timeout = Number.parseInt(values.timeout);

let bound = false;

for await (const startTime of setInterval(5000, Date.now())) {
if (Date.now() - startTime >= timeout) {
console.error(`Timeout exceeded (${prettyMilliseconds(timeout)})`);
Expand All @@ -82,7 +82,7 @@ for await (const startTime of setInterval(5000, Date.now())) {
process.exit(1);
}

state = data.state;
const state = data.state;
if (state.type === 'IDLE') {
throw new Error('invariant');
}
Expand Down
27 changes: 11 additions & 16 deletions _workers/websites-integration/functions/$lib/objects/Deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,21 @@ export class Deployment extends DurableObject<Env> {

async #next(nextState: DeploymentState) {
await this.#ctx.storage.put<DeploymentState>('state', nextState);
return true;
}

async getCurrentState() {
const state = await this.#ctx.storage.get<DeploymentState>('state');
if (!state) {
throw new Error('not initialized');
throw new Error('Not initialized');
}
return state;
}

async init(params: DeploymentParameters, bindUrl: string, callbackUrl: string) {
const state = await this.getCurrentState();
if (state.type !== 'IDLE') {
throw new Error('invariant');
return false;
}

const { status: actionStatus } = await this.#octokit.actions.createWorkflowDispatch({
Expand All @@ -98,20 +99,15 @@ export class Deployment extends DurableObject<Env> {
params,
};

await this.#next(nextState);

return {
state: nextState,
};
return await this.#next(nextState);
}

async cancel() {
const state = await this.getCurrentState();
if (state.type !== 'IN_PROGRESS') {
return;
if (state.type === 'DONE') {
return false;
}

this.#next({
return await this.#next({
...state,
type: 'DONE',
status: 'cancelled',
Expand All @@ -121,18 +117,17 @@ export class Deployment extends DurableObject<Env> {
async bind(runId: string) {
const state = await this.getCurrentState();
if (!state.runId) {
this.#next({ ...state, runId });
return await this.#next({ ...state, runId });
}
return false;
}

async finish(result: DeploymentResult) {
const state = await this.getCurrentState();

if (state.type === 'DONE') {
throw new Error(`The deployment has already finished with status: ${state.status}`);
return false;
}

await this.#next({
return await this.#next({
type: 'DONE',
runId: result.run_id,
status: result.status,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,28 @@ export const onRequestPost: PagesFunction<Env, 'id'> = async (context) => {
}

try {
await stub.finish(result);
const ok = await stub.finish(result);
if (!ok) {
return json(
{
id: paramId,
message: 'Callback failed, perhaps the deployment has already been finished',
},
{ status: 400 },
);
}
} catch (error) {
console.error(error);

return json(
{
id: paramId,
message: 'Callback failed, perhaps the deployment has already been finished',
message: 'Internal error',
error,
},
{ status: 400 },
{ status: 500 },
);
}

return json(null, { status: 204 });
return json({ id: paramId, message: 'The result has been reported successfully' });
};
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const onRequestGet: PagesFunction<Env, 'id'> = async (context) => {
state = await stub.getCurrentState();
} catch (error) {
console.error(error);
return json({ id: paramId, message: 'Invalid state' }, { status: 500 });
return json({ id: paramId, message: 'Failed to get state', error }, { status: 500 });
}

if (state.type === 'DONE' && state.artifactName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const onRequestGet: PagesFunction<Env, 'id'> = async (context) => {
return json({ id: paramId, state });
} catch (error) {
console.error(error);
return json({ id: paramId, message: 'Invalid state' }, { status: 500 });
return json({ id: paramId, message: 'Failed to get state', error }, { status: 500 });
}
};

Expand All @@ -35,11 +35,8 @@ export const onRequestPost: PagesFunction<Env, 'id'> = async (context) => {
let deploymentId: DurableObjectId;
try {
deploymentId = context.env.DEPLOYMENT.idFromString(paramId);
} catch (err) {
return json(
{ id: paramId, message: 'Invalid ID format' },
{ status: 400, statusText: 'Invalid ID format' },
);
} catch {
return json({ id: paramId, message: 'Invalid ID format' }, { status: 400 });
}

let stub: DurableObjectStub<Deployment>;
Expand All @@ -51,11 +48,11 @@ export const onRequestPost: PagesFunction<Env, 'id'> = async (context) => {

try {
await stub.bind(params.run_id);
return json({ id: paramId, run_id: params.run_id, message: 'Job is successfully bound' });
return json({ id: paramId, run_id: params.run_id, message: 'Job bound successfully ' });
} catch (error) {
console.error(error);
return json(
{ id: paramId, run_id: params.run_id, message: 'Failed to bind job' },
{ id: paramId, run_id: params.run_id, message: 'Failed to bind job', error },
{ status: 500 },
);
}
Expand Down
15 changes: 10 additions & 5 deletions _workers/websites-integration/functions/deployments/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,22 @@ export const onRequestPost: PagesFunction<Env> = async (context) => {
const stub = context.env.DEPLOYMENT.get(id);

try {
const { state } = await stub.init(params, bindUrl.toString(), callbackUrl.toString());
const ok = await stub.init(params, bindUrl.toString(), callbackUrl.toString());
if (!ok) {
return json({ id: id.toString(), message: 'Already initialized' }, { status: 400 });
}
return json({
state,
id: id.toString(),
bind_url: bindUrl.toString(),
check_url: checkUrl.toString(),
callback_url: callbackUrl.toString(),
artifact_url: artifactUrl.toString(),
});
} catch (err) {
console.error(err);
return json({ message: 'Failed to initialize a deployment' }, { status: 500 });
} catch (error) {
console.error(error);
return json(
{ id: id.toString(), message: 'Failed to create a deployment', error },
{ status: 500 },
);
}
};

0 comments on commit 9c0ddb3

Please sign in to comment.