项目作者: renoki-co

项目描述 :
Chained Jobs Shared Data is a package that helps you share some data (usually an array) between chained jobs.
高级语言: PHP
项目地址: git://github.com/renoki-co/laravel-chained-jobs-shared-data.git
创建时间: 2020-06-17T19:30:04Z
项目社区:https://github.com/renoki-co/laravel-chained-jobs-shared-data

开源协议:Apache License 2.0

下载


Laravel Chained Jobs Shared Data

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:

  1. 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.

  1. // use Illuminate\Bus\Queueable;
  2. // use Illuminate\Foundation\Bus\Dispatchable;
  3. use RenokiCo\ChainedJobsSharedData\Traits\Dispatchable;
  4. use RenokiCo\ChainedJobsSharedData\Traits\Queueable;
  5. class MyJob implements ShouldQueue
  6. {
  7. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  8. // the rest of job here
  9. }

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.

  1. CreateUser::withChain([
  2. new CreateApiKey,
  3. new MakeTestApiCall,
  4. ])->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:

  1. // CreateUser.php
  2. public function handle()
  3. {
  4. $user = $this->createUser();
  5. $this->sharedData['user'] = $user;
  6. }
  1. // CreateApiKey.php
  2. public function handle()
  3. {
  4. $apiKey = $this->createApiKeyForUser($this->sharedData['user']);
  5. $this->sharedData['api_key'] = $apiKey;
  6. }
  1. // MakeTestApiCall.php
  2. public function handle()
  3. {
  4. $this->makeApiCall(
  5. $this->sharedData['user'],
  6. $this->sharedData['api_key'],
  7. );
  8. }

🐛 Testing

  1. 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