From 7e385bea74faf93cc465316ca74e2a6709372d26 Mon Sep 17 00:00:00 2001 From: William Moss <33007279+willmoss1000@users.noreply.github.com> Date: Sun, 11 Nov 2018 10:01:37 +0000 Subject: [PATCH 1/2] Update CartCleanupJob --- .../included/jobcontrol/server/jobs/cart.js | 65 +++++++++++-------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/imports/plugins/included/jobcontrol/server/jobs/cart.js b/imports/plugins/included/jobcontrol/server/jobs/cart.js index 727da994101..4708773bd2d 100644 --- a/imports/plugins/included/jobcontrol/server/jobs/cart.js +++ b/imports/plugins/included/jobcontrol/server/jobs/cart.js @@ -4,12 +4,7 @@ import { Meteor } from "meteor/meteor"; import { Accounts, Cart, Jobs } from "/lib/collections"; import Reaction from "/imports/plugins/core/core/server/Reaction"; import { Job } from "/imports/plugins/core/job-collection/lib"; - -let moment; -async function lazyLoadMoment() { - if (moment) return; - moment = await import("moment").default; -} +import moment from "moment"; /** * @param {Object} olderThan older than date @@ -55,34 +50,48 @@ export function cartCleanupJob() { pollInterval: 60 * 60 * 1000, // backup polling, see observer below workTimeout: 180 * 1000 }, (job, callback) => { - Logger.debug("Processing cart/removeFromCart"); + Logger.info("Processing cart/removeFromCart"); const settings = Reaction.getShopSettings(); if (settings.cart) { - Promise.await(lazyLoadMoment()); const schedule = (settings.cart.cleanupDurationDays).match(/\d/);// configurable in shop settings const olderThan = moment().subtract(Number(schedule[0]), "days")._d; + Logger.info("removing carts older than", olderThan); const carts = getStaleCarts(olderThan); - carts.forEach((cart) => { - const account = Accounts.findOne({ _id: cart.accountId }); - const removeCart = Cart.remove({ accountId: account._id }); - if (!account.emails.length) { - const removeAccount = Accounts.remove({ - _id: account._id, - emails: [] - }); - Hooks.Events.run("afterAccountsRemove", null, account._id); - Meteor.users.remove({ _id: account.userId, emails: [] }); // clears out anonymous user - if (removeCart && removeAccount) { - const success = "Stale anonymous user cart and account successfully cleaned"; - Logger.debug(success); - job.done(success, { repeatId: true }); + if (carts && carts.length) { + const totalLength = carts.length; + let currentRun = 0; + carts.forEach((cart) => { + const account = Accounts.findOne({ _id: cart.accountId }); + if (account) { + const removeCarts = Cart.remove({ accountId: account._id }); + if (account) { + if (!account.emails.length && removeCarts) { + Accounts.remove({ + _id: account._id, + emails: [] + }); + Hooks.Events.run("afterAccountsRemove", null, account._id); + Meteor.users.remove({ _id: account.userId, emails: [] }); // clears out anonymous user + Logger.info("Stale anonymous user cart and account successfully cleaned"); + } else if (removeCarts) { + Logger.info("Stale anonymous user cart and account successfully cleaned"); + } + } + } else { + Cart.remove({ _id: cart._id }); + Logger.info("Removed just this cart"); } - } else { - const success = "Stale user cart successfully cleaned"; - Logger.debug(success); - job.done(success, { repeatId: true }); - } - }); + currentRun += 1; + job.progress( + currentRun, + totalLength, + { echo: true } + ); + }); + } else { Logger.info("No carts found"); } + const success = "Cart cleanup job completed"; + Logger.info(success); + job.done(success); } else { Logger.debug("No cart cleanup schedule"); } From 4fe2350ae12f0501ae510a7422da65334deb2cba Mon Sep 17 00:00:00 2001 From: William Moss <33007279+willmoss1000@users.noreply.github.com> Date: Fri, 30 Nov 2018 00:37:54 +0000 Subject: [PATCH 2/2] Simplify job --- .../included/jobcontrol/server/jobs/cart.js | 44 +------------------ 1 file changed, 2 insertions(+), 42 deletions(-) diff --git a/imports/plugins/included/jobcontrol/server/jobs/cart.js b/imports/plugins/included/jobcontrol/server/jobs/cart.js index 4708773bd2d..3e27d9fb881 100644 --- a/imports/plugins/included/jobcontrol/server/jobs/cart.js +++ b/imports/plugins/included/jobcontrol/server/jobs/cart.js @@ -1,18 +1,10 @@ import Hooks from "@reactioncommerce/hooks"; import Logger from "@reactioncommerce/logger"; -import { Meteor } from "meteor/meteor"; -import { Accounts, Cart, Jobs } from "/lib/collections"; +import { Cart, Jobs } from "/lib/collections"; import Reaction from "/imports/plugins/core/core/server/Reaction"; import { Job } from "/imports/plugins/core/job-collection/lib"; import moment from "moment"; -/** - * @param {Object} olderThan older than date - * @return {Object} stale carts - * @private - */ -const getStaleCarts = (olderThan) => Cart.find({ updatedAt: { $lte: olderThan } }).fetch(); - /** * @summary Adds an afterCoreInit hook for removing stale carts * @returns {undefined} @@ -56,39 +48,7 @@ export function cartCleanupJob() { const schedule = (settings.cart.cleanupDurationDays).match(/\d/);// configurable in shop settings const olderThan = moment().subtract(Number(schedule[0]), "days")._d; Logger.info("removing carts older than", olderThan); - const carts = getStaleCarts(olderThan); - if (carts && carts.length) { - const totalLength = carts.length; - let currentRun = 0; - carts.forEach((cart) => { - const account = Accounts.findOne({ _id: cart.accountId }); - if (account) { - const removeCarts = Cart.remove({ accountId: account._id }); - if (account) { - if (!account.emails.length && removeCarts) { - Accounts.remove({ - _id: account._id, - emails: [] - }); - Hooks.Events.run("afterAccountsRemove", null, account._id); - Meteor.users.remove({ _id: account.userId, emails: [] }); // clears out anonymous user - Logger.info("Stale anonymous user cart and account successfully cleaned"); - } else if (removeCarts) { - Logger.info("Stale anonymous user cart and account successfully cleaned"); - } - } - } else { - Cart.remove({ _id: cart._id }); - Logger.info("Removed just this cart"); - } - currentRun += 1; - job.progress( - currentRun, - totalLength, - { echo: true } - ); - }); - } else { Logger.info("No carts found"); } + Cart.remove({ updatedAt: { $lte: olderThan } }, { multi: true }); const success = "Cart cleanup job completed"; Logger.info(success); job.done(success);