Skip to content

Commit

Permalink
fix: fix all eslint errors
Browse files Browse the repository at this point in the history
Signed-off-by: Ute Weiss <[email protected]>
  • Loading branch information
weissu42 committed Aug 27, 2021
1 parent 97b2038 commit 26f45ca
Show file tree
Hide file tree
Showing 26 changed files with 512 additions and 522 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ module.exports = {
'@typescript-eslint/promise-function-async': 'error',
'@typescript-eslint/quotes': 'off',
'@typescript-eslint/restrict-plus-operands': 'error',
'@typescript-eslint/no-unnecessary-condition': 'error',
'@typescript-eslint/no-unnecessary-condition': ['error', {}],
'@typescript-eslint/semi': ['off', null],
'@typescript-eslint/strict-boolean-expressions': [
'error',
Expand All @@ -107,7 +107,7 @@ module.exports = {
'brace-style': ['off', 'off'],
'comma-dangle': 'off',
complexity: 'off',
'constructor-super': 'error',
'constructor-super': 'off',
'eol-last': 'off',
eqeqeq: ['error', 'smart'],
'guard-for-in': 'error',
Expand Down
817 changes: 397 additions & 420 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/executor/JobExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class JobExecutor {
const data = await this.handler();
result = {
status: ExecutionStatus.finished,
handlerResult: data !== undefined ? data : 'finished',
handlerResult: data ?? 'finished',
};
} catch (e) {
this.logger.error('job failed', MomoErrorType.executeJob, { name: jobEntity.name }, e);
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { MongoSchedule } from './schedule/MongoSchedule';
export { connect, MomoConnectionOptions } from './connect';
export { MomoError } from './logging/error/MomoError';
export { momoError } from './logging/error/MomoError';
export { MomoErrorType } from './logging/error/MomoErrorType';
export { MomoEvent, MomoErrorEvent, MomoEventData } from './logging/MomoEvents';
export { MomoJob } from './job/MomoJob';
Expand Down
2 changes: 1 addition & 1 deletion src/job/MomoJob.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type Handler = () => Promise<string | void> | string | void;
export type Handler = () => Promise<string | undefined | void> | string | undefined | void;

export interface MomoJob {
immediate?: boolean;
Expand Down
6 changes: 3 additions & 3 deletions src/job/MomoJobBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ export class MomoJobBuilder {
}

build(): MomoJob {
if (!this.momoJob.name) {
if (this.momoJob.name === undefined) {
throw Error('Error: Job must have a specified name');
}

if (!this.momoJob.interval) {
if (this.momoJob.interval === undefined) {
throw Error('Error: Job must have a specified interval');
}

if (!this.momoJob.handler) {
if (this.momoJob.handler === undefined) {
throw Error('Error: Job must have a specified handler');
}

Expand Down
4 changes: 2 additions & 2 deletions src/job/findLatest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { JobEntity } from '../repository/JobEntity';

function compareLastFinished(left: JobEntity, right: JobEntity): JobEntity {
const r = right.executionInfo?.lastFinished;
if (!r) return left;
if (r === undefined) return left;
const l = left.executionInfo?.lastFinished;
if (!l) return right;
if (l === undefined) return right;
return DateTime.fromISO(r).toMillis() > DateTime.fromISO(l).toMillis() ? right : left;
}

Expand Down
12 changes: 6 additions & 6 deletions src/job/validate.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import humanInterval from 'human-interval';
import { Job } from './Job';
import { MomoError } from '../logging/error/MomoError';
import { momoError } from '../logging/error/MomoError';
import { MomoErrorType } from '../logging/error/MomoErrorType';
import { Logger } from '../logging/Logger';

export function validate({ name, interval, concurrency, maxRunning }: Job, logger?: Logger): boolean {
if (maxRunning < 0) {
logger?.error('job cannot be defined', MomoErrorType.defineJob, { name, maxRunning }, MomoError.invalidMaxRunning);
logger?.error('job cannot be defined', MomoErrorType.defineJob, { name, maxRunning }, momoError.invalidMaxRunning);
return false;
}

Expand All @@ -15,7 +15,7 @@ export function validate({ name, interval, concurrency, maxRunning }: Job, logge
'job cannot be defined',
MomoErrorType.defineJob,
{ name, concurrency },
MomoError.invalidConcurrency
momoError.invalidConcurrency
);
return false;
}
Expand All @@ -25,14 +25,14 @@ export function validate({ name, interval, concurrency, maxRunning }: Job, logge
'job cannot be defined',
MomoErrorType.defineJob,
{ name, concurrency, maxRunning },
MomoError.invalidConcurrency
momoError.invalidConcurrency
);
return false;
}

const parsedInterval = humanInterval(interval);
if (!parsedInterval || isNaN(parsedInterval) || parsedInterval <= 0) {
logger?.error('job cannot be defined', MomoErrorType.defineJob, { name, interval }, MomoError.nonParsableInterval);
if (parsedInterval === undefined || isNaN(parsedInterval) || parsedInterval <= 0) {
logger?.error('job cannot be defined', MomoErrorType.defineJob, { name, interval }, momoError.nonParsableInterval);
return false;
}

Expand Down
1 change: 1 addition & 0 deletions src/logging/LogEmitter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import EventEmitter from 'events';
import TypedEmitter from 'typed-emitter';

import { MomoEvents } from './MomoEvents';
import { debug, error, Logger } from './Logger';

Expand Down
2 changes: 1 addition & 1 deletion src/logging/error/MomoError.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const MomoError = {
export const momoError = {
nonParsableInterval: new Error('non-parsable job interval'),
invalidConcurrency: new Error('concurrency must be at least 1'),
invalidMaxRunning: new Error('maxRunning must be at least 0'),
Expand Down
17 changes: 8 additions & 9 deletions src/repository/JobRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,27 @@ export class JobRepository extends MongoRepository<JobEntity> {
async updateJob(name: string, update: Partial<JobEntity>): Promise<void> {
const savedJobs = await this.find({ name });

if (savedJobs.length === 0) {
if (savedJobs[0] === undefined) {
return;
}
const updatedJob = merge(savedJobs, update);
const updatedJob = merge(savedJobs[0], update);

await this.save(updatedJob);
}
}

function merge(savedJobs: JobEntity[], { interval, concurrency, maxRunning, executionInfo }: Partial<JobEntity>) {
const updatedJob = savedJobs[0];
function merge(savedJob: JobEntity, { interval, concurrency, maxRunning, executionInfo }: Partial<JobEntity>) {
if (interval !== undefined) {
updatedJob.interval = interval;
savedJob.interval = interval;
}
if (concurrency !== undefined) {
updatedJob.concurrency = concurrency;
savedJob.concurrency = concurrency;
}
if (maxRunning !== undefined) {
updatedJob.maxRunning = maxRunning;
savedJob.maxRunning = maxRunning;
}
if (executionInfo !== undefined) {
updatedJob.executionInfo = executionInfo;
savedJob.executionInfo = executionInfo;
}
return updatedJob;
return savedJob;
}
8 changes: 4 additions & 4 deletions src/schedule/Schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Schedule extends LogEmitter {
* Returns 0 if no job with this name is scheduled.
*/
public getUnexpectedErrorCount(name?: string): number {
if (name) {
if (name !== undefined) {
return this.jobSchedulers[name]?.getUnexpectedErrorCount() ?? 0;
}
return sum(Object.values(this.jobSchedulers).map((jobScheduler) => jobScheduler.getUnexpectedErrorCount()));
Expand Down Expand Up @@ -87,7 +87,7 @@ export class Schedule extends LogEmitter {
*/
public async start(): Promise<void> {
this.logger.debug('start all jobs', { count: this.count() });
await Promise.all(Object.values(this.jobSchedulers).map((jobScheduler) => jobScheduler.start()));
await Promise.all(Object.values(this.jobSchedulers).map(async (jobScheduler) => jobScheduler.start()));
}

/**
Expand Down Expand Up @@ -134,7 +134,7 @@ export class Schedule extends LogEmitter {
public async stop(): Promise<void> {
this.logger.debug('stop all jobs', { count: this.count() });
try {
await Promise.all(Object.values(this.jobSchedulers).map((jobScheduler) => jobScheduler.stop()));
await Promise.all(Object.values(this.jobSchedulers).map(async (jobScheduler) => jobScheduler.stop()));
} catch (error) {
this.logger.error('message failed to stop jobs', MomoErrorType.stopJob, { count: this.count() }, error);
}
Expand Down Expand Up @@ -199,7 +199,7 @@ export class Schedule extends LogEmitter {
*/
public async list(): Promise<MomoJobDescription[]> {
return (
await Promise.all(Object.values(this.jobSchedulers).map((jobScheduler) => jobScheduler.getJobDescription()))
await Promise.all(Object.values(this.jobSchedulers).map(async (jobScheduler) => jobScheduler.getJobDescription()))
).filter((jobDescription): jobDescription is MomoJobDescription => jobDescription !== undefined);
}

Expand Down
15 changes: 8 additions & 7 deletions src/scheduler/JobScheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Job } from '../job/Job';
import { JobExecutor } from '../executor/JobExecutor';
import { setIntervalWithDelay, TimeoutHandle } from './setIntervalWithDelay';
import { calculateDelay } from './calculateDelay';
import { MomoError } from '../logging/error/MomoError';
import { momoError } from '../logging/error/MomoError';
import { MomoErrorType } from '../logging/error/MomoErrorType';
import { getExecutionsRepository, getJobRepository } from '../repository/getRepository';
import { Logger } from '../logging/Logger';
Expand Down Expand Up @@ -46,7 +46,7 @@ export class JobScheduler {
'get job description - job not found',
MomoErrorType.scheduleJob,
{ name: this.jobName },
MomoError.jobNotFound
momoError.jobNotFound
);
return;
}
Expand All @@ -66,14 +66,14 @@ export class JobScheduler {
'cannot schedule job',
MomoErrorType.scheduleJob,
{ name: this.jobName },
MomoError.jobNotFound
momoError.jobNotFound
);
return;
}

const interval = humanInterval(jobEntity.interval);
if (!interval || isNaN(interval)) {
throw MomoError.nonParsableInterval;
if (interval === undefined || isNaN(interval)) {
throw momoError.nonParsableInterval;
}

this.interval = jobEntity.interval;
Expand Down Expand Up @@ -107,7 +107,7 @@ export class JobScheduler {
'job not found, skip execution',
MomoErrorType.executeJob,
{ name: this.jobName },
MomoError.jobNotFound
momoError.jobNotFound
);
return {
status: ExecutionStatus.notFound,
Expand All @@ -131,7 +131,7 @@ export class JobScheduler {
'job not found, skip execution',
MomoErrorType.executeJob,
{ name: this.jobName },
MomoError.jobNotFound
momoError.jobNotFound
);
return;
}
Expand All @@ -144,6 +144,7 @@ export class JobScheduler {
this.logger.debug('execute job', { name: this.jobName, times: numToExecute });

for (let i = 0; i < numToExecute; i++) {
// eslint-disable-next-line no-void
void this.jobExecutor.execute(jobEntity).catch((e) => {
this.handleUnexpectedError(e);
});
Expand Down
4 changes: 2 additions & 2 deletions src/scheduler/calculateDelay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { max } from 'lodash';

export function calculateDelay(millisecondsInterval: number, immediate: boolean, job: JobEntity): number {
const nextStart = calculateNextStart(millisecondsInterval, job);
if (!nextStart) {
if (nextStart === undefined) {
return immediate ? 0 : millisecondsInterval;
}

Expand All @@ -13,6 +13,6 @@ export function calculateDelay(millisecondsInterval: number, immediate: boolean,

function calculateNextStart(interval: number, job: JobEntity): number | undefined {
const lastStarted = job.executionInfo?.lastStarted;
const lastStartedDateTime = lastStarted ? DateTime.fromISO(lastStarted) : undefined;
const lastStartedDateTime = lastStarted !== undefined ? DateTime.fromISO(lastStarted) : undefined;
return lastStartedDateTime?.plus({ milliseconds: interval }).toMillis();
}
2 changes: 1 addition & 1 deletion test/job/check.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('check', () => {
});

it('returns nothing if job not found', async () => {
mockRepositories().jobRepository;
mockRepositories();
expect(await check(name)).toBeUndefined();
});

Expand Down
4 changes: 2 additions & 2 deletions test/job/clear.integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ describe('clear', () => {

beforeAll(async () => {
mongo = await MongoMemoryServer.create();
await connect({ url: await mongo.getUri() });
await connect({ url: mongo.getUri() });
jobRepository = getJobRepository();
});

beforeEach(async () => await jobRepository.delete({}));
beforeEach(async () => jobRepository.delete({}));

afterAll(async () => {
await disconnect();
Expand Down
2 changes: 1 addition & 1 deletion test/job/findLatest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ExecutionInfo } from '../../src';

function createJob(lastFinished?: number) {
const job = JobEntity.from({ name: 'test' } as Job);
if (lastFinished) {
if (lastFinished !== undefined) {
job.executionInfo = { lastFinished: DateTime.fromMillis(lastFinished).toISO() } as ExecutionInfo;
}
return job;
Expand Down
12 changes: 6 additions & 6 deletions test/job/validate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { validate } from '../../src/job/validate';
import { withDefaults } from '../../src/job/withDefaults';
import { Job } from '../../src/job/Job';
import { Logger } from '../../src/logging/Logger';
import { MomoError, MomoErrorType } from '../../src';
import { momoError, MomoErrorType } from '../../src';

describe('validate', () => {
const logger: Logger = {
Expand All @@ -26,7 +26,7 @@ describe('validate', () => {
'job cannot be defined',
MomoErrorType.defineJob,
{ name: job.name, interval: job.interval },
MomoError.nonParsableInterval
momoError.nonParsableInterval
);
});

Expand All @@ -39,7 +39,7 @@ describe('validate', () => {
'job cannot be defined',
MomoErrorType.defineJob,
{ name: job.name, interval: job.interval },
MomoError.nonParsableInterval
momoError.nonParsableInterval
);
});

Expand All @@ -52,7 +52,7 @@ describe('validate', () => {
'job cannot be defined',
MomoErrorType.defineJob,
{ name: job.name, maxRunning: -1 },
MomoError.invalidMaxRunning
momoError.invalidMaxRunning
);
});

Expand All @@ -65,7 +65,7 @@ describe('validate', () => {
'job cannot be defined',
MomoErrorType.defineJob,
{ name: job.name, concurrency: job.concurrency },
MomoError.invalidConcurrency
momoError.invalidConcurrency
);
});

Expand All @@ -84,7 +84,7 @@ describe('validate', () => {
'job cannot be defined',
MomoErrorType.defineJob,
{ name: job.name, concurrency: job.concurrency, maxRunning: job.maxRunning },
MomoError.invalidConcurrency
momoError.invalidConcurrency
);
});
});
6 changes: 3 additions & 3 deletions test/repository/ExecutionsRepository.integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ describe('ExecutionsRepository', () => {
ExecutionsRepository.deadScheduleThreshold = 1000;

mongo = await MongoMemoryServer.create();
await connect({ url: await mongo.getUri() });
await connect({ url: mongo.getUri() });
executionsRepository = getExecutionsRepository();
});

beforeEach(async () => await executionsRepository.clear());
beforeEach(async () => executionsRepository.clear());

afterAll(async () => {
await disconnect();
Expand All @@ -34,7 +34,7 @@ describe('ExecutionsRepository', () => {
const entities = await executionsRepository.find({});

expect(entities).toHaveLength(1);
expect(entities[0].scheduleId).toEqual(scheduleId);
expect(entities[0]?.scheduleId).toEqual(scheduleId);
});
});

Expand Down
Loading

0 comments on commit 26f45ca

Please sign in to comment.