Code Monkey home page Code Monkey logo

laravel-job's Introduction

laravel-job

Current version Monthly Downloads Total Downloads Build Status

Laravel job tools:

  • dispatch job from command line with parameters to queue or run synchronously;
  • Job base class with boilerplate.

Installation

$ composer require mxl/laravel-job

Laravel 5.5+ will use the auto-discovery feature to add MichaelLedin\LaravelJob\LaravelJobServiceProvider::class to providers.

This package is not compatible with older Laravel versions.

Usage

Dispatching job from command line to the queue

Make sure that you either use sync connection (see default property in config/queue.php) or run queue worker:

$ php artisan queue:work

Then dispatch command with:

$ php artisan job:dispatch YourJob

if YourJob class is located under \App\Jobs or specify full class name with namespace:

$ php artisan job:dispatch '\Path\To\YourJob'

Running jobs immediately

If you want to run job right now without posting it to queue use job:dispatchNow command:

$ php artisan job:dispatchNow YourJob

Dispatching jobs with parameters

$ php artisan job:dispatch YourJob John 1990-01-01

John and 1990-01-01 values will be passed to YourJob constructor as $name and $birthDate arguments:

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

    __constructor($name, $birthDate)
   {
       // ...
   }

   public function handle()
   {
       // ...
   }
}

Using job with parameters from command line and PHP code

Often a job is already in use somewhere from PHP code and if it has constructor arguments that must have specific type, then it can be required to parse command line parameters.

For this purpose implement FromParameters interface:

use MichaelLedin\LaravelJob\FromParameters;
use Carbon\Carbon;

class YourJob implements ShouldQueue, FromParameters
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
 
    __constructor(string $name, Carbon $birthDate)
    {
        // ...
    }
 
    public function handle()
    {
        // ...
    }

    public static function fromParameters(...$parameters)
    {
        return new self($parameters[0], Carbon::parse($parameters[1]));
    } 
}

Job boilerplate

Job classes always use the same interface ShouldQueue and Dispatchable, InteractsWithQueue, Queueable, SerializesModels traits.

To avoid such boilerplate your jobs can extend MichaelLedin\LaravelJob\Job class:

use MichaelLedin\LaravelJob\Job;
use Carbon\Carbon;

class YourJob extends Job
{
    // ...
}

It also includes default FromParameters interface implementation that is equivalent to calling constructor with arguments provided by command line parameters without any parsing.
To add parsing override fromParameters method.

Maintainers

Other useful Laravel packages from the author

License

See the LICENSE file for details.

laravel-job's People

Contributors

mxl avatar repat avatar

Watchers

 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.