-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rabbitmq): scheduler declare exchange (#28)
and simplify scheduler
- Loading branch information
Showing
8 changed files
with
47 additions
and
93 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
35 changes: 19 additions & 16 deletions
35
scheduler/src/background-tasks/if-job-is-doing-too-long.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 |
---|---|---|
@@ -1,20 +1,23 @@ | ||
import ms from "ms"; | ||
import { bus } from "../bus"; | ||
import { cfg, CollectionName, JobStatus, AppEvent } from "../cfg"; | ||
import { mongoConnectionPool } from "../connections"; | ||
import { cfg, CollectionName, JobStatus, ExchangeName } from "../cfg"; | ||
import { mongoConnectionPool, rabbitmqConnectionPool } from "../connections"; | ||
import { DKHPTDJob } from "../entities"; | ||
import logger from "../loggers/logger"; | ||
import { loop } from "../utils"; | ||
import { loop, toBuffer } from "../utils"; | ||
|
||
export const setup = () => loop.infinity(async () => { | ||
const cursor = mongoConnectionPool.getClient().db(cfg.DATABASE_NAME).collection(CollectionName.DKHPTD).find({ | ||
doingAt: { $lt: Date.now() - ms("1m") }, /* less than now - 1 minute then it's probaly dead or error */ | ||
status: JobStatus.DOING, | ||
}, { sort: { timeToStart: 1 } }); | ||
while (await cursor.hasNext()) { | ||
const entry = await cursor.next(); | ||
const job = new DKHPTDJob(entry); | ||
logger.info(`Found stale job ${job._id}`); | ||
bus.emit(AppEvent.STALE_JOB, job._id); | ||
} | ||
}, ms("10s")); | ||
export const setup = () => { | ||
rabbitmqConnectionPool.getChannel().assertExchange(ExchangeName.MAYBE_STALE_JOB, "fanout"); | ||
loop.infinity(async () => { | ||
const cursor = mongoConnectionPool.getClient().db(cfg.DATABASE_NAME).collection(CollectionName.DKHPTD).find({ | ||
doingAt: { $lt: Date.now() - ms("1m") }, /* less than now - 1 minute then it's probaly dead or error */ | ||
status: JobStatus.DOING, | ||
}, { sort: { timeToStart: 1 } }); | ||
while (await cursor.hasNext()) { | ||
const entry = await cursor.next(); | ||
const job = new DKHPTDJob(entry); | ||
const jobId = job._id; | ||
logger.info(`Found stale job ${jobId}`); | ||
rabbitmqConnectionPool.getChannel().publish(ExchangeName.MAYBE_STALE_JOB, "", toBuffer(jobId.toHexString())); | ||
} | ||
}, ms("10s")); | ||
} |
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 |
---|---|---|
@@ -1,27 +1,30 @@ | ||
import { ObjectId } from "mongodb"; | ||
import { bus } from "../bus"; | ||
import { AppEvent, cfg, CollectionName, JobStatus } from "../cfg"; | ||
import { mongoConnectionPool } from "../connections"; | ||
import { cfg, CollectionName, JobStatus, QueueName } from "../cfg"; | ||
import { mongoConnectionPool, rabbitmqConnectionPool } from "../connections"; | ||
import { DKHPTDJob } from "../entities"; | ||
import logger from "../loggers/logger"; | ||
import ms from "ms"; | ||
import { loop, toJobWorkerMessage } from "../utils"; | ||
import { loop, toBuffer, toJobWorkerMessage } from "../utils"; | ||
|
||
export const setup = () => loop.infinity(async () => { | ||
const cursor = mongoConnectionPool.getClient().db(cfg.DATABASE_NAME).collection(CollectionName.DKHPTD).find({ | ||
timeToStart: { $lt: Date.now() }, /* less than now then it's time to run */ | ||
status: JobStatus.READY, | ||
}, { sort: { timeToStart: 1 } }); | ||
while (await cursor.hasNext()) { | ||
const entry = await cursor.next(); | ||
const job = new DKHPTDJob(entry); | ||
bus.emit(AppEvent.NEW_JOB, toJobWorkerMessage(job)); | ||
await mongoConnectionPool.getClient() | ||
.db(cfg.DATABASE_NAME).collection(CollectionName.DKHPTD).updateOne({ _id: new ObjectId(job._id) }, { | ||
$set: { | ||
status: JobStatus.DOING, | ||
doingAt: Date.now(), | ||
}, | ||
}); | ||
} | ||
}, ms("10s")); | ||
export const setup = () => { | ||
rabbitmqConnectionPool.getChannel().assertQueue(QueueName.RUN_JOB, {}); | ||
loop.infinity(async () => { | ||
const cursor = mongoConnectionPool.getClient().db(cfg.DATABASE_NAME).collection(CollectionName.DKHPTD).find({ | ||
timeToStart: { $lt: Date.now() }, /* less than now then it's time to run */ | ||
status: JobStatus.READY, | ||
}, { sort: { timeToStart: 1 } }); | ||
while (await cursor.hasNext()) { | ||
const entry = await cursor.next(); | ||
const job = new DKHPTDJob(entry); | ||
logger.info("New job: " + job._id) | ||
rabbitmqConnectionPool.getChannel().sendToQueue(QueueName.RUN_JOB, toBuffer(toJobWorkerMessage(job))); | ||
await mongoConnectionPool.getClient() | ||
.db(cfg.DATABASE_NAME).collection(CollectionName.DKHPTD).updateOne({ _id: new ObjectId(job._id) }, { | ||
$set: { | ||
status: JobStatus.DOING, | ||
doingAt: Date.now(), | ||
}, | ||
}); | ||
} | ||
}, ms("10s")); | ||
} |
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.