Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: Add suspended type #47

Closed
wants to merge 13 commits into from
2 changes: 2 additions & 0 deletions openapi3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,7 @@ components:
- Failed
- Expired
- Aborted
- Suspended
example: Pending
jobResponse:
type: object
Expand All @@ -815,6 +816,7 @@ components:
- pendingTasks
- inProgressTasks
- abortedTasks
- suspendedTasks
- status
- percentage
- reason
Expand Down
3 changes: 3 additions & 0 deletions src/DAL/entity/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ export class JobEntity {
@Column('int', { nullable: false, default: 0 })
public abortedTasks: number;

@Column('int', { nullable: false, default: 0 })
public suspendedTasks: number;

@OneToMany(() => TaskEntity, (task) => task.job, {
cascade: true,
})
Expand Down
16 changes: 10 additions & 6 deletions src/DAL/migration/FullSchema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

SET search_path TO "JobManager", public; -- CHANGE SCHEMA NAME TO MATCH ENVIRONMENT
CREATE TYPE "operation_status_enum" AS ENUM
('Pending', 'In-Progress', 'Completed', 'Failed', 'Expired', 'Aborted');
('Pending', 'In-Progress', 'Completed', 'Failed', 'Expired', 'Aborted', 'Suspended');

CREATE TABLE "Job"
(
Expand Down Expand Up @@ -35,11 +35,12 @@ CREATE TABLE "Job"
"pendingTasks" int NOT NULL DEFAULT 0,
"inProgressTasks" int NOT NULL DEFAULT 0,
"abortedTasks" int NOT NULL DEFAULT 0,
"suspendedTasks" int NOT NULL DEFAULT 0,
"additionalIdentifiers" text COLLATE pg_catalog."default",
"domain" text COLLATE pg_catalog."default" NOT NULL DEFAULT ''::text,
CONSTRAINT "PK_job_id" PRIMARY KEY (id),
CONSTRAINT "UQ_uniqueness_on_active_tasks" EXCLUDE ("resourceId" with =, "version" with =, "type" with =, "additionalIdentifiers" with =) WHERE ((status = 'Pending' OR status = 'In-Progress') AND ("additionalIdentifiers" IS NOT NULL)),
CONSTRAINT "UQ_uniqueness_on_active_tasks_no_additional_identifier" EXCLUDE ("resourceId" with =, "version" with =, "type" with =) WHERE ((status = 'Pending' OR status = 'In-Progress') AND ("additionalIdentifiers" IS NULL))
CONSTRAINT "UQ_uniqueness_on_active_tasks" EXCLUDE ("resourceId" with =, "version" with =, "type" with =, "additionalIdentifiers" with =) WHERE ((status = 'Pending' OR status = 'In-Progress' OR status = 'Suspended') AND ("additionalIdentifiers" IS NOT NULL)),
CONSTRAINT "UQ_uniqueness_on_active_tasks_no_additional_identifier" EXCLUDE ("resourceId" with =, "version" with =, "type" with =) WHERE ((status = 'Pending' OR status = 'In-Progress' OR status = 'Suspended') AND ("additionalIdentifiers" IS NULL))
);

CREATE INDEX "jobCleanedIndex"
Expand Down Expand Up @@ -107,7 +108,8 @@ BEGIN
"expiredTasks" = "expiredTasks" + CASE WHEN NEW."status" = 'Expired' THEN 1 ELSE 0 END,
"pendingTasks" = "pendingTasks" + CASE WHEN NEW."status" = 'Pending' THEN 1 ELSE 0 END,
"inProgressTasks" = "inProgressTasks" + CASE WHEN NEW."status" = 'In-Progress' THEN 1 ELSE 0 END,
"abortedTasks" = "abortedTasks" + CASE WHEN NEW."status" = 'Aborted' THEN 1 ELSE 0 END
"abortedTasks" = "abortedTasks" + CASE WHEN NEW."status" = 'Aborted' THEN 1 ELSE 0 END,
"suspendedTasks" = "suspendedTasks" + CASE WHEN NEW."status" = 'Suspended' THEN 1 ELSE 0 END
WHERE id = NEW."jobId";
RETURN NULL;
END;
Expand All @@ -131,7 +133,8 @@ BEGIN
"expiredTasks" = "expiredTasks" - CASE WHEN OLD."status" = 'Expired' THEN 1 ELSE 0 END,
"pendingTasks" = "pendingTasks" - CASE WHEN OLD."status" = 'Pending' THEN 1 ELSE 0 END,
"inProgressTasks" = "inProgressTasks" - CASE WHEN OLD."status" = 'In-Progress' THEN 1 ELSE 0 END,
"abortedTasks" = "abortedTasks" - CASE WHEN OLD."status" = 'Aborted' THEN 1 ELSE 0 END
"abortedTasks" = "abortedTasks" - CASE WHEN OLD."status" = 'Aborted' THEN 1 ELSE 0 END,
"suspendedTasks" = "suspendedTasks" - CASE WHEN OLD."status" = 'Suspended' THEN 1 ELSE 0 END
WHERE id = OLD."jobId";
RETURN NULL;
END;
Expand All @@ -156,7 +159,8 @@ BEGIN
"expiredTasks" = "expiredTasks" + CASE WHEN NEW."status" = 'Expired' THEN 1 WHEN OLD."status" = 'Expired' THEN -1 ELSE 0 END,
"pendingTasks" = "pendingTasks" + CASE WHEN NEW."status" = 'Pending' THEN 1 WHEN OLD."status" = 'Pending' THEN -1 ELSE 0 END,
"inProgressTasks" = "inProgressTasks" + CASE WHEN NEW."status" = 'In-Progress' THEN 1 WHEN OLD."status" = 'In-Progress' THEN -1 ELSE 0 END,
"abortedTasks" = "abortedTasks" + CASE WHEN NEW."status" = 'Aborted' THEN 1 WHEN OLD."status" = 'Aborted' THEN -1 ELSE 0 END
"abortedTasks" = "abortedTasks" + CASE WHEN NEW."status" = 'Aborted' THEN 1 WHEN OLD."status" = 'Aborted' THEN -1 ELSE 0 END,
"suspendedTasks" = "suspendedTasks" + CASE WHEN NEW."status" = 'Suspended' THEN 1 WHEN OLD."status" = 'Suspended' THEN -1 ELSE 0 END
WHERE id = NEW."jobId";
END IF;
RETURN NULL;
Expand Down
76 changes: 76 additions & 0 deletions src/DAL/migration/v3.0.0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
-- Alter the table to add the modified and new constraints
ALTER TYPE "JobManager"."operation_status_enum" ADD VALUE 'Suspended';

ALTER TABLE "Job" ADD COLUMN "suspendedTasks" INT DEFAULT 0 NOT NULL;

ALTER TABLE "Job"
DROP CONSTRAINT IF EXISTS "UQ_uniqueness_on_active_tasks";

ALTER TABLE "Job"
ADD CONSTRAINT "UQ_uniqueness_on_active_tasks" EXCLUDE ("resourceId" with =, "version" with =, "type" with =, "additionalIdentifiers" with =) WHERE ((status = 'Pending' OR status = 'In-Progress' OR status = 'Suspended') AND ("additionalIdentifiers" IS NOT NULL));

ALTER TABLE "Job"
DROP CONSTRAINT IF EXISTS "UQ_uniqueness_on_active_tasks_no_additional_identifier";

ALTER TABLE "Job"
ADD CONSTRAINT "UQ_uniqueness_on_active_tasks_no_additional_identifier" EXCLUDE ("resourceId" with =, "version" with =, "type" with =) WHERE ((status = 'Pending' OR status = 'In-Progress' OR status = 'Suspended') AND ("additionalIdentifiers" IS NULL));

CREATE OR REPLACE FUNCTION update_tasks_counters_insert() RETURNS trigger
SET search_path FROM CURRENT
LANGUAGE plpgsql
AS $$
BEGIN
UPDATE "Job"
SET "taskCount" = "taskCount" + 1,
"completedTasks" = "completedTasks" + CASE WHEN NEW."status" = 'Completed' THEN 1 ELSE 0 END,
"failedTasks" = "failedTasks" + CASE WHEN NEW."status" = 'Failed' THEN 1 ELSE 0 END,
"expiredTasks" = "expiredTasks" + CASE WHEN NEW."status" = 'Expired' THEN 1 ELSE 0 END,
"pendingTasks" = "pendingTasks" + CASE WHEN NEW."status" = 'Pending' THEN 1 ELSE 0 END,
"inProgressTasks" = "inProgressTasks" + CASE WHEN NEW."status" = 'In-Progress' THEN 1 ELSE 0 END,
"abortedTasks" = "abortedTasks" + CASE WHEN NEW."status" = 'Aborted' THEN 1 ELSE 0 END,
"suspendedTasks" = "suspendedTasks" + CASE WHEN NEW."status" = 'Suspended' THEN 1 ELSE 0 END
WHERE id = NEW."jobId";
RETURN NULL;
END;
$$;

CREATE OR REPLACE FUNCTION update_tasks_counters_delete() RETURNS trigger
SET search_path FROM CURRENT
LANGUAGE plpgsql
AS $$
BEGIN
UPDATE "Job"
SET "taskCount" = "taskCount" - 1,
"completedTasks" = "completedTasks" - CASE WHEN OLD."status" = 'Completed' THEN 1 ELSE 0 END,
"failedTasks" = "failedTasks" - CASE WHEN OLD."status" = 'Failed' THEN 1 ELSE 0 END,
"expiredTasks" = "expiredTasks" - CASE WHEN OLD."status" = 'Expired' THEN 1 ELSE 0 END,
"pendingTasks" = "pendingTasks" - CASE WHEN OLD."status" = 'Pending' THEN 1 ELSE 0 END,
"inProgressTasks" = "inProgressTasks" - CASE WHEN OLD."status" = 'In-Progress' THEN 1 ELSE 0 END,
"abortedTasks" = "abortedTasks" - CASE WHEN OLD."status" = 'Aborted' THEN 1 ELSE 0 END,
"suspendedTasks" = "suspendedTasks" - CASE WHEN OLD."status" = 'Suspended' THEN 1 ELSE 0 END
WHERE id = OLD."jobId";
RETURN NULL;
END;
$$;


CREATE OR REPLACE FUNCTION update_tasks_counters_update() RETURNS trigger
SET search_path FROM CURRENT
LANGUAGE plpgsql
AS $$
BEGIN
IF NEW."status" != OLD."status" THEN
UPDATE "Job"
SET
"completedTasks" = "completedTasks" + CASE WHEN NEW."status" = 'Completed' THEN 1 WHEN OLD."status" = 'Completed' THEN -1 ELSE 0 END,
"failedTasks" = "failedTasks" + CASE WHEN NEW."status" = 'Failed' THEN 1 WHEN OLD."status" = 'Failed' THEN -1 ELSE 0 END,
"expiredTasks" = "expiredTasks" + CASE WHEN NEW."status" = 'Expired' THEN 1 WHEN OLD."status" = 'Expired' THEN -1 ELSE 0 END,
"pendingTasks" = "pendingTasks" + CASE WHEN NEW."status" = 'Pending' THEN 1 WHEN OLD."status" = 'Pending' THEN -1 ELSE 0 END,
"inProgressTasks" = "inProgressTasks" + CASE WHEN NEW."status" = 'In-Progress' THEN 1 WHEN OLD."status" = 'In-Progress' THEN -1 ELSE 0 END,
"abortedTasks" = "abortedTasks" + CASE WHEN NEW."status" = 'Aborted' THEN 1 WHEN OLD."status" = 'Aborted' THEN -1 ELSE 0 END,
"suspendedTasks" = "suspendedTasks" + CASE WHEN NEW."status" = 'Suspended' THEN 1 WHEN OLD."status" = 'Suspended' THEN -1 ELSE 0 END
WHERE id = NEW."jobId";
END IF;
RETURN NULL;
END;
$$;
3 changes: 2 additions & 1 deletion src/DAL/repositories/jobRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export class JobRepository extends GeneralRepository<JobEntity> {
.addSelect(`CAST(COUNT(CASE WHEN task.status = '${OperationStatus.PENDING}' THEN 1 ELSE NULL END) as integer)`, 'job_pendingTasks')
.addSelect(`CAST(COUNT(CASE WHEN task.status = '${OperationStatus.ABORTED}' THEN 1 ELSE NULL END) as integer)`, 'job_abortedTasks')
.addSelect(`CAST(COUNT(CASE WHEN task.status = '${OperationStatus.FAILED}' THEN 1 ELSE NULL END) as integer)`, 'job_failedTasks')
.addSelect(`CAST(COUNT(CASE WHEN task.status = '${OperationStatus.SUSPENDED}' THEN 1 ELSE NULL END) as integer)`, 'job_suspendedTasks')
.andWhere(`task.type = '${req.taskType}'`)
.groupBy('job.id');
}
Expand Down Expand Up @@ -250,7 +251,7 @@ export class JobRepository extends GeneralRepository<JobEntity> {
FROM "Job" AS jb
INNER JOIN "Task" as tk ON tk."jobId" = jb.id
WHERE jb.id = $1 AND
(jb.status = '${OperationStatus.EXPIRED}' OR jb.status = '${OperationStatus.FAILED}' OR jb.status = '${OperationStatus.ABORTED}') AND
(jb.status = '${OperationStatus.EXPIRED}' OR jb.status = '${OperationStatus.FAILED}' OR jb.status = '${OperationStatus.ABORTED} OR jb.status = '${OperationStatus.SUSPENDED}') AND
(tk.status = '${OperationStatus.EXPIRED}' OR tk.status = '${OperationStatus.FAILED}' OR tk.status = '${OperationStatus.ABORTED}') AND
jb."isCleaned" = FALSE`;
const sqlRes = (await this.query(query, [jobId])) as { unResettableTasks: string; failedTasks: string }[];
Expand Down
7 changes: 6 additions & 1 deletion src/DAL/repositories/taskRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,12 @@ export class TaskRepository extends GeneralRepository<TaskEntity> {
.where('"jobId" = :jobId', { jobId })
.andWhere(
new Brackets((qb) => {
qb.where([{ status: OperationStatus.FAILED }, { status: OperationStatus.EXPIRED }, { status: OperationStatus.ABORTED }]);
qb.where([
{ status: OperationStatus.FAILED },
{ status: OperationStatus.EXPIRED },
{ status: OperationStatus.ABORTED },
{ status: OperationStatus.SUSPENDED },
]);
})
)
.execute();
Expand Down
1 change: 1 addition & 0 deletions src/common/dataModels/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export enum OperationStatus {
FAILED = 'Failed',
EXPIRED = 'Expired',
ABORTED = 'Aborted',
SUSPENDED = 'Suspended',
}
1 change: 1 addition & 0 deletions src/common/dataModels/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export interface IGetJobResponse {
pendingTasks: number;
inProgressTasks: number;
abortedTasks: number;
suspendedTasks: number;
additionalIdentifiers?: string;
expirationDate?: Date;
domain: string;
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/jobs/jobs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ describe('job', function () {

expect(response.status).toBe(httpStatusCodes.OK);
expect(select).toHaveBeenCalledTimes(1);
expect(addSelect).toHaveBeenCalledTimes(6);
expect(addSelect).toHaveBeenCalledTimes(7);
expect(innerJoin).toHaveBeenCalledTimes(1);
expect(groupBy).toHaveBeenCalledTimes(1);
expect(where).toHaveBeenCalledTimes(1);
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/DAL/jobModelConverter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ describe('JobModelConverter', () => {
inProgressTasks: 1,
pendingTasks: 1,
abortedTasks: 0,
suspendedTasks: 0,
internalId: 'test id',
producerName: 'test producer',
productType: 'test type',
Expand Down Expand Up @@ -221,6 +222,7 @@ describe('JobModelConverter', () => {
failedTasks: 0,
inProgressTasks: 1,
abortedTasks: 0,
suspendedTasks: 0,
internalId: 'test id',
producerName: 'test producer',
productType: 'test type',
Expand Down
Loading