msavin/SteveJobs

Jobs: Unable to execute job - queue is busy:

kpervin opened this issue · 6 comments

Currently no jobs will run, and Jobs: Unable to execute job - queue is busy: error is displayed in the log anytime they are run (even after cancellation). Is there a way to view the queue to see what's going on?

@msavin

Can I please get some help with this? This is causing issues in production and I am unsure how to resolve this. I have attempted to restart the queue via stopping the job using Jobs.stop("job_name"), starting it again via Jobs.start("job_name"), then calling Jobs.run("job_name").

What can I do to get rid of the Jobs: Unable to execute job - queue is busy: message and actually run the job?

Jobs.register({
    ...
    noticeToAccountingExport: function () {
        try {
            let columns = [
                [
                    "Veterinary Practice",
                    "Registration Status",
                    "Address",
                    "Administrator",
                    "Administrator Email",
                    "Pet Name",
                    "Pet Guardian",
                    "Account Number",
                    "Campaign ID",
                    "Start Date",
                    "End Date",
                    "Sponsor Name",
                    "Campaign Type",
                    "Pre-Approved",
                    "Donations",
                    "Sponsor Match",
                    "Adjustments",
                    "Max Fee Deductible",
                    "Max Payment",
                ],
            ];
            let data = [];
            const campaigns = Campaigns.find({
                campaignStatus: {
                    $in: ["closed", "closed au", "closed aw"]
                },
                endDate: {
                    $gte: moment()
                        .subtract(1, "month")
                        .toDate(),
                },
            }).fetch();
            const length = campaigns.map((campaign) => {
                const user = campaign.getInitiator();
                const campaignInitiator = Roles.userIsInRole(user, "non-profit") ?
                    user.profile.organizationName :
                    `${user.profile.username} ${user.profile.lastName}`;
                const {
                    clinicName
                } = campaign.clinic;
                const clinic = campaign.getClinic();
                const feeToBeDeducted =
                    campaign.totalFunded * 0.15 < 50 ? campaign.totalFunded * 0.15 : 50;
                let sumOfAdjustments = 0;
                if (campaign.fundingAdjustment && campaign.fundingAdjustment.length > 0) {
                    campaign.fundingAdjustment.forEach(({
                        fundingAdjustmentAmount
                    }) => {
                        sumOfAdjustments += fundingAdjustmentAmount;
                    });
                }

                let preApproved = "",
                    sponsorMatch = "";
                if (campaign.sponsors.sponsorID !== "") {
                    preApproved = campaign.sponsors.seedAmount;
                    if (campaign.sponsors.sponsorshipType !== "Seed Donation") {
                        sponsorMatch = Accounting.formatMoney(
                            campaign.sponsors.totalSponsorFunds -
                            Number(campaign.sponsors.seedAmount), {
                                precision: 2
                            }
                        );
                    }
                }

                data.push([
                    clinicName,
                    clinic ? "Registered" : "Not Registered",
                    clinic ? clinic.address1 : "",
                    campaign.clinic.clinicContactName,
                    campaign.clinic.clinicEmail,
                    campaign.pet.name,
                    campaignInitiator,
                    campaign.accountId,
                    campaign._id._str,
                    campaign.startDate
                        ?
                moment(campaign.startDate).format(standard_date_format)
                        :
                "",
                    campaign.endDate ? moment(campaign.endDate).format(standard_date_format) : "",
                    campaign.sponsors.organizationName,
                    campaign.sponsors.type !== "default"
                        ?
                campaign.sponsors.organizationName
                        :
                "Standard",
                    Accounting.formatMoney(preApproved, {
                        precision: 2
                    }),
                    Accounting.formatMoney(campaign.totalDonations, {
                        precision: 2
                    }),
                    Accounting.formatMoney(sponsorMatch, {
                        precision: 2
                    }),
                    Accounting.formatMoney(sumOfAdjustments, {
                        precision: 2
                    }),
                    Accounting.formatMoney(feeToBeDeducted, {
                        precision: 2
                    }),
                    Accounting.formatMoney(campaign.totalFunded, {
                        precision: 2
                    }),
                ]);
            }).length;
            if (length === 0) {
                data.push(["No campaigns closed in past 30 days"]);
            }
            const CSVHead = buildHead(columns);
            const CSVBody = buildBody(data);
            const csv = `${CSVHead}${CSVBody}`.trim();
            let csvContent = encodeURI(`data:text/csv;charset=utf-8,${csv}`);

            const text = `This email will be sent again next Monday.`;

            let to = "email@domain.com"

            console.log(`Finished processing ${length} campaigns for csv. Emailing to ${to}`);
            Email.send({
                to,
                from: "anotherEmail@doman.com",
                subject: `Notice To Accounting, Closed Campaigns Past 30 Days ${moment().format(
                    "dddd, MMMM Do YYYY, h:mm:ss a"
                )}`,
                text,
                attachments: [
                    {
                        filename: `Notice To Accounting, Closed Campaigns Past 30 Days ${moment().format(
                            "dddd, MMMM Do YYYY, h:mm:ss a"
                        )}.csv`,
                        path: csvContent,
                    },
                ],
            });
            const date = getDay();
            this.reschedule({
                date,
                // on: { hours: 4, minutes: 0 }, //UTC 04:00 = 00:00 EDT
            });
        } catch (e) {
            logger.error(e.stack);
            return this.failure(e.stack);
        }
        console.log(`\n=> Finished noticeToAccountingExport.\n`);
    },
})


This is the getDay function, for further clarification.

function getDay(dayINeed = 1 /*for Monday*/) {
    const today = moment().isoWeekday();
    let date;
    // if we haven't yet passed the day of the week that I need:
    if (today < dayINeed) {
        // then just give me this week's instance of that day
        date = moment()
            .isoWeekday(dayINeed)
            .utc()
            .hour(4)
            .minute(0) //UTC 04:00 = 00:00 EDT
            .toDate();
    } else {
        // otherwise, give me *next week's* instance of that same day
        date = moment()
            .add(1, "weeks")
            .isoWeekday(dayINeed)
            .utc()
            .hour(4)
            .minute(0) //UTC 04:00 = 00:00 EDT
            .toDate();
    }
    return date;
}

Here's the thing, though. It has worked in the past. It just doesn't seem to work after a certain period of time, which I am unsure why that is the case.

This isn't the only job, either. It seems to be every job that I have.

Shouldn't you have a this.success() at the end of your function?

should be fixed in new version

@Gorbus this.success() is now automated if function runs successfully