add worker jobs

This commit is contained in:
Simon Larsen 2022-10-19 16:33:33 +01:00
parent 526963b694
commit e0692ad649
No known key found for this signature in database
GPG Key ID: AB45983AA9C81CDE
9 changed files with 7109 additions and 29 deletions

24
.github/workflows/compile.workers.yaml vendored Normal file
View File

@ -0,0 +1,24 @@
name: Compile Workers
on:
pull_request:
push:
branches-ignore:
- 'hotfix-*'
- 'release'
- 'staging-release'
jobs:
compile:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
check-latest: true
- run: cd Common && npm install
- run: cd Model && npm install
- run: cd CommonServer && npm install
- run: cd CommonUI && npm install --force
- run: cd Workers && npm install && npm run compile

View File

@ -0,0 +1,21 @@
name: Workers Docker Build
on:
pull_request:
push:
branches-ignore:
- 'hotfix-*'
- 'release'
- 'staging-release'
jobs:
# define jobs to build docker imag
docker-build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
# build image for home
- name: build docker image
run: npm run build-ci workers

View File

@ -49,42 +49,29 @@ export class Service extends DatabaseService<Model> {
return { createBy, carryForward: null };
}
protected override async onCreateSuccess(
onCreate: OnCreate<Model>,
createdItem: Model
): Promise<Model> {
if (!createdItem.projectId) {
public async changeAttachedMonitorStates(
item: Model,
props: DatabaseCommonInteractionProps
): Promise<void> {
if (!item.projectId) {
throw new BadDataException('projectId is required');
}
if (!createdItem.id) {
if (!item.id) {
throw new BadDataException('id is required');
}
if (!createdItem.currentScheduledMaintenanceStateId) {
throw new BadDataException('currentScheduledMaintenanceStateId is required');
}
if (createdItem.changeMonitorStatusToId && createdItem.projectId) {
if (item.changeMonitorStatusToId && item.projectId) {
// change status of all the monitors.
await MonitorService.changeMonitorStatus(
createdItem.projectId,
createdItem.monitors?.map((monitor: Monitor) => {
item.projectId,
item.monitors?.map((monitor: Monitor) => {
return new ObjectID(monitor._id || '');
}) || [],
createdItem.changeMonitorStatusToId,
onCreate.createBy.props
item.changeMonitorStatusToId,
props
);
}
await this.changeScheduledMaintenanceState(
createdItem.projectId,
createdItem.id,
createdItem.currentScheduledMaintenanceStateId,
onCreate.createBy.props
);
return createdItem;
}
public async changeScheduledMaintenanceState(
@ -93,6 +80,19 @@ export class Service extends DatabaseService<Model> {
scheduledMaintenanceStateId: ObjectID,
props: DatabaseCommonInteractionProps
): Promise<void> {
await this.updateBy({
data: {
currentScheduledMaintenanceStateId: scheduledMaintenanceStateId.id
},
query: {
_id: scheduledMaintenanceId.toString()!
},
props: {
isRoot: true
}
});
const statusTimeline: ScheduledMaintenanceStateTimeline =
new ScheduledMaintenanceStateTimeline();

View File

@ -0,0 +1,73 @@
import logger from 'CommonServer/Utils/Logger';
import cron from 'node-cron';
import { EVERY_MINUTE } from '../../Utils/CronTime';
import ScheduledMaintenanceService from 'CommonServer/Services/ScheduledMaintenanceService'
import QueryHelper from 'CommonServer/Types/Database/QueryHelper';
import OneUptimeDate from 'Common/Types/Date';
import LIMIT_MAX from 'Common/Types/Database/LimitMax';
import ScheduledMaintenance from 'Model/Models/ScheduledMaintenance';
import ScheduledMaintenanceState from 'Model/Models/ScheduledMaintenanceState';
import ScheduledMaintenanceStateService from 'CommonServer/Services/ScheduledMaintenanceStateService';
cron.schedule(EVERY_MINUTE, async () => {
try {
// get all scheduled events of all the projects.
const events: Array<ScheduledMaintenance> = await ScheduledMaintenanceService.findBy({
query: {
currentScheduledMaintenanceState: {
isScheduledState: true,
} as any,
startsAt: QueryHelper.lessThan(OneUptimeDate.getCurrentDate())
},
props: {
isRoot: true
},
limit: LIMIT_MAX,
skip: 0,
select: {
_id: true,
projectId: true,
changeMonitorStatusToId: true
},
populate: {
monitors: {
_id: true
}
}
});
// change their state to Ongoing.
for (const event of events) {
const scheduledMaintenanceState: ScheduledMaintenanceState | null =
await ScheduledMaintenanceStateService.findOneBy({
query: {
projectId: event.projectId!,
isScheduledState: true,
},
select: {
_id: true,
},
props: {
isRoot: true,
},
});
if (!scheduledMaintenanceState || !scheduledMaintenanceState.id) {
continue;
}
await ScheduledMaintenanceService.changeScheduledMaintenanceState(event.projectId!, event.id!, scheduledMaintenanceState.id, {
isRoot: true
})
// change attached monitor states.
await ScheduledMaintenanceService.changeAttachedMonitorStates(event, { isRoot: true })
}
} catch (err) {
logger.error(err);
}
});

View File

@ -0,0 +1 @@
export const EVERY_MINUTE: string = '* * * * *';

6936
Workers/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -4,14 +4,26 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"start": "ts-node Index.ts",
"compile": "tsc",
"dev": "nodemon",
"audit": "npm audit --audit-level=low",
"dep-check": "depcheck ./ --skip-missing=true",
"test": "jest"
},
"author": "",
"license": "MIT",
"dependencies": {
"node-cron": "^3.0.2",
"@types/node-cron": "^3.0.4",
"Common": "file:../Common",
"CommonServer": "file:../CommonServer",
"Model": "file:../Model",
"CommonServer": "file:../CommonServer"
"node-cron": "^3.0.2"
},
"devDependencies": {
"@types/jest": "^27.5.0",
"@types/node": "^17.0.31",
"jest": "^28.1.0",
"ts-jest": "^28.0.2"
}
}

View File

@ -52,6 +52,21 @@ services:
- redis
- postgres
workers:
ports:
- '3452:3452'
build:
context: .
dockerfile: ./Workers/Dockerfile
env_file:
- ./Common/.env
- ./CommonServer/.env
- ./Workers/.env
environment: *common-variables
depends_on:
- postgres
- mail
file:
ports:
- '3125:3125'

View File

@ -217,11 +217,9 @@ services:
- ./Workers/.env
environment: *common-variables
depends_on:
- redis
- postgres
- mail
links:
- redis
- postgres
- mail
volumes: