peppeocchi / php-cron-scheduler

PHP cron job scheduler
MIT License
808 stars 143 forks source link

Run script at different times depending on conditions #77

Closed jamminjames closed 5 years ago

jamminjames commented 5 years ago

I have a php script I want to run once a month or every day, depending on the value of certain values in a database.

I have no problem getting the variables into scheduler.php, but can't seem to write a function that will accomplish the above.

I've tried various ways of using the function lines you present, but can't get the result I need. What is the best way to do this?

It's not clear from the instructions if I should chain the command all into one thing, but maybe that's the key, like this, which I've seen in another cron scheduler:

$schedule = new Scheduler();

$schedule->php('filetorun.php')
    ->at("10 10 10 * *"); 
        ->when(function() {
             if ($strcron) { return true; }
         });
    ->output(['logs/cron_renewalemail.log'])
         ->run();

Thank you!

rafaelmb commented 5 years ago

is $strcron outside your closure? if it this you have to ineject it inside to use it properly

$schedule = new Scheduler();

$schedule->php('filetorun.php')
    ->at("10 10 10 * *"); 
        ->when(function() use (strcron) {
             if ($strcron) { return true; }
         });
    ->output(['logs/cron_renewalemail.log'])
         ->run();
peppeocchi commented 5 years ago

@jamminjames I think @rafaelmb got a point here, if you want to use a external variable in a Closure you have to specify it like the below

->when(function() use ($myExternalVariable) {
    # ....
})

I have to be honest I haven't tried your code yet but the when method works so there is no reason why in you code it shouldn't. Can you try the suggested solution and see if that works for you now?

thanks @rafaelmb!

jamminjames commented 5 years ago

Yes, that works, thank you!