All Projects → vuongxuongminh → Laravel Async

vuongxuongminh / Laravel Async

Licence: mit
Package provide simple way to run code asynchronously for your Laravel application.

Projects that are alternatives of or similar to Laravel Async

Laravel Settings
Simple Settings package for a laravel application
Stars: ✭ 45 (-8.16%)
Mutual labels:  laravel, laravel-package
Doorman
Limit access to your Laravel applications by using invite codes
Stars: ✭ 913 (+1763.27%)
Mutual labels:  laravel, laravel-package
Blade Migrations Laravel
An intelligent alternative version of Laravel 5/6 Database Migrations - uses raw-sql syntax, transactions, auto-rollback, UP-DOWN-UP testing
Stars: ✭ 25 (-48.98%)
Mutual labels:  laravel, laravel-package
Eloquent Ldap
A Laravel 5.1 package that first tries to log the user against the internal database if that fails, it tries against the configured LDAP/AD server.
Stars: ✭ 19 (-61.22%)
Mutual labels:  laravel, laravel-package
Laravel Qrcode Ecommerce
This is a complete laravel project that handles qrcodes, payments, api/microservices, and ecommerce
Stars: ✭ 36 (-26.53%)
Mutual labels:  laravel, laravel-package
Laravel Aws Sns
Laravel package for the AWS SNS Events
Stars: ✭ 24 (-51.02%)
Mutual labels:  laravel, laravel-package
Chatify
A Laravel package that allows you to add a complete user messaging system into your new/existing Laravel application.
Stars: ✭ 885 (+1706.12%)
Mutual labels:  laravel, laravel-package
Laravel Blade Directives
A collection of nice Laravel Blade directives
Stars: ✭ 813 (+1559.18%)
Mutual labels:  laravel, laravel-package
Laravel Weather
🌤️ A wrapper around Open Weather Map API (Current weather)
Stars: ✭ 36 (-26.53%)
Mutual labels:  laravel, laravel-package
Cms
Statamic 3: The Core Composer Package
Stars: ✭ 965 (+1869.39%)
Mutual labels:  laravel, laravel-package
Simple Cache
An easy to use Caching trait for Laravel's Eloquent Models.
Stars: ✭ 19 (-61.22%)
Mutual labels:  laravel, laravel-package
Laravel Multilang
Package to integrate multi language (multi locale) functionality in Laravel 5.x.
Stars: ✭ 47 (-4.08%)
Mutual labels:  laravel, laravel-package
Laravel Log
Simple API to write logs for Laravel.
Stars: ✭ 19 (-61.22%)
Mutual labels:  laravel, laravel-package
Laravel Translatable
It's a Laravel database translations manager
Stars: ✭ 47 (-4.08%)
Mutual labels:  laravel, laravel-package
Package Skeleton
📦 My base for PHP packages.
Stars: ✭ 6 (-87.76%)
Mutual labels:  laravel, laravel-package
Laravel Mailguneu
Allow customising the Mailgun server URL to use EU servers.
Stars: ✭ 13 (-73.47%)
Mutual labels:  laravel, laravel-package
Twitter
Twitter API for Laravel 5.5+, 6.x, 7.x & 8.x
Stars: ✭ 755 (+1440.82%)
Mutual labels:  laravel, laravel-package
Laravel Notify
Flexible Flash notifications for Laravel
Stars: ✭ 787 (+1506.12%)
Mutual labels:  laravel, laravel-package
Laravel Database Logger
Log database query sql in Laravel Application, support Guard,Auth to multiple file record
Stars: ✭ 31 (-36.73%)
Mutual labels:  laravel, laravel-package
Laravel Compass
A REST client inside your Laravel app
Stars: ✭ 1,002 (+1944.9%)
Mutual labels:  laravel, laravel-package

Laravel Async


Latest version Build status Quantity score StyleCI Total download License

About it

A package provide an easy way to run code asynchronous and parallel base on Spatie Async wrapper for Laravel application.

Installation

Require Laravel Async using Composer:

composer require vxm/laravel-async

The package will automatically register itself.

You can publish the config-file (optional) with:

php artisan vendor:publish --provider="VXM\Async\AsyncServiceProvider" --tag="config"

This is the contents of the published config file:

return [
    /*
     * The PHP binary will be use in async processes.
     */
    'withBinary' => PHP_BINARY,

    /*
     * Maximum concurrency async processes.
     */
    'concurrency' => 20,

    /*
     * Async process timeout.
     */
    'timeout' => 15,

    /*
     * Sleep (micro-second) time when waiting async processes.
     */
    'sleepTime' => 50000,

    /*
     * Default output length of async processes.
     */
    'defaultOutputLength' => 1024 * 10,

    /*
     * An autoload script to boot composer autoload and Laravel application.
     * Default null meaning using an autoload of this package.
     */
    'autoload' => null,
];

