peppeocchi / php-cron-scheduler

PHP cron job scheduler
MIT License
811 stars 144 forks source link

Task running before the previous task is finished #105

Open elvispdosreis opened 4 years ago

elvispdosreis commented 4 years ago
<?php

this task will always be performed

   $scheduler->call(function () use (&$mercadolivre) {
        $mercadolivre->duplicateAttributes();
    })->at('0 0,12 * * *')->then(function ($output) use (&$logger) {
        if (!empty($output)) {
            $logger->error("MercadoLivre Duplicate Attributes - {$output}");
        }
    }, true);

I need the task to run only once until the end of the execution, even if the scheduler is called, this task should not be executed, before it ends

    $scheduler->call(function () use (&$mercadolivre) {
        $mercadolivre->messaging();
    })->at('* * * * *')->then(function ($output) use (&$logger) {
        if (!empty($output)) {
            $logger->error("MercadoLivre Messaging - {$output}");
        }
    }, true)->onlyOne();

as it is today, it is always executed.

peppeocchi commented 4 years ago

@elvispdosreis I am not sure I am following, you have 2 tasks running, you want one of the two to run only once, is that correct? And the second currently gets executed multiple times even though you are calling the onlyOne?

You should check if the the lock files are being generated correctly, they're based on a signature of the function, it would be better to pass a custom identifier (https://github.com/peppeocchi/php-cron-scheduler#scheduling-jobs)

Just keep in mind that because you're using Closures, your tasks will run synchronously, so the second one will run after the first one is complete. My suggestion would be to call external scripts and schedule those external scripts so they can both run asynchronously.

To recap:

elvispdosreis commented 4 years ago

The scheduler is creating the lock file correctly with the task id, he is creating tasks

$scheduler->call(function (MercadoLivre $mercadolivre) {
        $mercadolivre->messaging();
    }, [$mercadolivre], 'mercadoLivre-messaging')->at('* * * * *')->then(function ($output) {
        // ...
    }, true)->onlyOne(ROOT_PATH . '/tmp');

however if I kill the process or he dies the lock file continues

image