Skip to content

Commit

Permalink
feat(core): Add support for ready hooks, and credentials overwrite en…
Browse files Browse the repository at this point in the history
…dpoint in workers
  • Loading branch information
netroy committed Aug 17, 2023
1 parent 914dd1f commit d6b2d8e
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions packages/cli/src/commands/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import { BaseCommand } from './BaseCommand';
import { ExecutionRepository } from '@db/repositories';
import { OwnershipService } from '@/services/ownership.service';
import { generateHostInstanceId } from '@/databases/utils/generators';
// eslint-disable-next-line import/no-extraneous-dependencies
import { IConfig } from '@oclif/config';
import type { ICredentialsOverwrite } from '@/Interfaces';
import { CredentialsOverwrites } from '@/CredentialsOverwrites';

export class Worker extends BaseCommand {
static description = '\nStarts a n8n worker';
Expand All @@ -46,12 +46,7 @@ export class Worker extends BaseCommand {

static jobQueue: JobQueue;

readonly uniqueInstanceId: string;

constructor(argv: string[], cmdConfig: IConfig) {
super(argv, cmdConfig);
this.uniqueInstanceId = generateHostInstanceId('worker');
}
readonly uniqueInstanceId = generateHostInstanceId('worker');

/**
* Stop n8n in a graceful way.
Expand Down Expand Up @@ -372,6 +367,45 @@ export class Worker extends BaseCommand {
process.exit(1);
}
});

let presetCredentialsLoaded = false;
const endpointPresetCredentials = config.getEnv('credentials.overwrite.endpoint');
if (endpointPresetCredentials !== '') {
// POST endpoint to set preset credentials
app.post(
`/${endpointPresetCredentials}`,
async (req: express.Request, res: express.Response) => {
if (!presetCredentialsLoaded) {
const body = req.body as ICredentialsOverwrite;

if (req.contentType !== 'application/json') {
ResponseHelper.sendErrorResponse(
res,
new Error(
'Body must be a valid JSON, make sure the content-type is application/json',
),
);
return;
}

CredentialsOverwrites().setData(body);

await this.loadNodesAndCredentials.generateTypesForFrontend();

presetCredentialsLoaded = true;

ResponseHelper.sendSuccessResponse(res, { success: true }, true, 200);
} else {
ResponseHelper.sendErrorResponse(
res,
new Error('Preset credentials can be set once'),
);
}
},
);
}

await this.externalHooks.run('worker.ready');
}

// Make sure that the process does not close
Expand Down

0 comments on commit d6b2d8e

Please sign in to comment.