mirror of
https://github.com/henrywhitaker3/Speedtest-Tracker.git
synced 2026-01-02 11:07:20 +01:00
Added settings section and custom cron scheduling
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Console;
|
||||
|
||||
use App\Helpers\SettingsHelper;
|
||||
use App\Helpers\SpeedtestHelper;
|
||||
use App\Jobs\SpeedtestJob;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
@@ -26,9 +27,7 @@ class Kernel extends ConsoleKernel
|
||||
*/
|
||||
protected function schedule(Schedule $schedule)
|
||||
{
|
||||
$schedule->call(function() {
|
||||
SpeedtestJob::dispatch();
|
||||
})->hourlyAt(0);
|
||||
$schedule->job(new SpeedtestJob)->cron(SettingsHelper::get('schedule')['value']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -14,7 +14,8 @@ class SettingsHelper {
|
||||
} else if(sizeof($name) == 1) {
|
||||
return $name[0];
|
||||
} else {
|
||||
return $name;
|
||||
$name = $name->keyBy('name');
|
||||
return $name->all();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Helpers\SettingsHelper;
|
||||
use App\Rules\Cron;
|
||||
use App\Setting;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
@@ -11,7 +12,7 @@ class SettingsController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
return Setting::get();
|
||||
return Setting::get()->keyBy('name');
|
||||
}
|
||||
|
||||
public function get(Setting $setting)
|
||||
@@ -24,6 +25,10 @@ class SettingsController extends Controller
|
||||
$rule = [
|
||||
'name' => [ 'required', 'string', 'min:1' ],
|
||||
];
|
||||
if($request->name == 'schedule') {
|
||||
$rule['value'] = [ 'required', new Cron ];
|
||||
}
|
||||
|
||||
$validator = Validator::make($request->all(), $rule);
|
||||
if($validator->fails()) {
|
||||
return response()->json([
|
||||
|
||||
41
app/Rules/Cron.php
Normal file
41
app/Rules/Cron.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rules;
|
||||
|
||||
use Cron\CronExpression;
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
|
||||
class Cron implements Rule
|
||||
{
|
||||
/**
|
||||
* Create a new rule instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the validation rule passes.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function passes($attribute, $value)
|
||||
{
|
||||
return CronExpression::isValidExpression($value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation error message.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function message()
|
||||
{
|
||||
return 'The :attribute field must be a valid cron expression.';
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ class Setting extends Model
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name', 'value'
|
||||
'name', 'value', 'description'
|
||||
];
|
||||
|
||||
protected $table = 'settings';
|
||||
|
||||
Reference in New Issue
Block a user