-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
190 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
"editor.codeActionsOnSave": { | ||
"source.fixAll.eslint": true | ||
}, | ||
"jest.autoRun": {} | ||
"jest.autoRun": {}, | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
FROM node:alpine as builder | ||
WORKDIR /app | ||
COPY --chown=node:node package.json yarn.lock ./ | ||
RUN yarn install --frozen-lockfile | ||
COPY --chown=node:node . . | ||
RUN yarn build | ||
|
||
|
||
# Dev stage | ||
FROM builder as dev | ||
WORKDIR /app | ||
EXPOSE 3000 | ||
CMD ["yarn", "dev:jobs"] | ||
|
||
|
||
# Prod stage | ||
FROM node:alpine as prod | ||
WORKDIR /app | ||
COPY --from=builder /app/node_modules ./dist/node_modules | ||
COPY --from=builder /app/dist/src ./dist/src | ||
CMD ["node", "dist/src/services/jobs/index.js"] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import initializeJobs from './scansStarter' | ||
|
||
initializeJobs() |
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,55 @@ | ||
import { getScan, getScansWithProbes, listenScans } from "../../storage/scan.storage"; | ||
import cron from 'node-cron' | ||
import { restartScan } from "../requests/scanService"; | ||
|
||
const cronMapping: Record<string, cron.ScheduledTask> = {} | ||
|
||
// Keep in memory scans periodicities to avoid restarting job on scan update | ||
const periodicityMapping = {} | ||
|
||
const init = async () => { | ||
console.log(`[JOBS][SCAN][INIT] Initializating scan jobs...`) | ||
const scans = await getScansWithProbes() | ||
for (const scan of scans) { | ||
if (scan.periodicity !== 'ONCE') { | ||
const scanTask = cron.schedule(scan.periodicity, () => restartScan(scan)) | ||
cronMapping[scan.id] = scanTask | ||
cronMapping[scan.id].start() | ||
} | ||
periodicityMapping[scan.id] = scan.periodicity | ||
} | ||
console.log(`[JOBS][SCAN][INIT] Initialized ${Object.values(cronMapping).length} scans`) | ||
|
||
listenScans(async (change) => { | ||
if (change.eventType === 'UPDATE' || change.eventType === 'INSERT') { | ||
const isUpdate = change.eventType === 'UPDATE' | ||
const changeScan = change.new | ||
if (isUpdate && changeScan.periodicity === periodicityMapping[changeScan.id]) { | ||
// The periodicity has not been edited | ||
return | ||
} | ||
|
||
if (changeScan.periodicity === 'ONCE') { | ||
periodicityMapping[changeScan.id] = changeScan.periodicity | ||
if (isUpdate) { | ||
cronMapping[changeScan.id].stop() | ||
delete cronMapping[changeScan.id] | ||
} | ||
return | ||
} | ||
|
||
console.log(`[JOBS][SCAN][${change.eventType.toUpperCase()}] ${isUpdate ? 'Restarting' : 'Starting'} scan ${change.new.id} job...`) | ||
const scan = await getScan(change.new.id) | ||
if (isUpdate && cronMapping[scan.id]) { | ||
cronMapping[scan.id].stop() | ||
} | ||
cronMapping[scan.id] = cron.schedule(scan.periodicity, () => restartScan(scan)) | ||
cronMapping[scan.id].start() | ||
periodicityMapping[scan.id] = scan.periodicity | ||
console.log({ old: periodicityMapping[scan.id], new: scan.periodicity }) | ||
console.log(`[JOBS][SCAN][${change.eventType.toUpperCase()}] ${isUpdate ? 'Restarted' : 'Started'} scan ${change.new.id} job`) | ||
} | ||
}) | ||
} | ||
|
||
export default init |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
alter table "public"."probes" add column "settings" jsonb; | ||
|
||
alter table "public"."probes_results" add column "reportId" uuid; | ||
|
||
alter table "public"."scans" add column "lastRun" timestamp with time zone; | ||
|
||
alter table "public"."probes_results" add constraint "probes_results_reportId_fkey" FOREIGN KEY ("reportId") REFERENCES reports(id) ON DELETE CASCADE not valid; | ||
|
||
alter table "public"."probes_results" validate constraint "probes_results_reportId_fkey"; |
Oops, something went wrong.