Usage

Run async code

After install, now you can try run async code via Async facade:

use Async;

for ($i = 1; $i < 20; $i++) {
    Async::run(function () use ($i) {
        sleep(1);

        return $i;
    });
}

var_dump(implode(', ', Async::wait()));

// Output value may be like:
// string(65) "5, 2, 1, 14, 4, 6, 7, 8, 19, 16, 12, 18, 13, 3, 10, 9, 11, 17, 15"

An async job can be callable class, anonymous function or Laravel callback:

use Async;

// run with anonymous function:
Async::run(function() {
    // Do a thing
});

// run with [email protected]
Async::run('Your\AsyncJobs\[email protected]');

// call default `handle` method if method not set.
Async::run('Your\AsyncJobs\Class');

You can run multiple job one time and waiting until all done.

use Async;

Async::run('Your\AsyncJobs\[email protected]');
Async::run('Your\AsyncJobs\[email protected]');
Async::run('Your\AsyncJobs\[email protected]');
Async::run('Your\AsyncJobs\[email protected]');

// Another way:

Async::batchRun(
    'Your\AsyncJobs\[email protected]',
    'Your\AsyncJobs\[email protected]',
    'Your\AsyncJobs\[email protected]',
    'Your\AsyncJobs\[email protected]'
);

$results = Async::wait(); // result return from jobs above

Event listeners

When creating asynchronous processes, you can add the following event hooks:

use Async;

Async::run(function () {

    return 123;
}, [
    'success' => function ($output) { 
        // `$output` of job in this case is `123`.
    },
    'timeout' => function () { 
        // A job took too long to finish.
    },
    'error' => function (\Throwable $exception) {
        // When an exception is thrown from job, it's caught and passed here.
    },
]);

// Another way:
Async::run('[email protected]', [
    'success' => '[email protected]',
    'timeout' => '[email protected]',
    'error' => '[email protected]'
]);

Async::batchRun(
    ['[email protected]', ['success' => '[email protected]']],
    ['[email protected]', ['success' => '[email protected]']],
    ['[email protected]', ['success' => '[email protected]']]
);

Working with complex job

When working with complex job you may want to setup more before it run (ex: job depend on Eloquent model). This package provide you an Artisan command make:async-job to generate a job template. By default, all of the async jobs for your application are stored in the app/AsyncJobs directory. If the app/AsyncJobs directory doesn't exist, it will be created. You may generate a new async job using the Artisan CLI:

php artisan make:async-job MyJob

After created it, you need to prepare your job structure, example:

namespace App\AsyncJobs;

use App\MyModel;
use VXM\Async\Invocation;
use App\MyHandleDependency;
use Illuminate\Queue\SerializesModels;

class MyJob
{

    use Invocation;
    use SerializesModels;

    protected $model;
    
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(MyModel $model)
    {
        $this->model = $model;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle(MyHandleDependency $dependency)
    {
        //
    }
}

In this example, note that we were able to pass an Eloquent model directly into the async job's constructor. Because of the SerializesModels trait that the job is using, Eloquent models will be gracefully serialized and unserialized when the job is processing. If your async job accepts an Eloquent model in its constructor, only the identifier for the model will be serialized onto the queue. When the job is actually handled, the system will automatically re-retrieve the full model instance from the database. It's all totally transparent to your application and prevents issues that can arise from serializing full Eloquent model instances.

The handle method is called when the job is processed in async process. Note that we are able to type-hint dependencies on the handle method of the job. The Laravel service container automatically injects these dependencies.

If you would like to take total control over how the container injects dependencies into the handle method, you may use the container's bindMethod method. The bindMethod method accepts a callback which receives the job and the container. Within the callback, you are free to invoke the handle method however you wish. Typically, you should call this method from a service provider:

use App\AsyncJobs\MyJob;
use App\MyHandleDependency;

$this->app->bindMethod(MyJob::class.'@handle', function ($job, $app) {
    return $job->handle($app->make(MyHandleDependency::class));
});

Now run it asynchronously:

use Async;
use App\MyModel;
use App\AsyncJobs\MyJob;

$model = App\MyModel::find(1);

Async::run(new MyJob($model));

// or batch run with multiple models:

$model2 = App\MyModel::find(2);

Async::batchRun(
    new MyJob($model),
    new MyJob($model2)
);

Compare with queue

You can feel this package look like queue and thing why not using queue?

Queue is a good choice for common async jobs. This package using in cases end-user need to get response in single request but it's a heavy things need to using several processes for calculation or IO heavy operations. And it no need to run a queue listener.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].