Skip to content

Instantly share code, notes, and snippets.

@TvL2386
Last active January 8, 2018 04:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TvL2386/67fe243079cbdb6c3e7d0c0a11509782 to your computer and use it in GitHub Desktop.
Save TvL2386/67fe243079cbdb6c3e7d0c0a11509782 to your computer and use it in GitHub Desktop.
my scheduler
$ php artisan schedule:daemon
[2018-01-07 12:11:24] Starting schedule:daemon
[2018-01-07 12:11:24] Waiting for next minute
[2018-01-07 12:12:00] running main loop
Running scheduled command: App\Jobs\CreateSnapshot
[2018-01-07 12:13:00] running main loop
Running scheduled command: App\Jobs\CreateSnapshot
[2018-01-07 12:14:00] running main loop
Running scheduled command: App\Jobs\CreateSnapshot
[2018-01-07 12:15:00] running main loop
Running scheduled command: App\Jobs\CreateSnapshot
[2018-01-07 12:16:00] running main loop
Running scheduled command: App\Jobs\CreateSnapshot
[2018-01-07 12:17:00] running main loop
Running scheduled command: App\Jobs\CreateSnapshot
[2018-01-07 12:18:00] running main loop
Running scheduled command: App\Jobs\CreateSnapshot
[2018-01-07 12:19:00] running main loop
Running scheduled command: App\Jobs\CreateSnapshot
[2018-01-07 12:20:00] running main loop
Running scheduled command: App\Jobs\UpdateCurrencies
Running scheduled command: App\Jobs\CreateSnapshot
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Console\Scheduling\ScheduleRunCommand;
use Illuminate\Console\Scheduling\Schedule;
class start_scheduler extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'schedule:daemon';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Start a scheduler daemon that runs every minute';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$this->log("Starting schedule:daemon");
$this->log("Waiting for next minute");
sleep($this->seconds_until_next_minute());
# The main loop
while (true) {
$this->loop();
sleep($this->seconds_until_next_minute());
}
}
private function loop() {
$this->log("running main loop");
echo `php artisan schedule:run`;
}
private function seconds_until_next_minute() {
return (60-time()%60);
}
private function carbon() {
return \Carbon\Carbon::now()->toDateTimeString();
}
private function log($message) {
$this->info(sprintf("[%s] %s", $this->carbon(), $message));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment