Skip to content

Commit fce39cc

Browse files
committed
Introduzione API per le task cron
1 parent 7bc1866 commit fce39cc

4 files changed

Lines changed: 86 additions & 0 deletions

File tree

‎cron.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@
8686
return;
8787
}
8888

89+
// Rimozione dei log più vecchi
90+
$database->query('DELETE FROM zz_tasks_logs WHERE DATE_ADD(created_at, INTERVAL :interval DAY) <= NOW()', [
91+
':interval' => 7,
92+
]);
93+
8994
// Risveglio programmato tramite slot
9095
$timestamp = $slot_minimo->getTimestamp();
9196
time_sleep_until($timestamp);

‎src/API/Common/Task.php‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace API\Common;
4+
5+
use API\Interfaces\CreateInterface;
6+
use API\Interfaces\RetrieveInterface;
7+
use API\Resource;
8+
use Models\Cache;
9+
use Tasks\Log;
10+
11+
class Task extends Resource implements RetrieveInterface, CreateInterface
12+
{
13+
public function retrieve($request)
14+
{
15+
$logs = Log::latest()
16+
->take(1000)->get()
17+
->groupBy('task.name');
18+
19+
return [
20+
'results' => $logs->toArray(),
21+
];
22+
}
23+
24+
public function create($request)
25+
{
26+
// Rimozione della registrazione del cron attuale
27+
$ultima_esecuzione = Cache::get('Ultima esecuzione del cron');
28+
$ultima_esecuzione->set(null);
29+
30+
// Segnalazione della chiusura al cron attuale
31+
$cron_id = Cache::get('ID del cron');
32+
$cron_id->set(null);
33+
34+
// Rimozione dell'eventuale blocco sul cron
35+
$disattiva = Cache::get('Disabilita cron');
36+
$disattiva->set(null);
37+
38+
// Chiamata al cron per l'avvio
39+
$this->request();
40+
}
41+
42+
/**
43+
* Richiesta HTTP fire-and-forget.
44+
*
45+
* @source https://cwhite.me/blog/fire-and-forget-http-requests-in-php
46+
*/
47+
protected function request()
48+
{
49+
$endpoint = BASEURL.'/cron.php';
50+
$postData = json_encode([]);
51+
52+
$endpointParts = parse_url($endpoint);
53+
$endpointParts['path'] = $endpointParts['path'] ?: '/';
54+
$endpointParts['port'] = $endpointParts['port'] ?: $endpointParts['scheme'] === 'https' ? 443 : 80;
55+
56+
$contentLength = strlen($postData);
57+
58+
$request = "POST {$endpointParts['path']} HTTP/1.1\r\n";
59+
$request .= "Host: {$endpointParts['host']}\r\n";
60+
$request .= "User-Agent: OpenSTAManager API v1\r\n";
61+
$request .= "Authorization: Bearer api_key\r\n";
62+
$request .= "Content-Length: {$contentLength}\r\n";
63+
$request .= "Content-Type: application/json\r\n\r\n";
64+
$request .= $postData;
65+
66+
$prefix = substr($endpoint, 0, 8) === 'https://' ? 'tls://' : '';
67+
68+
$socket = fsockopen($prefix.$endpointParts['host'], $endpointParts['port']);
69+
fwrite($socket, $request);
70+
fclose($socket);
71+
}
72+
}

‎src/Tasks/Log.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ class Log extends Model
1515
'context' => 'array',
1616
];
1717

18+
protected $hidden = [
19+
'task',
20+
];
21+
1822
public function task()
1923
{
2024
return $this->belongsTo(Task::class, 'id_task');

‎update/2_4_18.sql‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,8 @@ INSERT INTO `zz_views` (`id`, `id_module`, `name`, `query`, `order`, `search`, `
7676
(NULL, (SELECT `id` FROM `zz_modules` WHERE `name` = 'Giacenze sedi'), 'Prezzo di vendita', 'prezzo_vendita', '6', '1', '0', '1', NULL, NULL, '1', '1', '1'),
7777
(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'),
7878
(NULL, (SELECT `id` FROM `zz_modules` WHERE `name` = 'Giacenze sedi'), 'Barcode', 'mg_articoli.barcode', '2', '1', '0', '0', '', '', '1', '0', '1');
79+
80+
-- Aggiunta risorse API dedicate alle task in cron
81+
INSERT INTO `zz_api_resources` (`id`, `version`, `type`, `resource`, `class`, `enabled`) VALUES
82+
(NULL, 'v1', 'retrieve', 'cron-logs', 'API\\Common\\Task', '1'),
83+
(NULL, 'v1', 'create', 'cron-restart', 'API\\Common\\Task', '1');

0 commit comments

Comments
 (0)