|
|
@@ -17,6 +17,7 @@ use Illuminate\Support\Facades\Validator;
|
|
17
|
17
|
use Illuminate\Support\Facades\Session;
|
|
18
|
18
|
use Illuminate\Support\Facades\Storage;
|
|
19
|
19
|
use Illuminate\Support\Facades\Auth;
|
|
|
20
|
+use Illuminate\Support\Facades\DB;
|
|
20
|
21
|
use Illuminate\Support\Str;
|
|
21
|
22
|
use App\Services\Attivita\AttivitaService;
|
|
22
|
23
|
|
|
|
@@ -284,20 +285,49 @@ class EndpointController extends Controller
|
|
284
|
285
|
|
|
285
|
286
|
$stampanti = $endpoint->stampanti;
|
|
286
|
287
|
$stampanti_ids = $endpoint->stampanti->pluck('id')->toArray();
|
|
287
|
|
- $query = PrintJob::whereIn('stampante_id', $stampanti_ids)
|
|
288
|
|
- ->where('stato', PrintJob::STATO_IN_CODA);
|
|
289
|
|
-
|
|
290
|
|
- $lavori = $query->get();
|
|
291
|
|
- if($query->count() > 0){
|
|
292
|
|
- PrintJob::whereIn('stampante_id', $stampanti_ids)
|
|
293
|
|
- ->where('stato', PrintJob::STATO_IN_CODA)
|
|
294
|
|
- ->update([
|
|
295
|
|
- 'stato' => PrintJob::STATO_PRESO,
|
|
296
|
|
- ]);
|
|
297
|
|
- $has_lavoro = true;
|
|
298
|
|
- }else{
|
|
299
|
|
- $has_lavoro = false;
|
|
|
288
|
+ $presoTimeoutSeconds = (int) env('PRINTJOB_PRESO_TIMEOUT_SECONDS', 60);
|
|
|
289
|
+ $strictSingleFetchEnv = env('PRINTJOB_STRICT_SINGLE_FETCH', true);
|
|
|
290
|
+ $strictSingleFetch = filter_var($strictSingleFetchEnv, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
|
|
|
291
|
+ if ($strictSingleFetch === null) {
|
|
|
292
|
+ $strictSingleFetch = true;
|
|
300
|
293
|
}
|
|
|
294
|
+ $batchSize = $strictSingleFetch
|
|
|
295
|
+ ? 1
|
|
|
296
|
+ : max(1, (int) env('PRINTJOB_FETCH_BATCH_SIZE', 2));
|
|
|
297
|
+ $lavori = collect();
|
|
|
298
|
+
|
|
|
299
|
+ DB::transaction(function () use ($stampanti_ids, $presoTimeoutSeconds, $batchSize, &$lavori, &$has_lavoro) {
|
|
|
300
|
+ // Recupera automaticamente i job "preso" vecchi (agent/pc caduto o esito non inviato).
|
|
|
301
|
+ PrintJob::query()
|
|
|
302
|
+ ->whereIn('stampante_id', $stampanti_ids)
|
|
|
303
|
+ ->where('stato', PrintJob::STATO_PRESO)
|
|
|
304
|
+ ->where('updated_at', '<', now()->subSeconds($presoTimeoutSeconds))
|
|
|
305
|
+ ->update([
|
|
|
306
|
+ 'stato' => PrintJob::STATO_IN_CODA,
|
|
|
307
|
+ 'last_error' => 'Ripristinato automaticamente da stato PRESO scaduto',
|
|
|
308
|
+ ]);
|
|
|
309
|
+
|
|
|
310
|
+ // Prelievo FIFO rigoroso: blocca e prende i job più vecchi in ordine.
|
|
|
311
|
+ $jobsDaConsegnare = PrintJob::query()
|
|
|
312
|
+ ->whereIn('stampante_id', $stampanti_ids)
|
|
|
313
|
+ ->where('stato', PrintJob::STATO_IN_CODA)
|
|
|
314
|
+ ->orderBy('id')
|
|
|
315
|
+ ->lockForUpdate()
|
|
|
316
|
+ ->limit($batchSize)
|
|
|
317
|
+ ->get();
|
|
|
318
|
+
|
|
|
319
|
+ if ($jobsDaConsegnare->isEmpty()) {
|
|
|
320
|
+ return;
|
|
|
321
|
+ }
|
|
|
322
|
+
|
|
|
323
|
+ foreach ($jobsDaConsegnare as $job) {
|
|
|
324
|
+ $job->stato = PrintJob::STATO_PRESO;
|
|
|
325
|
+ $job->save();
|
|
|
326
|
+ $lavori->push($job);
|
|
|
327
|
+ }
|
|
|
328
|
+
|
|
|
329
|
+ $has_lavoro = true;
|
|
|
330
|
+ }, 3);
|
|
301
|
331
|
|
|
302
|
332
|
return response()->json([
|
|
303
|
333
|
'message' => $has_lavoro ? 'Lavoro trovato con successo' : 'Nessun lavoro trovato',
|