Giter Site home page Giter Site logo

laravel-notification's Introduction

Laravel notification, Event Scheduling and Queue functionality

Added functionality to dispatch Job and send File to aws s3 bucket

Inside Controller

 try {
            $request->validate([
                "processfile" => ['required','file',"mimes:pdf","max:949087"]
            ]);
    
            $file = $request->file("processfile");
            $filepath = $file->getRealPath();
            $exte = $file->getClientOriginalExtension();
            $user_id = $request->user()->id;
            $filename = str_replace(" ","-",$file->getClientOriginalName());
            $filename = Str::replaceLast(".".$exte,"",$filename);
            $filename = $filename."-".date("Y-m-d-h-s").".".$exte;
            $fileContent = file_get_contents($filepath);
            ProcessDocumentFile::dispatch($fileContent,$filename,$user_id);
            return back()->with("success","File is updloaded succesfull");

        } catch (\Throwable $th) {
           
            return back()->with("UploadError","File upload failed , try again!");
        }

Inside Job Class contructor , since to allow serialization to be done successfull, try to encode the Uploaded File format before transimiting it from controller to Job Class

 public function __construct(
        public $fileContent,
        public $filename,
        public $user_id
    ) {
    }

Then in Handle Method of the Job Class

     public function handle(): void
    {
        
        $fullpath = "laravel-notification/".$this->filename;
        Storage::disk("s3")->put($fullpath, $this->fileContent);
        Processfile::create([
            "filename"  => $this->filename,
            "path" => $fullpath,
            "user_id" => $this->user_id
        ]);
    }

sometimes you may wish to run Jobs in Chain , where after completion of one Job , the 2nd Job will start immediately

  • Let say you want to send notification to user who post file to your application
  • This is just miner example , we don't go deep to send it via mail
  • inside controller
 Bus::chain([
                new ProcessDocumentFile($fileContent,$filename,$user_id),
                new CreateNewUser($request->user())
            ])
              ->onQueue("Fileprocessing") //instead of default Queue(default), let use another queue name
            ->dispatch();

I was successfull be able to Upload JWT Handbook see here and be able to download

Bus::chain([
        new ProcessDocumentFile($fileContent,$filename,$user_id),
        new CreateNewUser($request->user()),
        new NotificationDocumentConfirmation($request->user())
    ])
    ->onQueue("low")
    ->dispatch(); 

From terminal this is how Bus Jobs are processed in chain with Queue named == low

it's done , Thanks for reading

Regard mrwilbroad

laravel-notification's People

Stargazers

Wilbroad Mark avatar

Watchers

Wilbroad Mark avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.