Skip to content

Commit

Permalink
fix(upgrade-componentsjs): fix configs and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
adlerfaulkner committed Jul 20, 2023
1 parent d5b489e commit 1fa27d2
Show file tree
Hide file tree
Showing 20 changed files with 103 additions and 107 deletions.
1 change: 1 addition & 0 deletions .componentsignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"HttpErrorOptions",
"WinstonLogger",
"TypeOrmRepository",
"EntitySchemaOptions",
"ConfiguredJobOptions",
"MigrationInterface",
"BullQueueSettings",
Expand Down
2 changes: 0 additions & 2 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
"sor:config/http/handler/default.json",
"sor:config/http/static/default.json",
"sor:config/http/routes/default.json",
"sor:config/http/error-handler.json",
"sor:config/http/response-writer.json",
"sor:config/http/request-parser.json",
"sor:config/server/middleware/default.json",
"sor:config/server/server-factory/default.json",
"sor:config/storage/key-value/memory.json",
Expand Down
20 changes: 6 additions & 14 deletions config/jobs/bull-no-processing.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,13 @@
{
"@id": "urn:solid-on-rails:default:QueueAdapter",
"@type": "BullQueueAdapter",
"args_queues": [
{
"BullQueueAdapter:_args_queues_key": "default",
"BullQueueAdapter:_args_queues_value": {}
}
"queues": {
"default": {}
},
"jobs": [
{ "@type": "VoidJob" }
],
"args_jobs": [
{
"BullQueueAdapter:_args_jobs_key": "Void",
"BullQueueAdapter:_args_jobs_value": {
"@type": "VoidJob"
}
}
],
"args_queueProcessor": {
"queueProcessor": {
"@type": "VoidQueueProcessor"
}
},
Expand Down
20 changes: 6 additions & 14 deletions config/jobs/bull.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,13 @@
{
"@id": "urn:solid-on-rails:default:QueueAdapter",
"@type": "BullQueueAdapter",
"args_queues": [
{
"BullQueueAdapter:_args_queues_key": "default",
"BullQueueAdapter:_args_queues_value": {}
}
"queues": {
"default": {}
},
"jobs": [
{ "@type": "VoidJob" }
],
"args_jobs": [
{
"BullQueueAdapter:_args_jobs_key": "Void",
"BullQueueAdapter:_args_jobs_value": {
"@type": "VoidJob"
}
}
],
"args_queueProcessor": {
"queueProcessor": {
"@type": "BullQueueProcessor"
}
},
Expand Down
3 changes: 2 additions & 1 deletion config/jobs/scheduler/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"comment": "Logs a warning if the base URL changes.",
"@id": "urn:solid-on-rails:default:JobSchedulesInitializer",
"@type": "JobSchedulesInitializer",
"scheduler": { "@id": "urn:solid-on-rails:default:JobScheduler" }
"scheduler": { "@id": "urn:solid-on-rails:default:JobScheduler" },
"schedules": {}
},
{
"comment": "Ensure the preconfigured Job schedules get initialized.",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
"test:deploy": "chmod +x ./test/deploy/validate-package.sh && ./test/deploy/validate-package.sh",
"test:ts": "tsc -p test --noEmit",
"test:integration": "jest --coverageReporters text-summary -- test/integration",
"test:unit": "jest --config=./jest.coverage.config.js test/unit"
"test:unit": "jest --config=./jest.coverage.config.js test/unit",
"prepare": "npm run build"
},
"devDependencies": {
"@commitlint/cli": "^17.0.3",
Expand Down
1 change: 1 addition & 0 deletions src/jobs/Job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { ConfiguredJobOptions } from './JobOptions';
* A single asyncronous Job.
*/
export abstract class Job {
public abstract readonly name: string;
public readonly options: ConfiguredJobOptions = { queue: 'default' };

/**
Expand Down
2 changes: 2 additions & 0 deletions src/jobs/VoidJob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type { ConfiguredJobOptions } from './JobOptions';
* A job that does nothing.
*/
export class VoidJob extends Job {
public readonly name: string = 'Void';

public constructor(options?: ConfiguredJobOptions) {
super(options);
}
Expand Down
35 changes: 20 additions & 15 deletions src/jobs/adapter/BullQueueAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable tsdoc/syntax */
import type { Queue } from 'bull';
import Bull from 'bull';
import { getLoggerFor } from '../../logging/LogUtil';
Expand Down Expand Up @@ -53,13 +54,6 @@ export interface BullQueueSettings {
isSharedChildPool?: boolean;
}

export interface BullQueueAdapterArgs {
jobs: Record<string, Job>;
queues: Record<string, BullQueueSettings>;
redisConfig: RedisConfig;
queueProcessor: BullQueueProcessor;
}

const DEFAULT_BACKOFF_DELAY = 2000;

/**
Expand All @@ -68,22 +62,33 @@ const DEFAULT_BACKOFF_DELAY = 2000;
*/
export class BullQueueAdapter implements QueueAdapter {
protected readonly logger = getLoggerFor(this);
private readonly jobs: Record<string, Job>;
private readonly jobs: Job[];
private readonly queues: Record<string, Queue> = {};

public constructor(args: BullQueueAdapterArgs) {
this.jobs = args.jobs;

for (const [ queue, settings ] of Object.entries(args.queues)) {
/**
* @param jobs - The jobs which can be run.
* @param queues - The queues which jobs can be run on. @range {json}
* @param redisConfig - The configuration for redis to store job and queue details.
* @param queueProcessor - The queue processor.
*/
public constructor(
jobs: Job[],
queues: Record<string, BullQueueSettings>,
redisConfig: RedisConfig,
queueProcessor: BullQueueProcessor,
) {
this.jobs = jobs;

for (const [ queue, settings ] of Object.entries(queues)) {
this.queues[queue] = new Bull(
queue,
{
redis: args.redisConfig,
redis: redisConfig,
settings,
},
);
}
args.queueProcessor.processJobsOnQueues(this.queues, this.jobs, this);
queueProcessor.processJobsOnQueues(this.queues, this.jobs, this);
}

public async finalize(): Promise<void> {
Expand All @@ -98,7 +103,7 @@ export class BullQueueAdapter implements QueueAdapter {
data: Record<string, any> = {},
overrideOptions: Partial<JobOptions> = {},
): Promise<void> {
const job = this.jobs[jobName];
const job = this.jobs.find((jobIter): boolean => jobIter.name === jobName);
if (!job) {
throw new Error(`Job '${jobName}' is not defined`);
}
Expand Down
8 changes: 4 additions & 4 deletions src/jobs/processor/BullQueueProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ export class BullQueueProcessor implements QueueProcessor<Queue> {

public processJobsOnQueues(
queues: Record<string, Queue>,
jobs: Record<string, Job>,
jobs: Job[],
queueAdapter: QueueAdapter,
): void {
for (const queue of Object.keys(queues)) {
let isFirst = true;
for (const jobName of Object.keys(jobs)) {
queues[queue].process(jobName, isFirst ? 1 : 0, async(bullJob): Promise<void> =>
jobs[jobName].perform(bullJob.data, queueAdapter)) as any;
for (const job of jobs) {
queues[queue].process(job.name, isFirst ? 1 : 0, async(bullJob): Promise<void> =>
job.perform(bullJob.data, queueAdapter)) as any;
isFirst = false;
}
this.initializeQueueEvents(queues[queue]);
Expand Down
2 changes: 1 addition & 1 deletion src/jobs/processor/QueueProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Job } from '../Job';
export interface QueueProcessor<TQueue> {
processJobsOnQueues: (
queues: Record<string, TQueue>,
jobs: Record<string, Job>,
jobs: Job[],
queueAdapter: QueueAdapter,
) => void;
}
2 changes: 1 addition & 1 deletion src/jobs/processor/VoidQueueProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class VoidQueueProcessor implements QueueProcessor<any> {

public processJobsOnQueues(
queues: Record<string, any>,
jobs: Record<string, Job>,
jobs: Job[],
queueAdapter: QueueAdapter,
): void {
// Do nothing
Expand Down
23 changes: 11 additions & 12 deletions test/integration/ConfiguredJobs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,18 @@ describe('An http server with preconfigured jobs', (): void => {
bullConstructor = jest.fn().mockImplementation((): Bull.Queue => ({ process, add, on } as any));
(Bull as jest.Mock).mockImplementation(bullConstructor);

const adapter = new BullQueueAdapter({
queues: { default: {}},
jobs: {
Void: {
options: { queue: 'default' },
perform: jest.fn().mockImplementation(async({ value }: { value: string }): Promise<void> => {
jobValue = value;
}),
},
},
queueProcessor: new BullQueueProcessor(),
const adapter = new BullQueueAdapter(
[{
name: 'Void',
options: { queue: 'default' },
perform: jest.fn().mockImplementation(async({ value }: { value: string }): Promise<void> => {
jobValue = value;
}),
}],
{ default: {}},
redisConfig,
});
new BullQueueProcessor(),
);

const instances = await instantiateFromConfig(
'urn:solid-on-rails:test:Instances',
Expand Down
23 changes: 16 additions & 7 deletions test/integration/config/configured-jobs.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,10 @@

"sor:config/http/handler/no-routes.json",
"sor:config/http/static/default.json",

"sor:config/http/error-handler.json",
"sor:config/http/response-writer.json",
"sor:config/http/request-parser.json",


"sor:config/server/middleware/default.json",
"sor:config/server/server-factory/default.json",

"sor:config/jobs/scheduler/default.json",

"sor:config/storage/key-value/memory.json",

"sor:config/util/variables/default.json",
Expand All @@ -42,8 +36,15 @@
]
},
{
"@id": "urn:solid-on-rails:default:JobScheduler",
"@type": "AdapterBasedScheduler",
"adapter": { "@id": "urn:solid-on-rails:default:QueueAdapter" }
},
{
"comment": "Logs a warning if the base URL changes.",
"@id": "urn:solid-on-rails:default:JobSchedulesInitializer",
"@type": "JobSchedulesInitializer",
"scheduler": { "@id": "urn:solid-on-rails:default:JobScheduler" },
"schedules": {
"Void": {
"jobName": "Void",
Expand All @@ -53,6 +54,14 @@
}
}
}
},
{
"comment": "Ensure the preconfigured Job schedules get initialized.",
"@id": "urn:solid-on-rails:default:ParallelInitializer",
"@type": "ParallelHandler",
"handlers": [
{ "@id": "urn:solid-on-rails:default:JobSchedulesInitializer" }
]
}
]
}
2 changes: 0 additions & 2 deletions test/integration/config/data-mapper.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
"sor:config/http/handler/default.json",
"sor:config/http/static/default.json",
"sor:config/http/routes/default.json",
"sor:config/http/error-handler.json",
"sor:config/http/response-writer.json",
"sor:config/http/request-parser.json",
"sor:config/server/middleware/default.json",
"sor:config/server/server-factory/default.json",
"sor:config/storage/key-value/memory.json",
Expand Down
2 changes: 0 additions & 2 deletions test/integration/config/migration.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
"sor:config/http/handler/default.json",
"sor:config/http/static/default.json",
"sor:config/http/routes/default.json",
"sor:config/http/error-handler.json",
"sor:config/http/response-writer.json",
"sor:config/http/request-parser.json",
"sor:config/server/middleware/default.json",
"sor:config/server/server-factory/default.json",
"sor:config/storage/key-value/memory.json",
Expand Down
1 change: 0 additions & 1 deletion test/integration/config/websocket-server.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"sor:config/app/variables/default.json",
"sor:config/app/path/default.json",
"sor:config/http/static/default.json",
"sor:config/http/error-handler.json",
"sor:config/server/server-factory/default.json",
"sor:config/storage/key-value/memory.json",
"sor:config/util/variables/default.json",
Expand Down
Loading

0 comments on commit 1fa27d2

Please sign in to comment.