2020-08-28 13:51:15 +02:00
|
|
|
<?php
|
2020-09-07 15:04:06 +02:00
|
|
|
/*
|
|
|
|
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
2021-01-20 15:08:51 +01:00
|
|
|
* Copyright (C) DevCode s.r.l.
|
2020-09-07 15:04:06 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-08-28 13:51:15 +02:00
|
|
|
|
|
|
|
namespace API\Common;
|
|
|
|
|
|
|
|
use API\Interfaces\CreateInterface;
|
|
|
|
use API\Interfaces\RetrieveInterface;
|
|
|
|
use API\Resource;
|
2020-08-28 14:10:43 +02:00
|
|
|
use Carbon\Carbon;
|
2020-08-28 13:51:15 +02:00
|
|
|
use Models\Cache;
|
|
|
|
use Tasks\Log;
|
|
|
|
|
|
|
|
class Task extends Resource implements RetrieveInterface, CreateInterface
|
|
|
|
{
|
|
|
|
public function retrieve($request)
|
|
|
|
{
|
|
|
|
$logs = Log::latest()
|
|
|
|
->take(1000)->get()
|
|
|
|
->groupBy('task.name');
|
|
|
|
|
|
|
|
return [
|
|
|
|
'results' => $logs->toArray(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function create($request)
|
|
|
|
{
|
2020-08-28 14:10:43 +02:00
|
|
|
$database = database();
|
|
|
|
|
2020-08-28 13:51:15 +02:00
|
|
|
// Rimozione della registrazione del cron attuale
|
2020-09-23 13:36:37 +02:00
|
|
|
$ultima_esecuzione = Cache::pool('Ultima esecuzione del cron');
|
2020-08-28 13:51:15 +02:00
|
|
|
$ultima_esecuzione->set(null);
|
|
|
|
|
|
|
|
// Segnalazione della chiusura al cron attuale
|
2020-09-23 13:36:37 +02:00
|
|
|
$cron_id = Cache::pool('ID del cron');
|
2020-08-28 13:51:15 +02:00
|
|
|
$cron_id->set(null);
|
|
|
|
|
|
|
|
// Rimozione dell'eventuale blocco sul cron
|
2020-09-23 13:36:37 +02:00
|
|
|
$disattiva = Cache::pool('Disabilita cron');
|
2020-08-28 13:51:15 +02:00
|
|
|
$disattiva->set(null);
|
|
|
|
|
2020-08-28 14:10:43 +02:00
|
|
|
// Salvataggio delle modifiche
|
|
|
|
$database->commitTransaction();
|
|
|
|
|
|
|
|
// Attesa della conclusione per il cron precedente
|
2020-09-23 13:36:37 +02:00
|
|
|
$in_esecuzione = Cache::pool('Cron in esecuzione');
|
2020-08-28 14:10:43 +02:00
|
|
|
while ($in_esecuzione->content) {
|
|
|
|
$timestamp = (new Carbon())->addMinutes(1)->getTimestamp();
|
|
|
|
time_sleep_until($timestamp);
|
|
|
|
|
|
|
|
$in_esecuzione->refresh();
|
|
|
|
}
|
|
|
|
|
2020-08-28 13:51:15 +02:00
|
|
|
// Chiamata al cron per l'avvio
|
|
|
|
$this->request();
|
2020-08-28 14:10:43 +02:00
|
|
|
|
|
|
|
// Riavvio transazione
|
|
|
|
$database->beginTransaction();
|
2020-08-28 13:51:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Richiesta HTTP fire-and-forget.
|
|
|
|
*
|
|
|
|
* @source https://cwhite.me/blog/fire-and-forget-http-requests-in-php
|
|
|
|
*/
|
|
|
|
protected function request()
|
|
|
|
{
|
2020-09-23 13:36:37 +02:00
|
|
|
$endpoint = base_url().'/cron.php';
|
2020-08-28 13:51:15 +02:00
|
|
|
$postData = json_encode([]);
|
|
|
|
|
|
|
|
$endpointParts = parse_url($endpoint);
|
|
|
|
$endpointParts['path'] = $endpointParts['path'] ?: '/';
|
2021-07-23 11:37:46 +02:00
|
|
|
$endpointParts['port'] = $endpointParts['port'] ?: ($endpointParts['scheme'] === 'https' ? 443 : 80);
|
2020-08-28 13:51:15 +02:00
|
|
|
|
|
|
|
$contentLength = strlen($postData);
|
|
|
|
|
|
|
|
$request = "POST {$endpointParts['path']} HTTP/1.1\r\n";
|
|
|
|
$request .= "Host: {$endpointParts['host']}\r\n";
|
|
|
|
$request .= "User-Agent: OpenSTAManager API v1\r\n";
|
|
|
|
$request .= "Authorization: Bearer api_key\r\n";
|
|
|
|
$request .= "Content-Length: {$contentLength}\r\n";
|
|
|
|
$request .= "Content-Type: application/json\r\n\r\n";
|
|
|
|
$request .= $postData;
|
|
|
|
|
|
|
|
$prefix = substr($endpoint, 0, 8) === 'https://' ? 'tls://' : '';
|
|
|
|
|
|
|
|
$socket = fsockopen($prefix.$endpointParts['host'], $endpointParts['port']);
|
|
|
|
fwrite($socket, $request);
|
|
|
|
fclose($socket);
|
|
|
|
}
|
|
|
|
}
|