Git Product home page Git Product logo

Comments (7)

liebig avatar liebig commented on July 25, 2024

Hello mockingbirdnj83,

Thanks for using Cron. Please follow the README. Since version 1 of Cron you can use a build in route and don't have to create a own route. For this you can define your jobs in the /path/to/laravel/app/start/global.php file like

Event::listen('cron.collectJobs', function() {
    Cron::add('example1', '* * * * *', function() {
                    // Do some crazy things unsuccessfully every minute
                    return 'No';
                });

    Cron::add('example2', '*/2 * * * *', function() {
        // Do some crazy things successfully every two minute
        return null;
    });

    Cron::add('disabled job', '0 * * * *', function() {
        // Do some crazy things successfully every hour
    }, false);
});

Now please generate a route security key via php artisan cron:keygen and save this to the config file. Now you need for example an online cronjob service which runs this route (http://exampledomain.com/cron.php?key=YOURKEY) every minute. In comparison to a Java application server for example, PHP only runs if it is executed. If crontab or an online cronjob service provider calls PHP and starts the application, Cron can execute the jobs and will start the work. If PHP is not started, the application sleeps and nothing happens.

After this is done you should have a new line in the cron_manager table every minute.

Best Regards,
Marc

from cron.

mockingbirdnj83 avatar mockingbirdnj83 commented on July 25, 2024

Thanks to answer me so quickly.
Yes, I read the README a lot of time before posting this issue.
It works when I do it in global.php. But I want to automate the Cron creation. I can't do this by hand.

Let me explain : I have a form with fields corresponding to the parameters of my script. When I get these datas from the form, I would like to open a Cron that create a command line with them, and use shell_exec() to call my script with this command line. (I have shell access).

Something like this :

Cron::add('alert8', $pattern, function($parameter) {
    $cmd = './script/main.php';
    if ($parameter->foo == 1) {
        $cmd += ' -foo';
    } else if ($parameter->bar) {
        $cmd += " -bar $parameter->bar";
    }
    exec($cmd);
    return null;
}, true);

If I must use Cron:add in global.php I can't automate the Cron creation (without opening and writing in the file with php. And I would like to avoid this). It's why I'm wondering if there is a way to call Cron:add elsewhere than global.php ?

from cron.

liebig avatar liebig commented on July 25, 2024

Okay I understand that you want to add Cron jobs dynamically. But I don't understand the way you do this. If you send the form, you will add a Cron job and you will start Cron. After this is done and the request is handled, PHP will shut down and delete all the memory. So your added Cron job is gone because it is only added to the memory at runtime. For that reason you have to save the dynamic Cron job information somewhere else. I would recommend a database for this.

For dynamic Cron jobs your database table cron_definitions should have the fields ID, NAME, EXPRESSION and FUNCTION. Now add to the /path/to/laravel/app/start/global.php file something like

Event::listen('cron.collectJobs', function() {
    // TODO Run through cron_definitions via foreach and execute
    Cron:add($name, $expression, function() use ($function) {
        $return = eval($function);
        if ($return === FALSE) {
            return 'Parse Error of function ' . $function;
        } else {
            return $return;
        }
    });
});

Now you can use Crons build in route or the artisan command artisan cron:run to run the Cron job execution every minute. With a formular and a Laravel route you can create, read, update and delete the jobs dynamically.

Please note that the eval function could be a security risk.

I hope this helps.

Best Regards,
Marc

from cron.

mockingbirdnj83 avatar mockingbirdnj83 commented on July 25, 2024

Hello Liebig. Thanks a lot for your answers.

I understand what you said about the memory. And your idea is very relevant. It's definitely what I need.
I just have a quick question, what does that do ? :

function() use ($function)

I never seen this before.

One more time, thanks.
You really helped me.

from cron.

liebig avatar liebig commented on July 25, 2024

Hi mockingbirdnj83,
You are welcome. My variable names are poor. The $name, $expression and $function variables came from the database. You need these for Cron. You add the $function string to the anonymous function, which will be called if the expression matchs the current timestamp. This anonymous function will execute the $function via eval. So you need use to pass the database function string to the anonymous function with function() use ($function) {. You can read more about anonymous functions here. I hope this helps you.

from cron.

mockingbirdnj83 avatar mockingbirdnj83 commented on July 25, 2024

Hello Liebig,
Thanks to you I was able to do what I want.
Your tool is great, and your help too.

Thanks again for it and for all your efforts.

Thomas.

from cron.

liebig avatar liebig commented on July 25, 2024

Hi Thomas,
Thanks for your feedback. You are welcome!

Best Regards
Marc Liebig

from cron.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.