Full rewrite Laravel style
This commit is contained in:
parent
cc03a0cf03
commit
416f5294e6
15 changed files with 375 additions and 237 deletions
|
@ -2,21 +2,29 @@
|
||||||
|
|
||||||
namespace App\Console\Commands;
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use \Exception;
|
||||||
|
use App\Models\Task;
|
||||||
|
use App\Models\TaskHistory;
|
||||||
|
use App\Models\Notification;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use \Exception;
|
use Illuminate\Support\Facades\Queue;
|
||||||
use Illuminate\Queue\Console\MonitorCommand;
|
|
||||||
|
|
||||||
class RunMonitoring extends Command
|
class RunMonitoring extends Command
|
||||||
{
|
{
|
||||||
private $rounds = 50;
|
private $rounds = 50;
|
||||||
|
private $max_tries = 3;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The name and signature of the console command.
|
* The name and signature of the console command.
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $signature = 'monitolite:monitoring:run {rounds?}';
|
protected $signature = 'monitolite:monitoring:run
|
||||||
|
{--rounds=50 : the number of tasks to handle in one run}
|
||||||
|
{--task= : the ID of an individual task to handle}
|
||||||
|
{--force : handles tasks even if they are pending}
|
||||||
|
';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The console command description.
|
* The console command description.
|
||||||
|
@ -48,20 +56,47 @@ class RunMonitoring extends Command
|
||||||
public function handle()
|
public function handle()
|
||||||
{
|
{
|
||||||
$count = 0;
|
$count = 0;
|
||||||
$rounds = $this->argument('rounds') ?? $this->rounds;
|
$rounds = $this->option('rounds') ?? $this->rounds;
|
||||||
|
$this->max_tries = env('NB_TRIES', $this->max_tries);
|
||||||
|
|
||||||
|
// If a force has been asked via command line
|
||||||
|
$force = false;
|
||||||
|
if (! empty($this->option('force'))) {
|
||||||
|
if (empty($this->option('task'))) {
|
||||||
|
if ($this->confirm('You asked me to force the execution (--force) but you did not specify a particular task ID (--task). I might have to handle a large amount of tasks. Are you sure?')) {
|
||||||
|
$force = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$force = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Getting pending tasks
|
// Getting pending tasks
|
||||||
$tasks = DB::table('tasks')
|
$tasks = Task::where(function($query) use ($force) {
|
||||||
->where(function($query) {
|
$query->whereRaw('DATE_SUB(NOW(), INTERVAL frequency SECOND) > executed_at');
|
||||||
$query->whereRaw('DATE_SUB(NOW(), INTERVAL frequency SECOND) > last_execution');
|
$query->orWhereBetween('attempts', [1, ($this->max_tries - 1)]);
|
||||||
$query->orWhereNull('last_execution');
|
$query->orWhereNull('executed_at');
|
||||||
|
|
||||||
|
if ($force === true) {
|
||||||
|
$query->orWhere('id', '>', 0);
|
||||||
|
}
|
||||||
})
|
})
|
||||||
->where('active', 1)
|
->where('active', 1)
|
||||||
->orderBy('last_execution', 'ASC')
|
->orderBy('attempts', 'DESC')
|
||||||
|
->orderBy('executed_at', 'ASC')
|
||||||
->take($rounds)
|
->take($rounds)
|
||||||
->get()
|
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
||||||
|
// If a particular task has been set via the command line
|
||||||
|
if (! empty($this->option('task'))) {
|
||||||
|
$tasks = $tasks->where('id', '=', $this->option('task'));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now getting tasks
|
||||||
|
$tasks = $tasks->get();
|
||||||
|
|
||||||
if (is_null($tasks) || count($tasks) == 0) {
|
if (is_null($tasks) || count($tasks) == 0) {
|
||||||
$this->info('No task to process, going back to sleep');
|
$this->info('No task to process, going back to sleep');
|
||||||
return true;
|
return true;
|
||||||
|
@ -74,19 +109,10 @@ class RunMonitoring extends Command
|
||||||
$bar->start();
|
$bar->start();
|
||||||
|
|
||||||
foreach ($tasks as $task) {
|
foreach ($tasks as $task) {
|
||||||
$last_status = $new_status = $output = null;
|
|
||||||
$bar->advance();
|
$bar->advance();
|
||||||
|
|
||||||
// Getting current task last status
|
// Getting current task last status
|
||||||
$query = DB::table('tasks_history')
|
$previous_status = $task->status;
|
||||||
->select('status')
|
|
||||||
->where('task_id', $task->id)
|
|
||||||
->orderBy('datetime', 'DESC')
|
|
||||||
->first()
|
|
||||||
;
|
|
||||||
if ($query !== false && ! is_null($query)) {
|
|
||||||
$last_status = $query->status;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
switch ($task->type) {
|
switch ($task->type) {
|
||||||
|
@ -103,13 +129,43 @@ class RunMonitoring extends Command
|
||||||
continue 2;
|
continue 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->saveHistory($task, true);
|
$history = $this->saveHistory($task, true);
|
||||||
}
|
}
|
||||||
catch(MonitoringException $e) {
|
catch(MonitoringException $e) {
|
||||||
$this->saveHistory($task, false, $e->getMessage());
|
$history = $this->saveHistory($task, false, $e->getMessage());
|
||||||
}
|
}
|
||||||
catch(Exception $e) {
|
catch(Exception $e) {
|
||||||
$this->saveHistory($task, false, $e->getMessage());
|
$history = $this->saveHistory($task, false, $e->getMessage());
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
// Changing task timestamps and status
|
||||||
|
$task->executed_at = $history->created_at; # Using the same timestamp as the task history
|
||||||
|
$task->attempts = $history->status == 1 ? 0 : $task->attempts + 1; # when success, resetting counter
|
||||||
|
/**
|
||||||
|
* We don't want to change the primary status in the task table
|
||||||
|
* as long as failed tasks have reached the max tries limit
|
||||||
|
* In the cast of a success, we can change the status straight away
|
||||||
|
*/
|
||||||
|
if ($history->status == 0 && $task->attempts >= $this->max_tries) {
|
||||||
|
$task->status = 0;
|
||||||
|
}
|
||||||
|
else if ($history->status === 1) {
|
||||||
|
$task->status = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! $task->save()) {
|
||||||
|
throw new Exception('Cannot save task details');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Task status has changed
|
||||||
|
// But not from null (new task)
|
||||||
|
if (! is_null($previous_status) && $task->status != $previous_status) {
|
||||||
|
// If host is up, no double-check
|
||||||
|
if ($task->status == 1 || ($task->status == 0 && $task->attempts == $this->max_tries)) {
|
||||||
|
Notification::addNotificationTask($history);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$bar->finish();
|
$bar->finish();
|
||||||
|
@ -117,42 +173,39 @@ class RunMonitoring extends Command
|
||||||
|
|
||||||
if (!empty($this->results)) {
|
if (!empty($this->results)) {
|
||||||
$this->table(
|
$this->table(
|
||||||
['Host', 'Result', 'Message'],
|
['ID', 'Host', 'Type', 'Result', 'Attempts', 'Message'],
|
||||||
$this->results
|
$this->results
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
final private function saveHistory($task, $status, $output = null) {
|
final private function saveHistory(Task $task, $status, $output = null) {
|
||||||
$date = date('Y-m-d H:i:s');
|
$date = date('Y-m-d H:i:s');
|
||||||
|
|
||||||
|
// Inserting new history
|
||||||
|
$insert = new TaskHistory;
|
||||||
|
$insert->status = $status === true ? 1 : 0;
|
||||||
|
$insert->created_at = $date;
|
||||||
|
$insert->output = $output ?? '';
|
||||||
|
$insert->task_id = $task->id;
|
||||||
|
if (! $insert->save()) {
|
||||||
|
throw new Exception('Cannot insert history for task #'.$task->id);
|
||||||
|
}
|
||||||
|
|
||||||
$this->results[] = [
|
$this->results[] = [
|
||||||
|
'id' => $task->id,
|
||||||
'host' => $task->host,
|
'host' => $task->host,
|
||||||
|
'type' => $task->type,
|
||||||
'result' => $status === true ? 'OK' : 'FAILED',
|
'result' => $status === true ? 'OK' : 'FAILED',
|
||||||
|
'attempts' => $task->attempts,
|
||||||
'message' => $output
|
'message' => $output
|
||||||
];
|
];
|
||||||
|
|
||||||
$insert = DB::table('tasks_history')
|
|
||||||
->insert([
|
|
||||||
'status' => $status === true ? 1 : 0,
|
|
||||||
'datetime' => $date,
|
|
||||||
'output' => $output ?? '',
|
|
||||||
'task_id' => $task->id
|
|
||||||
]
|
|
||||||
);
|
|
||||||
|
|
||||||
if (false !== $insert) {
|
return $insert;
|
||||||
DB::table('tasks')
|
|
||||||
->where('id', $task->id)
|
|
||||||
->update([
|
|
||||||
'last_execution' => $date
|
|
||||||
])
|
|
||||||
;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final private function checkPing($task) {
|
final private function checkPing(Task $task) {
|
||||||
if (! function_exists('exec') || ! is_callable('exec')) {
|
if (! function_exists('exec') || ! is_callable('exec')) {
|
||||||
throw new MonitoringException('The "exec" command is required');
|
throw new MonitoringException('The "exec" command is required');
|
||||||
}
|
}
|
||||||
|
@ -196,7 +249,11 @@ class RunMonitoring extends Command
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
final private function checkHttp($task) {
|
final private function checkHttp(Task $task) {
|
||||||
|
if (app()->environment() == 'local') {
|
||||||
|
//throw new MonitoringException('Forcing error for testing');
|
||||||
|
}
|
||||||
|
|
||||||
// Preparing cURL
|
// Preparing cURL
|
||||||
$opts = [
|
$opts = [
|
||||||
CURLOPT_HTTPGET => true,
|
CURLOPT_HTTPGET => true,
|
||||||
|
|
|
@ -125,7 +125,7 @@ class SyncCustomers extends Command
|
||||||
|
|
||||||
// Inserting contacts
|
// Inserting contacts
|
||||||
foreach ($contacts as $c) {
|
foreach ($contacts as $c) {
|
||||||
app('db')->insert('INSERT INTO notifications (`task_id`, `contact_id`) VALUES (:task_id, :contact_id)', [
|
app('db')->insert('INSERT INTO contact_task (`task_id`, `contact_id`) VALUES (:task_id, :contact_id)', [
|
||||||
'task_id' => $task_id,
|
'task_id' => $task_id,
|
||||||
'contact_id' => $c->id
|
'contact_id' => $c->id
|
||||||
]);
|
]);
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use App\Models\Task;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
|
||||||
|
@ -22,14 +23,23 @@ class ApiController extends Controller
|
||||||
public function getTasks() {
|
public function getTasks() {
|
||||||
$tasks = [];
|
$tasks = [];
|
||||||
|
|
||||||
$query = DB::select('
|
$query = Task
|
||||||
SELECT DISTINCT t.id, t.host, t.type, t.params, t.frequency, t.creation_date, t.last_execution, t.active, t.group_id, h.status, h.output, g.name as group_name
|
::leftJoin('groups', 'groups.id', 'tasks.group_id')
|
||||||
FROM `tasks` as t
|
->leftJoinSub(
|
||||||
LEFT JOIN `tasks_history` as h ON (h.task_id = t.id)
|
DB::table('task_history')
|
||||||
LEFT JOIN `groups` as g ON (g.id = t.group_id)
|
->select('id', DB::raw('MAX(created_at) as created_at'), 'output', 'status', 'task_id')
|
||||||
WHERE (t.last_execution IS NULL OR h.datetime = t.last_execution)
|
->groupBy('id')
|
||||||
ORDER BY group_name ASC
|
, 'task_history', function($join) {
|
||||||
');
|
$join
|
||||||
|
->on('task_history.task_id', '=', 'tasks.id')
|
||||||
|
;
|
||||||
|
})
|
||||||
|
->select(
|
||||||
|
'tasks.id', 'tasks.host', 'tasks.status', 'tasks.type', 'tasks.params', 'tasks.frequency', 'tasks.created_at', 'tasks.executed_at', 'tasks.active', 'tasks.group_id',
|
||||||
|
'task_history.output',
|
||||||
|
'groups.name as group_name')
|
||||||
|
->get()
|
||||||
|
;
|
||||||
|
|
||||||
foreach ($query as $t) {
|
foreach ($query as $t) {
|
||||||
if (is_null($t->group_id)) {
|
if (is_null($t->group_id)) {
|
||||||
|
@ -56,41 +66,42 @@ class ApiController extends Controller
|
||||||
|
|
||||||
|
|
||||||
public function getTaskDetails($id) {
|
public function getTaskDetails($id) {
|
||||||
$query = DB::select('
|
|
||||||
SELECT t.id, t.host, t.type, t.params, t.frequency, t.creation_date, t.last_execution, t.active, t.group_id, h.status, h.output, g.name as group_name
|
|
||||||
FROM `tasks` as t
|
|
||||||
LEFT JOIN `tasks_history` as h ON (h.task_id = t.id)
|
|
||||||
LEFT JOIN `groups` as g ON (g.id = t.group_id)
|
|
||||||
WHERE (t.last_execution IS NULL OR h.datetime = t.last_execution) AND t.id = :task_id
|
|
||||||
LIMIT 1
|
|
||||||
', [
|
|
||||||
'task_id' => $id
|
|
||||||
]);
|
|
||||||
|
|
||||||
if ($query) {
|
$task = Task
|
||||||
foreach ($query as $q) {
|
::leftJoin('groups', 'groups.id', 'tasks.group_id')
|
||||||
return response()->json($q);
|
->leftJoinSub(
|
||||||
}
|
DB::table('task_history')
|
||||||
|
->select('id', DB::raw('MAX(created_at) as created_at'), 'output', 'status', 'task_id')
|
||||||
|
->groupBy('id')
|
||||||
|
, 'task_history', function($join) {
|
||||||
|
$join
|
||||||
|
->on('task_history.task_id', '=', 'tasks.id')
|
||||||
|
;
|
||||||
|
})
|
||||||
|
->select(
|
||||||
|
'tasks.id', 'tasks.host', 'tasks.status', 'tasks.type', 'tasks.params', 'tasks.frequency', 'tasks.created_at', 'tasks.executed_at', 'tasks.active', 'tasks.group_id',
|
||||||
|
'task_history.output',
|
||||||
|
'groups.name as group_name')
|
||||||
|
->findOrFail($id)
|
||||||
|
;
|
||||||
|
|
||||||
|
if (! is_null($task)) {
|
||||||
|
return response()->json($task);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toggleTaskStatus(Request $request, $id) {
|
public function toggleTaskStatus(Request $request, $id) {
|
||||||
if($active = $request->input('active')) {
|
$active = $request->input('active', null);
|
||||||
//throw new ApiException('Invalid parameters');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (is_null($active)) {
|
||||||
|
throw new ApiException('Invalid parameters');
|
||||||
|
}
|
||||||
$active = intval($active);
|
$active = intval($active);
|
||||||
|
|
||||||
$query = DB::update('
|
$task = Task::findOrFail($id);
|
||||||
UPDATE tasks
|
$task->active = $active;
|
||||||
SET active = :active
|
|
||||||
WHERE id = :id
|
|
||||||
', [
|
|
||||||
'active' => $active,
|
|
||||||
'id' => $id
|
|
||||||
]);
|
|
||||||
|
|
||||||
if ($query !== false) {
|
if ($task->save()) {
|
||||||
return $this->getTaskDetails($id);
|
return $this->getTaskDetails($id);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
|
@ -1,26 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Jobs;
|
|
||||||
|
|
||||||
class ExampleJob extends Job
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* Create a new job instance.
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute the job.
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function handle()
|
|
||||||
{
|
|
||||||
//
|
|
||||||
}
|
|
||||||
}
|
|
22
app/Models/Contact.php
Normal file
22
app/Models/Contact.php
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Contact extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that are mass assignable.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $fillable = [];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
24
app/Models/Group.php
Normal file
24
app/Models/Group.php
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Group extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that are mass assignable.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $fillable = [];
|
||||||
|
|
||||||
|
|
||||||
|
public function tasks() {
|
||||||
|
return $this->hasMany('App\Models\Task');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
40
app/Models/Notification.php
Normal file
40
app/Models/Notification.php
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Notification extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that are mass assignable.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $fillable = [];
|
||||||
|
|
||||||
|
public function contact() {
|
||||||
|
return $this->belongsTo('App\Models\Contact');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function task_history() {
|
||||||
|
return $this->belongsTo('App\Models\TaskHistory');
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function addNotificationTask(TaskHistory $history) {
|
||||||
|
$contacts = $history->task->contacts()->get();
|
||||||
|
if (! is_null($contacts)) {
|
||||||
|
foreach ($contacts as $c) {
|
||||||
|
$notification = new Notification;
|
||||||
|
$notification->contact_id = $c->id;
|
||||||
|
$notification->task_history_id = $history->id;
|
||||||
|
$notification->status = 'pending';
|
||||||
|
$notification->save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
40
app/Models/Task.php
Normal file
40
app/Models/Task.php
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class Task extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that are mass assignable.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $fillable = [];
|
||||||
|
|
||||||
|
public $timestamps = [
|
||||||
|
'created_at',
|
||||||
|
'updated_at',
|
||||||
|
'executed_at'
|
||||||
|
];
|
||||||
|
|
||||||
|
public function group() {
|
||||||
|
return $this->belongsTo('App\Models\Group');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function notifications() {
|
||||||
|
return $this->hasMany('App\Models\Notification');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function contacts() {
|
||||||
|
return $this->belongsToMany('App\Models\Contact');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function history() {
|
||||||
|
return $this->hasMany('App\Models\TaskHistory');
|
||||||
|
}
|
||||||
|
}
|
22
app/Models/TaskContact.php
Normal file
22
app/Models/TaskContact.php
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class TaskContact extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that are mass assignable.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $fillable = [];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
29
app/Models/TaskHistory.php
Normal file
29
app/Models/TaskHistory.php
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class TaskHistory extends Model
|
||||||
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
|
protected $table = 'task_history';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that are mass assignable.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $fillable = [];
|
||||||
|
|
||||||
|
public function notifications() {
|
||||||
|
return $this->hasMany('App\Models\Notification');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function task() {
|
||||||
|
return $this->belongsTo('App\Models\Task');
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -25,7 +25,7 @@ $app = new Laravel\Lumen\Application(
|
||||||
|
|
||||||
$app->withFacades();
|
$app->withFacades();
|
||||||
|
|
||||||
// $app->withEloquent();
|
$app->withEloquent();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
|
@ -71,7 +71,7 @@ function setDbTimezone() {
|
||||||
|
|
||||||
$app->configure('app');
|
$app->configure('app');
|
||||||
$app->configure('database');
|
$app->configure('database');
|
||||||
|
$app->configure('queue');
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
@ -4,6 +4,18 @@
|
||||||
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
|
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
|
||||||
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
|
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
|
||||||
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
|
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
|
||||||
|
DROP TABLE IF EXISTS `contact_task`;
|
||||||
|
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||||
|
/*!40101 SET character_set_client = utf8 */;
|
||||||
|
CREATE TABLE `contact_task` (
|
||||||
|
`task_id` int unsigned NOT NULL,
|
||||||
|
`contact_id` int unsigned NOT NULL,
|
||||||
|
PRIMARY KEY (`task_id`,`contact_id`),
|
||||||
|
KEY `contact_id` (`contact_id`),
|
||||||
|
CONSTRAINT `contact_task_ibfk_1` FOREIGN KEY (`task_id`) REFERENCES `tasks` (`id`) ON DELETE CASCADE,
|
||||||
|
CONSTRAINT `contact_task_ibfk_2` FOREIGN KEY (`contact_id`) REFERENCES `contacts` (`id`) ON DELETE CASCADE
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
|
||||||
|
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||||
DROP TABLE IF EXISTS `contacts`;
|
DROP TABLE IF EXISTS `contacts`;
|
||||||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||||
/*!40101 SET character_set_client = utf8 */;
|
/*!40101 SET character_set_client = utf8 */;
|
||||||
|
@ -32,7 +44,7 @@ DROP TABLE IF EXISTS `migrations`;
|
||||||
/*!40101 SET character_set_client = utf8 */;
|
/*!40101 SET character_set_client = utf8 */;
|
||||||
CREATE TABLE `migrations` (
|
CREATE TABLE `migrations` (
|
||||||
`id` int unsigned NOT NULL AUTO_INCREMENT,
|
`id` int unsigned NOT NULL AUTO_INCREMENT,
|
||||||
`migration` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
|
`migration` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
|
||||||
`batch` int NOT NULL,
|
`batch` int NOT NULL,
|
||||||
PRIMARY KEY (`id`)
|
PRIMARY KEY (`id`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||||
|
@ -41,12 +53,32 @@ DROP TABLE IF EXISTS `notifications`;
|
||||||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||||
/*!40101 SET character_set_client = utf8 */;
|
/*!40101 SET character_set_client = utf8 */;
|
||||||
CREATE TABLE `notifications` (
|
CREATE TABLE `notifications` (
|
||||||
|
`id` int unsigned NOT NULL AUTO_INCREMENT,
|
||||||
|
`contact_id` int unsigned NOT NULL DEFAULT '0',
|
||||||
|
`task_history_id` int unsigned NOT NULL DEFAULT '0',
|
||||||
|
`status` enum('pending','sent','error') CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'pending',
|
||||||
|
`created_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:01',
|
||||||
|
`updated_at` datetime DEFAULT NULL,
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
KEY `contact_id_frgn` (`contact_id`),
|
||||||
|
KEY `task_history_id_frgn` (`task_history_id`),
|
||||||
|
CONSTRAINT `contact_id_frgn` FOREIGN KEY (`contact_id`) REFERENCES `contacts` (`id`) ON DELETE CASCADE,
|
||||||
|
CONSTRAINT `task_history_id_frgn` FOREIGN KEY (`task_history_id`) REFERENCES `task_history` (`id`) ON DELETE CASCADE
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8_unicode_ci;
|
||||||
|
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||||
|
DROP TABLE IF EXISTS `task_history`;
|
||||||
|
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||||
|
/*!40101 SET character_set_client = utf8 */;
|
||||||
|
CREATE TABLE `task_history` (
|
||||||
|
`id` int unsigned NOT NULL AUTO_INCREMENT,
|
||||||
|
`status` int unsigned NOT NULL,
|
||||||
|
`output` text CHARACTER SET utf8 COLLATE utf8_general_ci,
|
||||||
`task_id` int unsigned NOT NULL,
|
`task_id` int unsigned NOT NULL,
|
||||||
`contact_id` int unsigned NOT NULL,
|
`created_at` datetime NOT NULL,
|
||||||
PRIMARY KEY (`task_id`,`contact_id`),
|
`updated_at` datetime NOT NULL DEFAULT '1970-01-01 00:00:01',
|
||||||
KEY `contact_id` (`contact_id`),
|
PRIMARY KEY (`id`),
|
||||||
CONSTRAINT `notifications_ibfk_1` FOREIGN KEY (`task_id`) REFERENCES `tasks` (`id`) ON DELETE CASCADE,
|
KEY `task_id` (`task_id`),
|
||||||
CONSTRAINT `notifications_ibfk_2` FOREIGN KEY (`contact_id`) REFERENCES `contacts` (`id`) ON DELETE CASCADE
|
CONSTRAINT `task_history_ibfk_1` FOREIGN KEY (`task_id`) REFERENCES `tasks` (`id`) ON DELETE CASCADE
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
|
||||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||||
DROP TABLE IF EXISTS `tasks`;
|
DROP TABLE IF EXISTS `tasks`;
|
||||||
|
@ -57,11 +89,14 @@ CREATE TABLE `tasks` (
|
||||||
`host` varchar(255) NOT NULL,
|
`host` varchar(255) NOT NULL,
|
||||||
`type` enum('ping','http') NOT NULL,
|
`type` enum('ping','http') NOT NULL,
|
||||||
`params` varchar(255) NOT NULL,
|
`params` varchar(255) NOT NULL,
|
||||||
`creation_date` datetime NOT NULL,
|
|
||||||
`frequency` int unsigned NOT NULL,
|
`frequency` int unsigned NOT NULL,
|
||||||
`last_execution` datetime DEFAULT NULL,
|
`attempts` int unsigned NOT NULL DEFAULT '0',
|
||||||
`active` int NOT NULL DEFAULT '0',
|
`active` int NOT NULL DEFAULT '0',
|
||||||
|
`status` int unsigned DEFAULT NULL,
|
||||||
`group_id` int unsigned DEFAULT NULL,
|
`group_id` int unsigned DEFAULT NULL,
|
||||||
|
`created_at` datetime NOT NULL,
|
||||||
|
`updated_at` datetime DEFAULT NULL,
|
||||||
|
`executed_at` datetime DEFAULT NULL,
|
||||||
PRIMARY KEY (`id`),
|
PRIMARY KEY (`id`),
|
||||||
UNIQUE KEY `host` (`host`,`type`),
|
UNIQUE KEY `host` (`host`,`type`),
|
||||||
KEY `group_id_frgn` (`group_id`),
|
KEY `group_id_frgn` (`group_id`),
|
||||||
|
@ -79,20 +114,6 @@ CREATE TABLE `tasks_archives` (
|
||||||
PRIMARY KEY (`id`)
|
PRIMARY KEY (`id`)
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8_unicode_ci;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8_unicode_ci;
|
||||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||||
DROP TABLE IF EXISTS `tasks_history`;
|
|
||||||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
|
||||||
/*!40101 SET character_set_client = utf8 */;
|
|
||||||
CREATE TABLE `tasks_history` (
|
|
||||||
`id` int unsigned NOT NULL AUTO_INCREMENT,
|
|
||||||
`status` int unsigned NOT NULL,
|
|
||||||
`datetime` datetime NOT NULL,
|
|
||||||
`output` text CHARACTER SET utf8 COLLATE utf8_general_ci,
|
|
||||||
`task_id` int unsigned NOT NULL,
|
|
||||||
PRIMARY KEY (`id`),
|
|
||||||
KEY `task_id` (`task_id`),
|
|
||||||
CONSTRAINT `tasks_history_ibfk_1` FOREIGN KEY (`task_id`) REFERENCES `tasks` (`id`) ON DELETE CASCADE
|
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
|
|
||||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
|
||||||
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
|
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
|
||||||
|
|
||||||
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
|
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
|
||||||
|
@ -100,3 +121,5 @@ CREATE TABLE `tasks_history` (
|
||||||
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
|
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
|
||||||
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
|
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
|
||||||
|
|
||||||
|
INSERT INTO `migrations` VALUES (1,'2021_12_21_101954_create_jobs_table',1);
|
||||||
|
INSERT INTO `migrations` VALUES (2,'2021_12_21_102355_create_failed_jobs_table',2);
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -42,9 +42,9 @@
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<span
|
<span
|
||||||
v-if="task.last_execution"
|
v-if="task.executed_at"
|
||||||
>
|
>
|
||||||
{{ moment(task.last_execution).fromNow() }}
|
{{ moment(task.executed_at).fromNow() }}
|
||||||
<img src="/img/info.svg" alt="Infos" width="16" :title="'Result: '+task.output" />
|
<img src="/img/info.svg" alt="Infos" width="16" :title="'Result: '+task.output" />
|
||||||
</span>
|
</span>
|
||||||
<span
|
<span
|
||||||
|
|
104
sql/create.sql
104
sql/create.sql
|
@ -1,104 +0,0 @@
|
||||||
#
|
|
||||||
# SQL Export
|
|
||||||
# Created by Querious (300063)
|
|
||||||
# Created: 19 December 2021 at 10:19:50 CET
|
|
||||||
# Encoding: Unicode (UTF-8)
|
|
||||||
#
|
|
||||||
|
|
||||||
|
|
||||||
SET @ORIG_FOREIGN_KEY_CHECKS = @@FOREIGN_KEY_CHECKS;
|
|
||||||
SET FOREIGN_KEY_CHECKS = 0;
|
|
||||||
|
|
||||||
SET @ORIG_UNIQUE_CHECKS = @@UNIQUE_CHECKS;
|
|
||||||
SET UNIQUE_CHECKS = 0;
|
|
||||||
|
|
||||||
SET @ORIG_TIME_ZONE = @@TIME_ZONE;
|
|
||||||
SET TIME_ZONE = '+00:00';
|
|
||||||
|
|
||||||
SET @ORIG_SQL_MODE = @@SQL_MODE;
|
|
||||||
SET SQL_MODE = 'NO_AUTO_VALUE_ON_ZERO';
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
DROP TABLE IF EXISTS `tasks_history`;
|
|
||||||
DROP TABLE IF EXISTS `notifications`;
|
|
||||||
DROP TABLE IF EXISTS `tasks`;
|
|
||||||
DROP TABLE IF EXISTS `groups`;
|
|
||||||
DROP TABLE IF EXISTS `contacts`;
|
|
||||||
|
|
||||||
|
|
||||||
CREATE TABLE `contacts` (
|
|
||||||
`id` int unsigned NOT NULL AUTO_INCREMENT,
|
|
||||||
`surname` varchar(200) NOT NULL,
|
|
||||||
`firstname` varchar(200) NOT NULL,
|
|
||||||
`email` varchar(250) NOT NULL,
|
|
||||||
`phone` varchar(20) NOT NULL,
|
|
||||||
`creation_date` datetime NOT NULL,
|
|
||||||
`active` int NOT NULL DEFAULT '1',
|
|
||||||
PRIMARY KEY (`id`)
|
|
||||||
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb3;
|
|
||||||
|
|
||||||
|
|
||||||
CREATE TABLE `groups` (
|
|
||||||
`id` int unsigned NOT NULL AUTO_INCREMENT,
|
|
||||||
`name` varchar(128) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
|
|
||||||
PRIMARY KEY (`id`)
|
|
||||||
) ENGINE=InnoDB AUTO_INCREMENT=244 DEFAULT CHARSET=utf8mb3 COLLATE=utf8_unicode_ci;
|
|
||||||
|
|
||||||
|
|
||||||
CREATE TABLE `tasks` (
|
|
||||||
`id` int unsigned NOT NULL AUTO_INCREMENT,
|
|
||||||
`host` varchar(255) NOT NULL,
|
|
||||||
`type` enum('ping','http') NOT NULL,
|
|
||||||
`params` varchar(255) NOT NULL,
|
|
||||||
`creation_date` datetime NOT NULL,
|
|
||||||
`frequency` int unsigned NOT NULL,
|
|
||||||
`last_execution` datetime DEFAULT NULL,
|
|
||||||
`active` int NOT NULL DEFAULT '0',
|
|
||||||
`group_id` int unsigned DEFAULT NULL,
|
|
||||||
PRIMARY KEY (`id`),
|
|
||||||
UNIQUE KEY `host` (`host`,`type`),
|
|
||||||
KEY `group_id_frgn` (`group_id`),
|
|
||||||
CONSTRAINT `group_id_frgn` FOREIGN KEY (`group_id`) REFERENCES `groups` (`id`) ON DELETE CASCADE
|
|
||||||
) ENGINE=InnoDB AUTO_INCREMENT=147 DEFAULT CHARSET=utf8mb3;
|
|
||||||
|
|
||||||
|
|
||||||
CREATE TABLE `notifications` (
|
|
||||||
`task_id` int unsigned NOT NULL,
|
|
||||||
`contact_id` int unsigned NOT NULL,
|
|
||||||
PRIMARY KEY (`task_id`,`contact_id`),
|
|
||||||
KEY `contact_id` (`contact_id`),
|
|
||||||
CONSTRAINT `notifications_ibfk_1` FOREIGN KEY (`task_id`) REFERENCES `tasks` (`id`) ON DELETE CASCADE,
|
|
||||||
CONSTRAINT `notifications_ibfk_2` FOREIGN KEY (`contact_id`) REFERENCES `contacts` (`id`) ON DELETE CASCADE
|
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;
|
|
||||||
|
|
||||||
|
|
||||||
CREATE TABLE `tasks_history` (
|
|
||||||
`id` int unsigned NOT NULL AUTO_INCREMENT,
|
|
||||||
`status` int unsigned NOT NULL,
|
|
||||||
`datetime` datetime NOT NULL,
|
|
||||||
`output` text CHARACTER SET utf8 COLLATE utf8_general_ci,
|
|
||||||
`task_id` int unsigned NOT NULL,
|
|
||||||
PRIMARY KEY (`id`),
|
|
||||||
KEY `task_id` (`task_id`),
|
|
||||||
CONSTRAINT `tasks_history_ibfk_1` FOREIGN KEY (`task_id`) REFERENCES `tasks` (`id`) ON DELETE CASCADE
|
|
||||||
) ENGINE=InnoDB AUTO_INCREMENT=88 DEFAULT CHARSET=utf8mb3;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
SET FOREIGN_KEY_CHECKS = @ORIG_FOREIGN_KEY_CHECKS;
|
|
||||||
|
|
||||||
SET UNIQUE_CHECKS = @ORIG_UNIQUE_CHECKS;
|
|
||||||
|
|
||||||
SET @ORIG_TIME_ZONE = @@TIME_ZONE;
|
|
||||||
SET TIME_ZONE = @ORIG_TIME_ZONE;
|
|
||||||
|
|
||||||
SET SQL_MODE = @ORIG_SQL_MODE;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Export Finished: 19 December 2021 at 10:19:50 CET
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue