xfra35 / f3-cron

Job scheduling for the PHP Fat-Free Framework
GNU General Public License v3.0
72 stars 22 forks source link

Please give example of $handler #2

Closed pixeline closed 8 years ago

pixeline commented 8 years ago

I'm not sure I understand how to define the job. For instance, i have this line:

$cron->set('Job1','App->job2','*/5 * * * *'); // runs every 5 minutes

What function would App->job2 actually call ? Could you provide a brief "hello world" example?

My confusion is probably due to the fact that I usually use route() with anonymous functions. I guess for this syntax example to work, I'd need to create an $App class with job2() as method?

Thank you!!!

xfra35 commented 8 years ago

App->job2 contains your business code. It could be anything: send automated emails, auto-update order status, archive old data, send reports, run a watchdog, auto-deploy website, etc.

The code handler is not expected to return anything. It is just passed one argument: the framework instance.

Here's a simple "hello world" example:

class Hello {

  function world(Base $f3) {
    $str=sprintf('Hello world, it\'s %s at the moment.',strftime('%X'));
    $f3->write($f3->TEMP.'hello-world.txt',$str,TRUE);
  }

}

$cron=Cron::instance();
$cron->set('HelloWorld','Hello->world','* * * * *'); // runs every minute

Every minute, this job will append one line to the file hello-world.txt located in the TEMP folder.

pixeline commented 8 years ago

Awesome, thank you! And thank you for such a prompt reply!