Использование Agenda с промежуточным ПО mon goose - PullRequest
0 голосов
/ 17 июня 2020

Я пытаюсь передать повестку дня внутри промежуточного программного обеспечения Moongose ​​"pre" "save", но получаю эту ошибку

Предупреждение: доступ к несуществующему свойству 'create 'экспорта модуля внутри круговой зависимости

и повестка дня не создается

Это

повестка дня. js

const Agenda = require('agenda')
const mongoose = require('mongoose')

var agenda = new Agenda({
     mongo: mongoose.connection ,
      db:{collection: 'scheduledTasks'},
      processEvery:'5 seconds'

    });

let jobTypes = ["increase_energy"];

jobTypes.forEach(type => {
    require('../scheduled_tasks/' + type)(agenda)
})

if (jobTypes.length) {
    agenda.start()
}




module.exports = agenda;

Это

прирост_энергии. js

const Profile = require('../models/profile')

const clamp = (val, min, max) => {
    return val > max ? max : val < min ? min : val;
}

module.exports = (agenda) => {
    agenda.define('increase energy', { priority: 'high', concurrency: 10 }, async job => {

        const usernameID = job.attrs.data.userId;
        const myProfile = await Profile.findOne({ usernameID: usernameID })
        const oldEnergy = myProfile.energy

        if (oldEnergy >= 100) {
            console.log('Done')
            await job.remove()
            return
        }

        myProfile.energy = oldEnergy + 10
        myProfile.energy = clamp(myProfile.energy, 0, 100)
        await myProfile.save()



    });
}

Это часть

профиль. js

profileSchema.pre('save', async function (next) {
    const profile = this
    // console.log(JSON.stringify(profile) )

    if (profile.isModified('energy')) {
        // console.log('run')
        await agenda.create('increase energy', { userId: profile.usernameID, firstTime: true })
            .repeatEvery('10 seconds')
            .unique({ 'data.userId': profile.usernameID })
            .save();

    }

    next()
})
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...