Skip to content

Commit

Permalink
Merge pull request #121 from basehub-ai/jb/fix-process-not-ending
Browse files Browse the repository at this point in the history
Jb/fix process not ending
  • Loading branch information
julianbenegas authored Oct 30, 2024
2 parents 8ad2825 + 2cc6217 commit b0823a7
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 35 deletions.
61 changes: 61 additions & 0 deletions packages/basehub/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,66 @@
# basehub

## 7.5.20

### Patch Changes

- d670cea: Expose cache key generator function
- 40d4c90: simulate stuck op
- 40d4c90: debug: remvove early exit
- 40d4c90: try to force repro
- 40d4c90: store pid again after cleaning up the parent dir
- d670cea: more logs
- d670cea: logs to debug process which keeps running
- 40d4c90: kill existing process if found

## 7.5.20-canary.7

### Patch Changes

- store pid again after cleaning up the parent dir

## 7.5.20-canary.6

### Patch Changes

- kill existing process if found

## 7.5.20-canary.5

### Patch Changes

- debug: remvove early exit

## 7.5.20-canary.4

### Patch Changes

- simulate stuck op

## 7.5.20-canary.3

### Patch Changes

- try to force repro

## 7.5.20-canary.2

### Patch Changes

- more logs

## 7.5.20-canary.1

### Patch Changes

- logs to debug process which keeps running

## 7.5.20-canary.0

### Patch Changes

- Expose cache key generator function

## 7.5.19

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/basehub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "basehub",
"description": "A very fast Headless CMS.",
"author": "JB <[email protected]>",
"version": "7.5.19",
"version": "7.5.20",
"license": "MIT",
"repository": "basehub-ai/basehub",
"bugs": "https://github.com/basehub-ai/basehub/issues",
Expand Down
78 changes: 45 additions & 33 deletions packages/basehub/src/bin/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { z } from "zod";
import { createHash } from "crypto";
import { ResolvedRef } from "../common-types";
import { ensureCrossPlatformTsImport } from "./util/cross-platform-ts-imports";
import { ensureSingleInstance, storePid } from "./util/ensure-single-instance";

