Showing posts with label Laravel 10. Show all posts
Showing posts with label Laravel 10. Show all posts

Sunday 11 June 2023

Overview


Queue means whatever comes first goes out first means first in first out is called the queue. It maintains the sequence.

When the task is time consuming or long running task like sending emails to all users, importing CSV to the database, multiple images upload in the server, etc. So we are using the queue and jobs.

Laravel also connects queues with several drivers like database, Redis, Amazon, Beanstalkd, etc. The Drives are used to store the list of tasks.

Topics

  1. Configure the queue and jobs
  2. Jobs
  3. Dispatching the job

Configure the queue

  • After setting up the laravel project first in the config folder queue.php file there. In this file connection array queue is configure.


  • In this connection array, we using the queue for database and table=> jobs jobs table is created and retry_after=>90 if any queue is failed so after 90 seconds it retry and the queue => ‘default’ is the name of the queue

  • After in the terminal fire the command php artisan queue:table so the one migration file is created like 2022_05_17_051432_create_jobs_tbl_table.php

  • After run the php artisan migrate command to migrate the jobs table.
  • Here all the queue configuration is done.

Jobs

  • After configuring the queue we create the jobs using the php artisan make:job JobName.

  • The jobs are created under the app/Jobs folder and the job must implement the shouldqueue interface and also use the trait file Dispatchable, InteractsWithQueue, Queueable, SerializesModels in the queue class.

  • In this created queue there are __construct and handle functions. Handle function we are writing the over logic the same as the controller function and here if we want to pass the data so we use the pass the argument to the constructor.




Dispatching the Jobs

        There are two method to dispatch the jobs

    1. using the dispatch global helper like dispatch(new JobName);
    2. using the dispatch static method like JobName::dispatch();

  • Job is run in the background so we can not see and browser response is very fast.

  • If we are dispatching the job by giving a particular time means giving the some delay into over jobs so JobName::dispatch()->deley(now()->addSeconds(20)); so the job will be executed after the 20 seconds and if not passing the any second by default job will execute the 1 second.

  • If we are passing some data into the job so we in the JobName::dispatch($some_data) and the job class pass the variable into the __constuct method if not pass then show the error.

  • And in the terminal fire, the command php artisan queue:work otherwise the jobs is not run and not getting the output.


  • If any jobs will be fail so in the failed_jobs table exception column show why the jobs are failing in the below image.

  • In the terminal display the failed jobs like the below:-


  • If we want to remove all failed jobs in failed_jobs table so we fire the php artisan queue:flush so it removes all failed jobs otherwise when fire the php artisan queue:work command it will run the again failed job.

  • Laravel by default 60 second a maximum execution time provide so if the task time takes more than in the jobs class we add the property like public $timeout = 1200; so the jobs will not be terminated.

  • After all, jobs run successfully we need to inform the user or send a message like your csv files data insert successfully.
NOTE: when we change the jobs code so every time we need to clear the cache otherwise it run from the cache and display the error.

php artisan config:cache
php artisan cache:clear
or php artisan o:c (optimize the cache)

Conclusion

  • Laravel queues are very useful to run the application smoothly.

  • In our application the task is heavy and it takes time until the user can not wait for the task to be completed. Here it allows the user to perform any other task and the process is done without the user interrupt and background.

  • When the task is very heavy (ex. upload 1 million records into the database) create big jobs into smaller and create the job batching.

  • You can download the one demo code for creating send email to users when add the post click here to download. Hope this blog is helpful for a basic understanding of Laravel queues and jobs.

Laravel 10: New Features

Overview


Laravel is one of the most famous and extensively employed for developing web applications, which is open-source in nature. Its popularity is because of its easy use, clear and simple documentation, and huge and helping community of developers. Laravel version 10 offers a range of new features and functionality that simplifies web application development.

What are the new features in Laravel 10?


A few new features added in Laravel version 10 are:

  1. Improved performance: Laravel 10 offers improved speed and efficiency compared to earlier versions, which enables your web applications to better handle increased traffic and requests.
  2. Time Testing Helpers: Laravel 10 includes time-testing helpers that allow developers to easily test time-sensitive code by manipulating the time in a test environment.
  3. Improved Routing Speed: The routing system in Laravel 10 has significantly improved routing performance, especially for applications with an extensive number of routes.
  4. Blade Component Attribute Improvements: Laravel 10 includes improvements to Blade component attributes that allow developers to easily pass data between components.
  5. New Rate Limiting Middleware: Laravel 10 includes a new rate-limiting middleware feature that can be used to restrict the number of requests sent to an API endpoint in an application.
  6. Custom Casts Improvements: Laravel 10 includes improvements to custom casts that allow developers to define custom data types for model attributes.
  7. Model Validation Improvements: Laravel 10 includes improvements to model validation that allow developers to validate model data without the need for a separate request class.
  8. Improved Testing Performance: Laravel 10 includes improvements to the testing framework that can significantly improve the speed of unit tests.