Code Monkey home page Code Monkey logo

Comments (3)

liebig avatar liebig commented on July 16, 2024

There are two options to load jobs from database. The first option is to load the cron expression and the function from database. In my opinion this is a bad idea because someone with database access can easily manipulate the whole application. Your database schema could look like:

Schema::create('jobs', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('expression');
    $table->string('function');
    $table->boolean('enabled');
});

After creating the Eloquent model Job class file for the jobs table you could add all cron jobs in the AppServiceProvider.php file like this:

public function boot() {
    \Event::listen('cron.collectJobs', function() {

        $jobs = App\Job::all();

        foreach ($jobs as $job) {
            \Cron::add($job->name, $job->expression, function() {
                eval($job->function)
            }, $job->enabled);
        }
    });
}

The second and safer way is to load only the expression from database.

Schema::create('jobs', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('expression');
    $table->boolean('enabled');
});
public function boot() {
    \Event::listen('cron.collectJobs', function() {

        $example1 = App\Job::where('name', 'example1')->firstOrFail();

        \Cron::add('example1', $example1->expression, function() {
                // Do some crazy things successfully
                return null;
            }, $job->enabled);
    });
}

The code is not tested but I hope you get the concept. I prefer the second solution although it is not as dynamic as the first one.

from cron.

larsbo avatar larsbo commented on July 16, 2024

Thank you very much for the detailed answer!

Your security concerns are a very good point, too. I thought about that and I think with a function whitelist you can have a compromise between the two options.

from cron.

liebig avatar liebig commented on July 16, 2024

Yes, I whitelist is maybe a good choice.

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.