davidroman0O / moleculer-cron

Moleculer Addons - Cron tasks
MIT License
38 stars 25 forks source link

Job is not running on runOnInit and onTick is ticked once #27

Closed habeeth closed 3 months ago

habeeth commented 7 months ago

I have updated node 14 to node 20, so I updated moleculer-cron:0.0.2 to .0.04, but the same is not working in 0.0.4. I have pasted the snippets for further analysis.

node -v 20.11.1

"moleculer": "0.14.32", "moleculer-cron": "0.0.4", dep - "cron":"1.8,2" "moleculer-web": "0.10.7",

import { Service, ServiceBroker, ServiceSchema } from 'moleculer'; -- 
import Cron from 'moleculer-cron';

export default class SchedulerService extends Service
{
    public constructor(public broker: ServiceBroker, schema: ServiceSchema<{}> = {})
    {
        super(broker);
        this.parseServiceSchema({
            name: 'scheduler',
            version: 1,
            mixins: [Cron],
            settings: {
                rest: '/',
                $noServiceNamePrefix: false,
                $noVersionPrefix: true,
            },
            hooks: {
                before: {

                },
            },
            actions: {
                // actions
            },
            methods: {
                // nothing
            },
            dependencies: [
                'config',
            ],
            started: async () =>
            {
            },
            crons: [
                {
                    name: 'AdapterConfigScheduler',
                    cronTime: '*/10 * * * *',
                    onTick: async () =>
                    {
                        console.log('ticked.............'); // PRINTING ONCE
                        await broker.call('config.networkAdapterConfig', {}, { parentCtx: null });
                    },
                    runOnInit: async () =>
                    {
                        console.log('runOnInit.............'); // NOT PRINTING
                        await broker.call('config.networkAdapterConfig', {}, { parentCtx: null });
                    },
                    timeZone: 'America/Los_Angeles',
                },
            ],
        });
    }
}

moleculer-cron\src\index.js

created() {
        this.$crons = [];
        if (this.schema.crons) {
            // `cron` changed their way of creating crons, so now it's a function that will proxy the instanciation
            this.$crons = this.schema.crons.map((job) => {
console.log('in create.') // PRINTS
                var instance_job = () => {
console.log('in create.') // NOT PRINTS, SERVER FREEZES
                    // cronTime, onTick, onComplete, start, timeZone, context, runOnInit, utcOffset, unrefTimeout
                    var cronjob = new cron.CronJob(
                        job.cronTime, // cronTime
                        job.onTick || (_ => {}), // onTick
                        job.onComplete || (_ => {}), // onComplete
                        job.manualStart || false, // start
                        job.timeZone, // timeZone
                        Object.assign(
                            this.broker,
                            {
                                getJob: this.getJob,
                            }
                        ), // context
                        job.runOnInit || (_ => {}), // runOnInit
                        job.utcOffset || null, // utcOffset
                        job.unrefTimeout || null, // unrefTimeout
                    )
                    cronjob.manualStart = job.manualStart || false
                    cronjob.name = job.name || this.makeid(20);
                    return cronjob;
                };

                return instance_job;
            });
        }
        return this.Promise.resolve();
    },
davidroman0O commented 4 months ago

Working on it, i will fix other things in the same time

davidroman0O commented 3 months ago

pushed