Skip to content

Commit

Permalink
Merge pull request #80 from defer-run/add-cancel-execution
Browse files Browse the repository at this point in the history
  • Loading branch information
charlypoly authored Jul 14, 2023
2 parents 60125d0 + c307b0f commit 2a38c17
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-candles-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@defer/client": minor
---

Remove running state
15 changes: 15 additions & 0 deletions .changeset/brave-jars-yawn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
"@defer/client": minor
---

Add cancel execution:

```ts
import { cancelExecution } from "@defer/client";

// ...

const { id } = await cancelExecution(executionId);

// ...
```
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jobs:
name: "Node ${{matrix.node_version}}"
runs-on: "ubuntu-latest"
strategy:
fail-fast: false
matrix:
node-version: [16, 18, 20]
steps:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,4 @@
"directory": "dist",
"access": "public"
}
}
}
13 changes: 13 additions & 0 deletions src/cancelExecution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {
cancelExecution as cancelExecution2,
CancelExecutionResponse,
} from "./client.js";
import { __httpClient } from "./index.js";

export async function cancelExecution(
id: string
): Promise<CancelExecutionResponse> {
if (__httpClient) return cancelExecution2(__httpClient, { id });

return {};
}
21 changes: 15 additions & 6 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ export interface FetchExecutionRequest {
id: string;
}

export type ExecutionState =
| "running"
| "created"
| "failed"
| "succeed"
| "started";
export type ExecutionState = "created" | "failed" | "succeed" | "started";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export interface FetchExecutionResponse<R = any> {
Expand All @@ -37,6 +32,20 @@ export type PoolExecutionRequest = FetchExecutionRequest;

export type PoolExecutionResponse = FetchExecutionResponse;

export type CancelExecutionRequest = FetchExecutionRequest;

export interface CancelExecutionResponse {}

export function cancelExecution(
client: HTTPClient,
request: CancelExecutionRequest
): Promise<CancelExecutionResponse> {
return client<CancelExecutionResponse>(
"POST",
`/public/v1/executions/${request.id}/cancel`
);
}

export function enqueueExecution(
client: HTTPClient,
request: EnqueueExecutionRequest
Expand Down
5 changes: 2 additions & 3 deletions src/getExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ export async function getExecution(
if (response)
return Promise.resolve({
...response,
// TODO: bump a major and drop `running` state (invalid API state)
state: response.state === "started" ? "running" : response.state,
state: response.state,
});

throw new APIError("execution not found", "");
throw new APIError("execution not found", "not found");
}
9 changes: 5 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { DeferError } from "./errors.js";
import { HTTPClient, makeHTTPClient } from "./httpClient.js";

export { getExecution } from "./getExecution.js";
export { cancelExecution } from "./cancelExecution.js";

interface Options {
accessToken?: string;
Expand Down Expand Up @@ -251,7 +252,7 @@ export const defer: Defer = (fn, options) => {
console.log(`[defer.run][${fn.name}] defer ignore, no token found.`);

const id = randomUUID();
__database.set(id, { id: id, state: "running" });
__database.set(id, { id: id, state: "started" });
execLocally(id, fn, functionArguments);
return { id };
};
Expand Down Expand Up @@ -334,7 +335,7 @@ export const delay: DeferDelay = (deferFn, delay) => {
console.log(`[defer.run][${fn.name}] defer ignore, no token found.`);

const id = randomUUID();
__database.set(id, { id: id, state: "running" });
__database.set(id, { id: id, state: "started" });
execLocally(id, fn, functionArguments);
return { id };
};
Expand Down Expand Up @@ -403,7 +404,7 @@ export const addMetadata: DeferAddMetadata = (deferFn, metadata) => {
console.log(`[defer.run][${fn.name}] defer ignore, no token found.`);

const id = randomUUID();
__database.set(id, { id: id, state: "running" });
__database.set(id, { id: id, state: "started" });
execLocally(id, fn, functionArguments);
return { id };
};
Expand Down Expand Up @@ -455,7 +456,7 @@ export const awaitResult: DeferAwaitResult =
response = await waitExecutionResult(__httpClient, { id: id });
} else {
const id = randomUUID();
__database.set(id, { id: id, state: "running" });
__database.set(id, { id: id, state: "started" });
response = await execLocally(id, fn, functionArguments);
}

Expand Down
2 changes: 1 addition & 1 deletion src/next/useDeferRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export const useDeferRoute = <
return [
execute,
{
loading: !!status && ["started", "created", "running"].includes(status),
loading: !!status && ["started", "created"].includes(status),
result,
error,
},
Expand Down
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default "1.7.2";
export default "unknow";
2 changes: 1 addition & 1 deletion tests/awaitResult.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe("awaitResult(deferFn)", () => {
})
// second `/runs/:id` call
.mockImplementationOnce(() => {
return { id: "1", state: "running" };
return { id: "1", state: "started" };
})
// third `/runs/:id` call
.mockImplementationOnce(() => {
Expand Down

0 comments on commit 2a38c17

Please sign in to comment.