Skip to content

Commit 3073b76

Browse files
committed
Introduce new hook
1 parent ae40946 commit 3073b76

File tree

3 files changed

+48
-16
lines changed

3 files changed

+48
-16
lines changed

src/getTasks.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import { join as pathJoin } from "path";
55
import { tryStat } from "./fs";
66
import {
77
isValidTask,
8+
ReleasableTaskList,
89
SharedOptions,
910
TaskList,
1011
WatchedTaskList,
12+
WorkerPluginContext,
1113
} from "./interfaces";
1214
import { FileDetails } from "./interfaces.js";
1315
import { CompiledSharedOptions, processSharedOptions } from "./lib";
@@ -102,7 +104,26 @@ export async function getTasks(
102104
export async function getTasksInternal(
103105
compiledSharedOptions: CompiledSharedOptions,
104106
taskPath: string,
105-
): Promise<WatchedTaskList> {
107+
): Promise<ReleasableTaskList> {
108+
return await compiledSharedOptions.middleware.run(
109+
"getTasks",
110+
{
111+
ctx: compiledSharedOptions,
112+
taskPath,
113+
taskList: {
114+
tasks: Object.create(null),
115+
release() {},
116+
},
117+
},
118+
_getTasksFromFilesystem,
119+
);
120+
}
121+
122+
async function _getTasksFromFilesystem(
123+
event: GraphileWorker.GetTasksEvent,
124+
): Promise<ReleasableTaskList> {
125+
const { ctx: compiledSharedOptions, taskList, taskPath } = event;
126+
106127
const { logger } = compiledSharedOptions;
107128
const pathStat = await tryStat(taskPath);
108129
if (!pathStat) {
@@ -111,7 +132,7 @@ export async function getTasksInternal(
111132
);
112133
}
113134

114-
const tasks: TaskList = Object.create(null);
135+
const tasks = taskList.tasks;
115136

116137
if (pathStat.isFile()) {
117138
// Try and require it
@@ -142,6 +163,11 @@ export async function getTasksInternal(
142163
await compiledSharedOptions.hooks.process("loadTaskFromFiles", event);
143164
const handler = event.handler;
144165
if (handler) {
166+
if (tasks[taskIdentifier]) {
167+
logger.warn(`Overwriting task identifier '${taskIdentifier}'`, {
168+
taskIdentifier,
169+
});
170+
}
145171
tasks[taskIdentifier] = handler;
146172
} else {
147173
logger.warn(
@@ -153,21 +179,11 @@ export async function getTasksInternal(
153179
}
154180
}
155181

156-
let released = false;
157-
return {
158-
tasks,
159-
compiledSharedOptions,
160-
release: () => {
161-
if (released) {
162-
return;
163-
}
164-
released = true;
165-
},
166-
};
182+
return taskList;
167183
}
168184

169185
async function getTasksFromDirectory(
170-
compiledSharedOptions: CompiledSharedOptions,
186+
compiledSharedOptions: WorkerPluginContext,
171187
collectedTaskPaths: Record<string, FileDetails[]>,
172188
taskPath: string,
173189
subpath: string[],
@@ -222,7 +238,7 @@ async function getTasksFromDirectory(
222238
}
223239

224240
function maybeAddFile(
225-
compiledSharedOptions: CompiledSharedOptions,
241+
compiledSharedOptions: WorkerPluginContext,
226242
collectedTaskPaths: Record<string, FileDetails[]>,
227243
subpath: string[],
228244
entry: string,

src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getTasks } from "./getTasks";
77
import {
88
FileDetails,
99
PromiseOrDirect,
10+
ReleasableTaskList,
1011
RunOnceOptions,
1112
SharedOptions,
1213
Task,
@@ -88,6 +89,12 @@ declare global {
8889
readonly scratchpad: Record<string, unknown>;
8990
}
9091

92+
interface GetTasksEvent {
93+
ctx: WorkerPluginContext;
94+
taskList: ReleasableTaskList;
95+
taskPath: string;
96+
}
97+
9198
interface PoolGracefulShutdownEvent {
9299
ctx: WorkerPluginContext;
93100
workerPool: WorkerPool;
@@ -431,6 +438,13 @@ declare global {
431438
*/
432439
migrate(event: GraphileWorker.MigrateEvent): PromiseOrDirect<void>;
433440

441+
/**
442+
* Called when loading the tasks
443+
*/
444+
getTasks(
445+
event: GraphileWorker.GetTasksEvent,
446+
): PromiseOrDirect<ReleasableTaskList>;
447+
434448
/**
435449
* Called when performing a graceful shutdown on a WorkerPool.
436450
*/

src/interfaces.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,11 @@ export type TaskList = {
314314
Task<any>;
315315
};
316316

317-
export interface WatchedTaskList {
317+
export interface ReleasableTaskList {
318318
tasks: TaskList;
319319
release: () => void;
320+
}
321+
export interface WatchedTaskList extends ReleasableTaskList {
320322
/** @internal */
321323
compiledSharedOptions: CompiledSharedOptions;
322324
}

0 commit comments

Comments
 (0)