Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Read more:

## v0.18.0

- For consistency, `cosmiconfig` is no longer supported for configuration; use
`graphile.config.ts` instead. (This release will throw errors if you're using
cosmisconfig, in later releases cosmiconfigs will not be detected.)
- Now published as pure ESM, but worry not as unflagged require(ESM) is now
enabled by default in
[Node 20.19.0+](https://nodejs.org/pt-br/blog/release/v20.19.0),
Expand Down
96 changes: 44 additions & 52 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,46 @@ import { MINUTE, SECOND } from "./cronConstants.ts";
import { defaultLogger } from "./logger.ts";

const cosmiconfigResult = cosmiconfigSync("graphile-worker").search();
const cosmiconfig = cosmiconfigResult?.config;

if (cosmiconfigResult && !cosmiconfigResult.isEmpty) {
const { config: cosmiconfig, filepath } = cosmiconfigResult;
if (
cosmiconfig.schema != null ||
cosmiconfig.pollInterval != null ||
cosmiconfig.maxPoolSize != null
) {
console.error(
`Cosmiconfig configuration found in ${filepath}. cosmiconfig is no longer supported, please switch to graphile.config.ts - an equivalent configuration might look like:`,
);
console.error();
console.error("```ts");
console.error("// graphile.config.ts");
console.error('import { WorkerPreset } from "graphile-worker";');
console.error("");
console.error("const preset: GraphileConfig.Preset = {");
console.error(" extends: [WorkerPreset],");
console.error(" worker: {");
if (cosmiconfig.schema != null) {
console.error(` schema: ${JSON.stringify(cosmiconfig.schema)},`);
}
if (cosmiconfig.pollInterval != null) {
console.error(
` pollInterval: ${JSON.stringify(cosmiconfig.pollInterval)},`,
);
}
if (cosmiconfig.maxPoolSize != null) {
console.error(
` maxPoolSize: ${JSON.stringify(cosmiconfig.maxPoolSize)},`,
);
}
console.error(" },");
console.error("};");
console.error("");
console.error("export default preset;");
console.error("```");
process.exit(1);
}
}

/**
* Defaults to use for various options throughout the codebase, sourced from
Expand All @@ -13,18 +52,10 @@ const cosmiconfig = cosmiconfigResult?.config;
export const makeWorkerPresetWorkerOptions = () =>
({
connectionString: process.env.DATABASE_URL,
schema:
process.env.GRAPHILE_WORKER_SCHEMA ||
enforceStringOrUndefined("schema", cosmiconfig?.schema) ||
"graphile_worker",
pollInterval:
enforceNumberOrUndefined("pollInterval", cosmiconfig?.pollInterval) ||
2000,
concurrentJobs:
enforceNumberOrUndefined("concurrentJobs", cosmiconfig?.concurrentJobs) ||
1,
maxPoolSize:
enforceNumberOrUndefined("maxPoolSize", cosmiconfig?.maxPoolSize) || 10,
schema: process.env.GRAPHILE_WORKER_SCHEMA || "graphile_worker",
pollInterval: 2000,
concurrentJobs: 1,
maxPoolSize: 10,
preparedStatements: true as boolean,
crontabFile: `${process.cwd()}/crontab`,
taskDirectory: `${process.cwd()}/tasks`,
Expand All @@ -35,42 +66,3 @@ export const makeWorkerPresetWorkerOptions = () =>
gracefulShutdownAbortTimeout: 5 * SECOND,
useNodeTime: false,
}) satisfies GraphileConfig.WorkerOptions;

function enforceStringOrUndefined(
keyName: string,
str: unknown,
): string | undefined {
if (typeof str === "string") {
return str;
} else if (!str) {
return undefined;
} else {
throw new Error(
`Expected '${keyName}' to be a string (or not set), but received ${typeof str}`,
);
}
}

function enforceNumberOrUndefined(
keyName: string,
nr: unknown,
): number | undefined {
if (typeof nr === "number") {
return nr;
} else if (typeof nr === "string") {
const val = parseFloat(nr);
if (isFinite(val)) {
return val;
} else {
throw new Error(
`Expected '${keyName}' to be a number (or not set), but received ${nr}`,
);
}
} else if (!nr) {
return undefined;
} else {
throw new Error(
`Expected '${keyName}' to be a number (or not set), but received ${typeof nr}`,
);
}
}
Loading