Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
therezor committed Jul 19, 2018
0 parents commit 9df8dac
Show file tree
Hide file tree
Showing 10 changed files with 942 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor
/.idea
.DS_Store
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions README.md
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();
}
?>
```
28 changes: 28 additions & 0 deletions composer.json
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"
]
}
}
}
18 changes: 18 additions & 0 deletions config/database-schedule.php
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,
],
];
36 changes: 36 additions & 0 deletions migrations/2018_07_17_172600_create_schedule_table.php
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'));
}
}
40 changes: 40 additions & 0 deletions src/Console/ScheduleRunCommand.php
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();
});
}
}
44 changes: 44 additions & 0 deletions src/Models/Schedule.php
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');
}
}
18 changes: 18 additions & 0 deletions src/Observer/ScheduleObserver.php
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);
}
}
40 changes: 40 additions & 0 deletions src/ServiceProvider.php
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()
{
}
}

0 comments on commit 9df8dac

Please sign in to comment.