-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 9df8dac
Showing
10 changed files
with
942 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/vendor | ||
/.idea | ||
.DS_Store |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Laravel database schedule | ||
Store your schedules in database (cache friendly) | ||
|
||
## Installation | ||
|
||
1) Run ```composer require therezor/laravel-database-schedule``` in your laravel project root folder | ||
|
||
2) Run ```php artisan vendor:publish``` | ||
|
||
3) Apply migration ```php artisan migrate``` | ||
|
||
4) Add new Schedule command to your app/Console/Kernel.php | ||
|
||
```php | ||
<?php | ||
class Kernel extends ConsoleKernel | ||
{ | ||
/** | ||
* The Artisan commands provided by your application. | ||
* | ||
* @var array | ||
*/ | ||
protected $commands = [ | ||
\TheRezor\DatabaseSchedule\Console\ScheduleRunCommand::class, | ||
]; | ||
} | ||
?> | ||
``` | ||
|
||
5) Use `TheRezor\DatabaseSchedule\Models\Schedule` to manage your database schedule | ||
|
||
```php | ||
<?php | ||
$schedule = new Schedule(); | ||
$schedule->dailyAt('18:00'); | ||
$schedule->command = MyComand::class; | ||
$schedule->params = ['id' => 1]; | ||
$schedule->save(); | ||
} | ||
?> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "therezor/laravel-database-schedule", | ||
"description": "Add laravel task to your schedule strait from database.", | ||
"type": "library", | ||
"license": "MIT", | ||
"require": { | ||
"php": "^7.0", | ||
"laravel/framework": "5.5.* || 5.6.*" | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Roman Nagirnyi", | ||
"email": "rnagirnyi@smartcats.pro" | ||
} | ||
], | ||
"autoload": { | ||
"psr-0": { | ||
"TheRezor\\DatabaseSchedule\\": "src/" | ||
} | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"TheRezor\\DatabaseSchedule\\ServiceProvider" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
return [ | ||
/** | ||
* Table and Model used for schedule list | ||
*/ | ||
'table' => 'schedule', | ||
'model' => \TheRezor\DatabaseSchedule\Models\Schedule::class, | ||
|
||
/** | ||
* Cache settings | ||
*/ | ||
'cache' => [ | ||
'store' => 'file', | ||
'key' => 'database_schedule', | ||
'enabled' => true, | ||
], | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Config; | ||
|
||
class CreateScheduleTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create(Config::get('database-schedule.table', 'schedule'), function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->string('command'); | ||
$table->text('params'); | ||
$table->string('expression'); | ||
$table->boolean('even_in_maintenance_mode')->default(false); | ||
$table->boolean('without_overlapping')->default(false); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::drop(Config::get('database-schedule.table', 'schedule')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace TheRezor\DatabaseSchedule\Console; | ||
|
||
use Illuminate\Console\Scheduling\ScheduleRunCommand as BaseScheduleCommand; | ||
use TheRezor\DatabaseSchedule\Models\Schedule; | ||
use Illuminate\Support\Facades\Config; | ||
use Illuminate\Support\Facades\Cache; | ||
|
||
class ScheduleRunCommand extends BaseScheduleCommand | ||
{ | ||
public function handle() | ||
{ | ||
$schedules = Config::get('database-schedule.cache.enabled') ? $this->getFromCache() : Schedule::get()->toArray(); | ||
|
||
foreach ($schedules as $s) { | ||
$event = $this->schedule->command($s['command'], $s['params'])->cron($s['expression']); | ||
|
||
if ($s['even_in_maintenance_mode']) { | ||
$event->evenInMaintenanceMode(); | ||
} | ||
|
||
if ($s['without_overlapping']) { | ||
$event->withoutOverlapping(); | ||
} | ||
} | ||
|
||
parent::handle(); | ||
} | ||
|
||
protected function getFromCache() | ||
{ | ||
$store = Config::get('database-schedule.cache.store', 'file'); | ||
$key = Config::get('database-schedule.cache.key', 'database_schedule'); | ||
|
||
return Cache::store($store)->rememberForever($key, function () { | ||
return Schedule::get()->toArray(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace TheRezor\DatabaseSchedule\Models; | ||
|
||
use App\Filters\System\NotificationFilter; | ||
use EloquentFilter\Filterable; | ||
use Illuminate\Console\Scheduling\ManagesFrequencies; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Facades\Config; | ||
|
||
class Schedule extends Model | ||
{ | ||
use ManagesFrequencies; | ||
|
||
/** | ||
* The database table used by the model. | ||
* | ||
* @var string | ||
*/ | ||
protected $table; | ||
|
||
protected $attributes = [ | ||
'expression' => '* * * * *', | ||
'params' => '{}', | ||
]; | ||
|
||
protected $casts = [ | ||
'params' => 'array', | ||
]; | ||
|
||
/** | ||
* Creates a new instance of the model. | ||
* | ||
* @param array $attributes | ||
* @return void | ||
*/ | ||
public function __construct(array $attributes = []) | ||
{ | ||
parent::__construct($attributes); | ||
|
||
$this->table = Config::get('database-schedule.table', 'schedule'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace TheRezor\DatabaseSchedule\Observer; | ||
|
||
use Illuminate\Support\Facades\Config; | ||
use Illuminate\Support\Facades\Cache; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class ScheduleObserver | ||
{ | ||
public function saved(Model $model) | ||
{ | ||
$store = Config::get('database-schedule.cache.store', 'file'); | ||
$key = Config::get('database-schedule.cache.key', 'database_schedule'); | ||
|
||
Cache::store($store)->forget($key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace TheRezor\DatabaseSchedule; | ||
|
||
use Illuminate\Support\ServiceProvider as LaravelServiceProvider; | ||
use Illuminate\Support\Facades\Config; | ||
use TheRezor\DatabaseSchedule\Observer\ScheduleObserver; | ||
|
||
class ServiceProvider extends LaravelServiceProvider | ||
{ | ||
/** | ||
* Bootstrap any application services. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() | ||
{ | ||
$this->publishes([ | ||
__DIR__ . '/../config/database-schedule.php' => config_path('database-schedule.php'), | ||
], 'config'); | ||
|
||
$this->publishes([ | ||
__DIR__ . '/../migrations/' => database_path('migrations'), | ||
], 'migrations'); | ||
|
||
if (Config::get('database-schedule.cache.enabled')) { | ||
$model = Config::get('database-schedule.model'); | ||
$model::observe(ScheduleObserver::class); | ||
} | ||
} | ||
|
||
/** | ||
* Register any application services. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
} | ||
} |