Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #259 from beabee-communityrm/feat/762-fix-cron-log…
Browse files Browse the repository at this point in the history
…ging

fix: log cron job via logger for properly formatted log messages
  • Loading branch information
wpf500 authored Apr 27, 2023
2 parents 1dbfbd9 + c142bc3 commit 416b8e7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
21 changes: 12 additions & 9 deletions src/tools/mailchimp/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import moment from "moment";
import { Between, getRepository } from "typeorm";

import * as db from "@core/database";
import { log as mainLogger } from "@core/logging";

import ContactsService from "@core/services/ContactsService";
import NewsletterService from "@core/services/NewsletterService";
Expand All @@ -13,6 +14,8 @@ import OptionsService from "@core/services/OptionsService";
import Contact from "@models/Contact";
import ContactRole from "@models/ContactRole";

const log = mainLogger.child({ app: "mailchimp-sync" });

async function fetchContacts(
startDate: string | undefined,
endDate: string | undefined
Expand All @@ -22,10 +25,10 @@ async function fetchContacts(
: moment().subtract({ d: 1, h: 2 }).toDate();
const actualEndDate = moment(endDate).toDate();

console.log("Start date:", actualStartDate.toISOString());
console.log("End date:", actualEndDate.toISOString());

console.log("# Fetching contacts");
log.info("Fetching contacts", {
startDate: actualStartDate,
endDate: actualEndDate
});

const memberships = await getRepository(ContactRole).find({
where: {
Expand All @@ -34,9 +37,9 @@ async function fetchContacts(
},
relations: ["contact", "contact.profile"]
});
console.log(`Got ${memberships.length} members`);
log.info(`Got ${memberships.length} members`);
return memberships.map(({ contact }) => {
console.log(contact.membership?.isActive ? "U" : "D", contact.email);
log.info(contact.membership?.isActive ? "U" : "D", contact.email);
return contact;
});
}
Expand All @@ -48,7 +51,7 @@ async function processContacts(contacts: Contact[]) {
!m.membership?.isActive
);

console.log(
log.info(
`Removing active member tag from ${contactsToArchive.length} contacts`
);
await NewsletterService.removeTagFromContacts(
Expand All @@ -57,7 +60,7 @@ async function processContacts(contacts: Contact[]) {
);

if (OptionsService.getBool("newsletter-archive-on-expired")) {
console.log(`Archiving ${contactsToArchive.length} contacts`);
log.info(`Archiving ${contactsToArchive.length} contacts`);
for (const contact of contactsToArchive) {
await ContactsService.updateContactProfile(
contact,
Expand All @@ -84,7 +87,7 @@ db.connect().then(async () => {
await processContacts(contacts);
}
} catch (err) {
console.error(err);
log.error(err);
}
await db.close();
});
2 changes: 1 addition & 1 deletion src/tools/start-gifts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ db.connect().then(async () => {
try {
await main(process.argv[2]);
} catch (err) {
console.error(err);
log.error(err);
}
await db.close();
});

0 comments on commit 416b8e7

Please sign in to comment.