Skip to content

Commit

Permalink
refactor: replace transformDateToUTC with custom logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Kampfmoehre committed Nov 20, 2023
1 parent bd9f9de commit 22a90cb
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 19 deletions.
14 changes: 7 additions & 7 deletions src/JobRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
JobState,
LoggerFactory,
SimpleLogger,
transformDateToUtc,
} from "@droidsolutions-oss/job-service";
import { add } from "date-fns";
import { Brackets, DataSource, EntityManager, FindOptionsWhere, QueryRunner, Raw, Repository } from "typeorm";
import { Job } from "./Entities/Job";
import { getDateInUtc } from "./UtcHelper";

export const getJobRepo = <TParams, TResult>(
dataSource: DataSource,
Expand Down Expand Up @@ -80,7 +80,7 @@ export class JobRepository<TParams, TResult>
cancellationToken?: AbortSignal,
): Promise<IJob<TParams, TResult>> {
const job: Job<TParams, TResult> = new Job();
const now = transformDateToUtc();
const now = getDateInUtc();
job.createdAt = now;
job.dueDate = dueDate ?? now;
job.state = JobState.Requested;
Expand Down Expand Up @@ -163,7 +163,7 @@ export class JobRepository<TParams, TResult>
): Promise<IJob<TParams, TResult> | undefined> {
const findOptions: FindOptionsWhere<Job<TParams, TResult>> = {
state: JobState.Requested,
dueDate: Raw((alias) => `${alias} < now()`),
dueDate: Raw((alias) => `${alias} <= now()`),
};

if (type) {
Expand All @@ -183,7 +183,7 @@ export class JobRepository<TParams, TResult>
this.jobTimes.set(job.id, process.hrtime.bigint());
job.state = JobState.Started;
job.runner = runner;
job.updatedAt = transformDateToUtc();
job.updatedAt = getDateInUtc();

cancellationToken?.throwIfAborted();
const info = await manager.update(
Expand Down Expand Up @@ -238,7 +238,7 @@ export class JobRepository<TParams, TResult>
throw new Error(`Unable to update progress because no job with id ${job.id} exists.`);
}

job.updatedAt = transformDateToUtc();
job.updatedAt = getDateInUtc();
if (failed === true) {
job.failedItems = (data.failedItems ?? 0) + items;
cancellationToken?.throwIfAborted();
Expand Down Expand Up @@ -267,7 +267,7 @@ export class JobRepository<TParams, TResult>
cancellationToken?: AbortSignal,
): Promise<void> {
job.state = JobState.Finished;
job.updatedAt = transformDateToUtc();
job.updatedAt = getDateInUtc();
const startTime = this.jobTimes.get(job.id);
if (startTime) {
const endTime = process.hrtime.bigint();
Expand All @@ -293,7 +293,7 @@ export class JobRepository<TParams, TResult>
return;
}

const nextRun: Date = add(transformDateToUtc(), addNextJobIn);
const nextRun: Date = add(getDateInUtc(), addNextJobIn);
cancellationToken?.throwIfAborted();
await this.addJobAsync(job.type, nextRun, job.parameters);
}
Expand Down
8 changes: 8 additions & 0 deletions src/UtcHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Transforms a given date (or now) into UTC data.
* @param {Date} date The date. Uses a new date if not given.
* @returns {Date} The given date transformed to UTC.
*/
export const getDateInUtc = (date = new Date()): Date => {
return new Date(date.getTime() + date.getTimezoneOffset() * 60000);
};
25 changes: 13 additions & 12 deletions test/JobRepository.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { JobState, transformDateToUtc } from "@droidsolutions-oss/job-service";
import { JobState } from "@droidsolutions-oss/job-service";
import { addDays, addMinutes, addSeconds } from "date-fns";
import { DataSource, DataSourceOptions, EntityManager } from "typeorm";
import { Job } from "../src/Entities/Job";
import { getJobRepo, JobRepository } from "../src/JobRepository";
import { TestEnumParameter, TestParameter, TestResult } from "./Fixture/TestParameter";
import { getDateInUtc } from "../src/UtcHelper";

describe("JobRepository", () => {
let dataSource: DataSource;
Expand Down Expand Up @@ -44,9 +45,9 @@ describe("JobRepository", () => {
let afterCreation: number;

beforeAll(async () => {
beforeCreation = transformDateToUtc().getTime();
beforeCreation = getDateInUtc().getTime();
job = await repo.addJobAsync(type, dueDate);
afterCreation = transformDateToUtc().getTime();
afterCreation = getDateInUtc().getTime();
});

it("should add a job", () => expect(job).toBeDefined());
Expand All @@ -72,9 +73,9 @@ describe("JobRepository", () => {

beforeAll(async () => {
// Handle UTC cnversionn by just using the same method
beforeCreation = transformDateToUtc().getTime();
beforeCreation = getDateInUtc().getTime();
job = await repo.addJobAsync(type);
afterCreation = transformDateToUtc().getTime();
afterCreation = getDateInUtc().getTime();
});

it("should add a job", () => expect(job).toBeDefined());
Expand Down Expand Up @@ -220,9 +221,9 @@ describe("JobRepository", () => {
olderJob = await repo.addJobAsync(type, addMinutes(dueDate, 10), parameters);

// Handle UTC cnversionn by just using the same method
beforeStart = transformDateToUtc().getTime();
beforeStart = getDateInUtc().getTime();
foundJob = await repo.getAndStartFirstPendingJobAsync(type, runner);
afterStart = transformDateToUtc().getTime();
afterStart = getDateInUtc().getTime();
});

it("should find oldest job", () => expect(foundJob?.id).toBe(olderJob.id));
Expand Down Expand Up @@ -333,9 +334,9 @@ describe("JobRepository", () => {
});

it("should add updated", async () => {
const beforeProgress = transformDateToUtc().getTime();
const beforeProgress = getDateInUtc().getTime();
await repo.addProgressAsync(existingJob, 5, false);
const afterProgress = transformDateToUtc().getTime();
const afterProgress = getDateInUtc().getTime();

const job = await repo.findOne({ where: { id: existingJob.id } });
expect(job?.successfulItems).toBe(5);
Expand All @@ -357,10 +358,10 @@ describe("JobRepository", () => {
beforeAll(async () => {
job = await repo.addJobAsync("finished-job-without-new", undefined, parameters);

beforeFinish = transformDateToUtc().getTime();
beforeFinish = getDateInUtc().getTime();
job.result = result;
await repo.finishJobAsync(job);
afterFinish = transformDateToUtc().getTime();
afterFinish = getDateInUtc().getTime();

loadedJob = await repo.findOne({ where: { id: job.id } });
});
Expand Down Expand Up @@ -392,7 +393,7 @@ describe("JobRepository", () => {
});

it("should add a day to next job", async () => {
beforeFinish = transformDateToUtc().getTime();
beforeFinish = getDateInUtc().getTime();
await repo.finishJobAsync(existingJob, { days: 1 });

const newJob = await repo.findOne({ where: { type, state: JobState.Requested } });
Expand Down

0 comments on commit 22a90cb

Please sign in to comment.