add more logs in background jobs

This commit is contained in:
m5r
2022-07-02 01:49:34 +02:00
parent a8491e28a5
commit aeda74dcf8
5 changed files with 63 additions and 6 deletions

View File

@ -10,6 +10,7 @@ type Payload = {
export default Queue<Payload>("fetch messages", async ({ data }) => {
const { phoneNumberId } = data;
logger.info(`Fetching messages for phone number with id=${phoneNumberId}`);
const phoneNumber = await db.phoneNumber.findUnique({
where: { id: phoneNumberId },
include: { twilioAccount: true },
@ -26,8 +27,11 @@ export default Queue<Payload>("fetch messages", async ({ data }) => {
]);
const messagesSent = sent.filter((message) => message.direction.startsWith("outbound"));
const messagesReceived = received.filter((message) => message.direction === "inbound");
const messages = [...messagesSent, ...messagesReceived];
logger.info(
`Found ${messagesSent.length} outbound messages and ${messagesReceived.length} inbound messages for phone number with id=${phoneNumberId}`,
);
const messages = [...messagesSent, ...messagesReceived];
await insertMessagesQueue.add(`insert messages of id=${phoneNumberId}`, {
phoneNumberId,
messages,

View File

@ -10,6 +10,7 @@ type Payload = {
export default Queue<Payload>("fetch phone calls", async ({ data }) => {
const { phoneNumberId } = data;
logger.info(`Fetching phone calls for phone number with id=${phoneNumberId}`);
const phoneNumber = await db.phoneNumber.findUnique({
where: { id: phoneNumberId },
include: { twilioAccount: true },
@ -24,8 +25,11 @@ export default Queue<Payload>("fetch phone calls", async ({ data }) => {
twilioClient.calls.list({ from: phoneNumber.number }),
twilioClient.calls.list({ to: phoneNumber.number }),
]);
const calls = [...callsSent, ...callsReceived];
logger.info(
`Found ${callsSent.length} outbound calls and ${callsReceived.length} inbound calls for phone number with id=${phoneNumberId}`,
);
const calls = [...callsSent, ...callsReceived];
await insertCallsQueue.add(`insert calls of id=${phoneNumberId}`, {
phoneNumberId,
calls,

View File

@ -13,8 +13,10 @@ type Payload = {
export default Queue<Payload>("insert messages", async ({ data }) => {
const { messages, phoneNumberId } = data;
logger.info(`Inserting ${messages.length} messages for phone number with id=${phoneNumberId}`);
const phoneNumber = await db.phoneNumber.findUnique({ where: { id: phoneNumberId } });
if (!phoneNumber) {
logger.warn(`No phone number found with id=${phoneNumberId}`);
return;
}
@ -37,7 +39,7 @@ export default Queue<Payload>("insert messages", async ({ data }) => {
.sort((a, b) => a.sentAt.getTime() - b.sentAt.getTime());
const { count } = await db.message.createMany({ data: sms, skipDuplicates: true });
logger.info(`inserted ${count} new messages for phoneNumberId=${phoneNumberId}`);
logger.info(`Inserted ${count} new messages for phone number with id=${phoneNumberId}`);
if (!phoneNumber.isFetchingMessages) {
return;

View File

@ -13,8 +13,10 @@ type Payload = {
export default Queue<Payload>("insert phone calls", async ({ data }) => {
const { calls, phoneNumberId } = data;
logger.info(`Inserting ${calls.length} phone calls for phone number with id=${phoneNumberId}`);
const phoneNumber = await db.phoneNumber.findUnique({ where: { id: phoneNumberId } });
if (!phoneNumber) {
logger.warn(`No phone number found with id=${phoneNumberId}`);
return;
}
@ -37,7 +39,7 @@ export default Queue<Payload>("insert phone calls", async ({ data }) => {
.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime());
const { count } = await db.phoneCall.createMany({ data: phoneCalls, skipDuplicates: true });
logger.info(`inserted ${count} new phone calls for phoneNumberId=${phoneNumberId}`);
logger.info(`Inserted ${count} new phone calls for phone number with id=${phoneNumberId}`);
if (!phoneNumber.isFetchingCalls) {
return;