1
0
mirror of https://github.com/devcode-it/openstamanager.git synced 2025-02-16 11:30:55 +01:00

Introduzione API per le task cron

This commit is contained in:
Dasc3er 2020-08-28 13:51:15 +02:00
parent 7bc18666f6
commit fce39cc05b
4 changed files with 86 additions and 0 deletions

View File

@ -86,6 +86,11 @@ while (true) {
return;
}
// Rimozione dei log più vecchi
$database->query('DELETE FROM zz_tasks_logs WHERE DATE_ADD(created_at, INTERVAL :interval DAY) <= NOW()', [
':interval' => 7,
]);
// Risveglio programmato tramite slot
$timestamp = $slot_minimo->getTimestamp();
time_sleep_until($timestamp);

72
src/API/Common/Task.php Normal file
View File

@ -0,0 +1,72 @@
<?php
namespace API\Common;
use API\Interfaces\CreateInterface;
use API\Interfaces\RetrieveInterface;
use API\Resource;
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)
{
// Rimozione della registrazione del cron attuale
$ultima_esecuzione = Cache::get('Ultima esecuzione del cron');
$ultima_esecuzione->set(null);
// Segnalazione della chiusura al cron attuale
$cron_id = Cache::get('ID del cron');
$cron_id->set(null);
// Rimozione dell'eventuale blocco sul cron
$disattiva = Cache::get('Disabilita cron');
$disattiva->set(null);
// Chiamata al cron per l'avvio
$this->request();
}
/**
* Richiesta HTTP fire-and-forget.
*
* @source https://cwhite.me/blog/fire-and-forget-http-requests-in-php
*/
protected function request()
{
$endpoint = BASEURL.'/cron.php';
$postData = json_encode([]);
$endpointParts = parse_url($endpoint);
$endpointParts['path'] = $endpointParts['path'] ?: '/';
$endpointParts['port'] = $endpointParts['port'] ?: $endpointParts['scheme'] === 'https' ? 443 : 80;
$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);
}
}

View File

@ -15,6 +15,10 @@ class Log extends Model
'context' => 'array',
];
protected $hidden = [
'task',
];
public function task()
{
return $this->belongsTo(Task::class, 'id_task');

View File

@ -76,3 +76,8 @@ INSERT INTO `zz_views` (`id`, `id_module`, `name`, `query`, `order`, `search`, `
(NULL, (SELECT `id` FROM `zz_modules` WHERE `name` = 'Giacenze sedi'), 'Prezzo di vendita', 'prezzo_vendita', '6', '1', '0', '1', NULL, NULL, '1', '1', '1'),
(NULL, (SELECT `id` FROM `zz_modules` WHERE `name` = 'Giacenze sedi'), 'Prezzo vendita ivato', 'IF( co_iva.percentuale IS NOT NULL, (mg_articoli.prezzo_vendita + mg_articoli.prezzo_vendita * co_iva.percentuale / 100), mg_articoli.prezzo_vendita + mg_articoli.prezzo_vendita*(SELECT co_iva.percentuale FROM co_iva INNER JOIN zz_settings ON co_iva.id=zz_settings.valore AND nome=\'Iva predefinita\')/100 )', '8', '1', '0', '1', '', '', '0', '0', '1'),
(NULL, (SELECT `id` FROM `zz_modules` WHERE `name` = 'Giacenze sedi'), 'Barcode', 'mg_articoli.barcode', '2', '1', '0', '0', '', '', '1', '0', '1');
-- Aggiunta risorse API dedicate alle task in cron
INSERT INTO `zz_api_resources` (`id`, `version`, `type`, `resource`, `class`, `enabled`) VALUES
(NULL, 'v1', 'retrieve', 'cron-logs', 'API\\Common\\Task', '1'),
(NULL, 'v1', 'create', 'cron-restart', 'API\\Common\\Task', '1');