vendredi 25 septembre 2020

Merging File using all server resouces in laravel

I have created job which is responsible for merging files. On localhost it is working fine. On server for almost hundred pdfs it work fine but as the number of pdfs increases it starts using all servers resources. And in result Server Got hanged for hours .I believe something is wrong with my algorithm But can't find out. It is for almost 1500 pdfs. Here is the code this job is receiving pdf one by one

<?php

namespace App\Jobs\Consignment;

use App\Helpers\Notification_Helper;
use App\Models\Admin\Contact;
use App\Models\Admin\Manifest;
use App\Models\Admin\Staff;
use CodeItNow\BarcodeBundle\Utils\BarcodeGenerator;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Barryvdh\DomPDF\Facade as PDF;
use DirectoryIterator;
use PDFMerger;
use File;
use Illuminate\Support\Facades\Storage;

class BulkPdfPrintLabel implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    protected $consignments;
    protected $last;
    protected $manifest_id;
    protected $user_id;
    protected $user_type;
    protected $filepath;
    public $timeout = 3600;

    public function __construct($consignments, $last, $manifest_id, $user_id, $user_type,$filepath)
    {
        $this->consignments = $consignments;
        $this->last = $last;
        $this->manifest_id = $manifest_id;
        $this->user_id = $user_id;
        $this->user_type = $user_type;
        $this->filepath =$filepath;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {

        $directory_path = public_path() . '/bulk-pdf-consignment/manifest_'.$this->manifest_id;
        if (!File::exists($directory_path)) {
            File::makeDirectory($directory_path, $mode = 0777, true, true);
        }

        $allDataConsignments = array();
    
        foreach ($this->consignments  as $key => $consignment) {
            // "printnum" => "1"

            // $consignment = $allConsignment::with('customers', 'pickup_addresses');

            $barcode = new BarcodeGenerator();
            $barcode->setText($consignment->barcode);
            $barcode->setType(BarcodeGenerator::Code128);
            $barcode->setScale(2);
            $barcode->setThickness(30);
            $barcode->setFontSize(10);
            $code = $barcode->generate();

            // This  $data array will be passed to our PDF blade
            $data = [
                'consignment' => $consignment,
                'barcode' => $code
            ];

           
            array_push($allDataConsignments, $data);
        }

        $customPaper = array(0, 0, 430.00, 283.80);
        $title = 'Consignment Bulk PDF';
        $pdf = PDF::loadView(
            'web.admin.partials.transports.consignments.consignment_bulk_pdf',
            compact('allDataConsignments', 'title')
        )->setPaper($customPaper, 'landscape');
       
         $pdf->save($this->filepath); 
    }
}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire