msavin / SteveJobs

A simple jobs queue that just works (for Meteor.js)
Other
207 stars 35 forks source link

Jobs: Unable to execute job - queue is busy: #91

Closed kpervin closed 2 years ago

kpervin commented 4 years ago

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?

kpervin commented 4 years ago

@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?

msavin commented 4 years ago

Sounds your job is not terminating... can you post the code Sent from my iPhone

On Sep 16, 2020, at 8:27 PM, ShaggyKris notifications@github.com wrote:

 @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?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.

kpervin commented 4 years ago
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;
}
kpervin commented 4 years ago

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.

Gorbus commented 3 years ago

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

msavin commented 2 years ago

should be fixed in new version

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