-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added cancel job functionality * Updated job controller * Updated type to patch and validation params approach * Updated job cancelation logic * Added job cancel functionality * Added enums webhook * Resolved comments * [Job Launcher] Added job id relation to the payment (#835) * Updated payment entity and migration * Updated job service * Updated unit tests * Updated unit tests
- Loading branch information
1 parent
3039dfc
commit 115e88f
Showing
14 changed files
with
289 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
packages/apps/job-launcher/server/src/common/enums/webhook.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum EventType { | ||
ESCROW_CREATED = 'escrow_created', | ||
TASK_CREATION_FAILED = 'task_creation_failed', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
packages/apps/job-launcher/server/src/modules/job/job.cron.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { Cron, CronExpression } from '@nestjs/schedule'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { LessThanOrEqual, Repository } from 'typeorm'; | ||
import { SortDirection } from '../../common/enums/collection'; | ||
import { JOB_RETRIES_COUNT_THRESHOLD } from '../../common/constants'; | ||
import { JobStatus } from '../../common/enums/job'; | ||
import { JobEntity } from './job.entity'; | ||
import { JobService } from './job.service'; | ||
|
||
@Injectable() | ||
export class JobCron { | ||
private readonly logger = new Logger(JobCron.name); | ||
|
||
constructor( | ||
private readonly jobService: JobService, | ||
@InjectRepository(JobEntity) | ||
private readonly jobEntityRepository: Repository<JobEntity>, | ||
) {} | ||
|
||
@Cron(CronExpression.EVERY_10_SECONDS) | ||
public async launchJob() { | ||
try { | ||
// TODO: Add retry policy and process failure requests https://github.com/humanprotocol/human-protocol/issues/334 | ||
const jobEntity = await this.jobEntityRepository.findOne({ | ||
where: { | ||
status: JobStatus.PAID, | ||
retriesCount: LessThanOrEqual(JOB_RETRIES_COUNT_THRESHOLD), | ||
waitUntil: LessThanOrEqual(new Date()), | ||
}, | ||
order: { | ||
waitUntil: SortDirection.ASC, | ||
}, | ||
}); | ||
|
||
if (!jobEntity) return; | ||
|
||
await this.jobService.launchJob(jobEntity); | ||
} catch (e) { | ||
this.logger.error(e); | ||
return; | ||
} | ||
} | ||
|
||
@Cron(CronExpression.EVERY_10_SECONDS) | ||
public async cancelJob() { | ||
try { | ||
const jobEntity = await this.jobEntityRepository.findOne({ | ||
where: { | ||
status: JobStatus.TO_CANCEL, | ||
retriesCount: LessThanOrEqual(JOB_RETRIES_COUNT_THRESHOLD), | ||
waitUntil: LessThanOrEqual(new Date()), | ||
}, | ||
order: { | ||
waitUntil: SortDirection.ASC, | ||
}, | ||
}); | ||
|
||
if (!jobEntity) return; | ||
|
||
await this.jobService.cancelJob(jobEntity); | ||
} catch (e) { | ||
this.logger.error(e); | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
115e88f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
job-launcher-server – ./packages/apps/job-launcher/server
job-launcher-server-nine.vercel.app
job-launcher-server-humanprotocol.vercel.app
job-launcher-server-git-develop-humanprotocol.vercel.app