const buildManifestSchema = z.object({
generatedAt: z.string(),
Expand All @@ -35,6 +36,42 @@ export const main = async (
const now = Date.now();
let previousResolvedRef: ResolvedRef | null = null;

const options: Options = {
token: args["--token"],
prefix: args["--env-prefix"],
output: args["--output"],
draft: args["--draft"],
apiVersion: args["--api-version"],
...(opts?.forceDraft && { draft: true }),
};

const basehubModulePath = resolvePkg("basehub") as string;

if (!basehubModulePath) {
throw new Error(
"basehub package not found in node_modules. If this issue persists, please raise an issue on the `basehub-ai/basehub` repository."
);
}

const { output } = await getStuffFromEnv({ ...options, previousResolvedRef });

let shouldAppendToGitIgnore = "";
let pathArgs: string[] = [];
if (output === "node_modules") {
// old default
pathArgs = ["node_modules", "basehub", "dist", "generated-client"];
} else if (output) {
pathArgs = [output];
} else {
// default
pathArgs = [".basehub"];
shouldAppendToGitIgnore = ".basehub";
}

const basehubOutputPath = path.resolve(process.cwd(), ...pathArgs);

ensureSingleInstance(basehubOutputPath);

async function generateSDK(
silent: boolean,
previousSchemaHash: string
Expand All @@ -45,20 +82,10 @@ export const main = async (
}> {
logIfNotSilent(silent, "🪄 Generating...");

const options: Options = {
token: args["--token"],
prefix: args["--env-prefix"],
output: args["--output"],
draft: args["--draft"],
apiVersion: args["--api-version"],
...(opts?.forceDraft && { draft: true }),
};

const {
url,
headers,
draft,
output,
gitBranch,
gitCommitSHA,
gitBranchDeploymentURL,
Expand All @@ -67,29 +94,6 @@ export const main = async (
token,
} = await getStuffFromEnv({ ...options, previousResolvedRef });

const basehubModulePath = resolvePkg("basehub");

if (!basehubModulePath) {
throw new Error(
"basehub package not found in node_modules. If this issue persists, please raise an issue on the `basehub-ai/basehub` repository."
);
}

let shouldAppendToGitIgnore = "";
let pathArgs: string[] = [];
if (output === "node_modules") {
// old default
pathArgs = ["node_modules", "basehub", "dist", "generated-client"];
} else if (output) {
pathArgs = [output];
} else {
// default
pathArgs = [".basehub"];
shouldAppendToGitIgnore = ".basehub";
}

const basehubOutputPath = path.resolve(process.cwd(), ...pathArgs);

if (!silent) {
logInsideBox([
`🎫 SDK Version: ${opts.version}`,
Expand Down Expand Up @@ -160,6 +164,8 @@ export const main = async (
fs.mkdirSync(basehubOutputPath, { recursive: true });
}

storePid(basehubOutputPath);

const { preventedClientGeneration, schemaHash } = await generate({
endpoint: url.toString(),
headers: {
Expand Down Expand Up @@ -660,6 +666,12 @@ R extends Omit<MutationGenqlSelection, "transaction" | "transactionAwaitable"> &
process.on(signal, () => {
onProcessEndCallbacks.forEach((cb) => cb());
process.exit(0);

// If we're still alive after process.exit(0), force kill ourselves
console.log(
`[${process.pid}] BaseHub process still alive after exit(), forcing SIGKILL...`
);
process.kill(process.pid, "SIGKILL");
});
});

Expand Down
25 changes: 25 additions & 0 deletions packages/basehub/src/bin/util/ensure-single-instance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import path from "path";
import fs from "fs";

export function ensureSingleInstance(output: string) {
const pidFile = path.join(output, ".pid");

if (fs.existsSync(pidFile)) {
const pid = Number(fs.readFileSync(pidFile, "utf8"));
try {
process.kill(pid, 0); // Check if process exists
process.kill(pid); // Kill it if it does
} catch (e) {
// Process doesn't exist
}
}

return () => {
// cleanup if needed
fs.unlinkSync(pidFile);
};
}

export function storePid(output: string) {
fs.writeFileSync(path.join(output, ".pid"), process.pid.toString());
}
70 changes: 70 additions & 0 deletions playground/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,75 @@
# playground

## 0.0.162

### Patch Changes

- Updated dependencies [d670cea]
- Updated dependencies [40d4c90]
- Updated dependencies [40d4c90]
- Updated dependencies [40d4c90]
- Updated dependencies [40d4c90]
- Updated dependencies [d670cea]
- Updated dependencies [d670cea]
- Updated dependencies [40d4c90]
- [email protected]

## 0.0.162-canary.7

### Patch Changes

- Updated dependencies
- [email protected]

## 0.0.162-canary.6

### Patch Changes

- Updated dependencies
- [email protected]

## 0.0.162-canary.5

### Patch Changes

- Updated dependencies
- [email protected]

## 0.0.162-canary.4

### Patch Changes

- Updated dependencies
- [email protected]

## 0.0.162-canary.3

### Patch Changes

- Updated dependencies
- [email protected]

## 0.0.162-canary.2

### Patch Changes

- Updated dependencies
- [email protected]

## 0.0.162-canary.1

### Patch Changes

- Updated dependencies
- [email protected]

## 0.0.162-canary.0

### Patch Changes

- Updated dependencies
- [email protected]

## 0.0.161

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion playground/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "playground",
"private": true,
"version": "0.0.161",
"version": "0.0.162",
"scripts": {
"dev": "basehub dev & next dev --port 3003",
"build": "basehub && next build",
Expand Down

0 comments on commit b0823a7

Please sign in to comment.