renoki-co/laravel-chained-jobs-shared-data

This package is abandoned and no longer maintained. No replacement package was suggested.

Share data between chained jobs in Laravel.

1.1.0 2020-09-06 18:50 UTC

This package is auto-updated.

Last update: 2020-12-05 13:44:46 UTC


README

CI codecov StyleCI Latest Stable Version Total Downloads Monthly Downloads License

Chained Jobs Shared Data is a package that helps you share some data (usually an array) between chained jobs.

🤝 Supporting

Renoki Co. on GitHub aims on bringing a lot of open source projects and helpful projects to the world. Developing and maintaining projects everyday is a harsh work and tho, we love it.

If you are using your application in your day-to-day job, on presentation demos, hobby projects or even school projects, spread some kind words about our work or sponsor our work. Kind words will touch our chakras and vibe, while the sponsorships will keep the open source projects alive.

🚀 Installation

You can install the package via composer:

composer require renoki-co/laravel-chained-jobs-shared-data

🙌 Usage

You just have to replace the default job's Dispatchable and Queueable traits with the ones provided by this package.

// use Illuminate\Bus\Queueable;
// use Illuminate\Foundation\Bus\Dispatchable;
use RenokiCo\ChainedJobsSharedData\Traits\Dispatchable;
use RenokiCo\ChainedJobsSharedData\Traits\Queueable;

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

    // the rest of job here
}

Use Case

The main use case is to share some data between chained jobs, while being able to modify it and be retrieved in the next jobs with the previous modifications.

CreateUser::withChain([
    new CreateApiKey,
    new MakeTestApiCall,
])->dispatch();

The CreateApiKey and MakeTestApiCall are the jobs that depend by the CreateUser at glance.

Without using this package's traits, the only workaround would be to trigger the CreateApiKey in the CreateUser job, then trigger the next one, and the next one, etc. and you will end up bad code to manage and troubleshoot.

If all job classes use the previous mentioned traits, having some shared data is going to ease the job:

// CreateUser.php

public function handle()
{
    $user = $this->createUser();

    $this->sharedData['user'] = $user;
}
// CreateApiKey.php

public function handle()
{
    $apiKey = $this->createApiKeyForUser($this->sharedData['user']);

    $this->sharedData['api_key'] = $apiKey;
}
// MakeTestApiCall.php

public function handle()
{
    $this->makeApiCall(
        $this->sharedData['user'],
        $this->sharedData['api_key'],
    );
}

🐛 Testing

vendor/bin/phpunit

🤝 Contributing

Please see CONTRIBUTING for details.

🔒 Security

If you discover any security related issues, please email alex@renoki.org instead of using the issue tracker.

🎉 Credits