You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 3, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+98Lines changed: 98 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,6 +40,7 @@ That's right! Let's just be clear that we're not talking about **Batched Jobs**.
40
40
- They are volatile, meaning if you lose one job in the chain - you lose the whole chain.
41
41
- They do not provide the `then`, `catch`, `finally` callable methods that batched jobs do.
42
42
- Long delays with memory based or SQS queue is not possible as you could lose the jobs due to expiry or if the server shuts down.
43
+
- You can't share data between jobs as there is no "state" across the chain
43
44
44
45
Laravel Haystack aims to solve this by storing the job chain in the database and queuing one job at a time. When the job is completed, Laravel Haystack listens out for the "job completed" event and queues the next job in the chain from the database.
45
46
@@ -49,6 +50,7 @@ Laravel Haystack aims to solve this by storing the job chain in the database and
49
50
- It provides callback methods like `then`, `catch` and `finally`.
50
51
- Global middleware that can be applied to every single job in the chain
51
52
- Delay that can be added to every job in the chain
53
+
- You can store and retrieve data/state that is accessible to every job in the chain.
52
54
- You can store the model for later processing.
53
55
54
56
### Use Cases
@@ -429,6 +431,102 @@ class ProcessPodcast implements ShouldQueue, StackableJo
429
431
}
430
432
```
431
433
434
+
## Shared Job Data
435
+
436
+
Laravel Haystack has the ability for your jobs to store and retrieve state/data between jobs. This is really useful if you need to store data in the first job and then in the second job, process the data and in the final job, email the processed data. You are able to create a process pipeline since each job is processed in sequential order. This is really exciting because with traditional chained jobs, you cannot share data between jobs.
437
+
438
+
```php
439
+
<?php
440
+
441
+
$haystack = Haystack::build()
442
+
->addJob(new RetrieveDataFromApi)
443
+
->addJob(new ProcessDataFromApi)
444
+
->addJob(new StoreDataFromApi)
445
+
->dispatch();
446
+
```
447
+
448
+
### Storing data inside of jobs
449
+
450
+
Inside your job, you can use the `setHaystackData()` method to store some data. This method accepts a key, value and optional Eloquent cast.
451
+
452
+
```php
453
+
<?php
454
+
455
+
namespace App\Jobs;
456
+
457
+
use Sammyjo20\LaravelHaystack\Contracts\StackableJob;
458
+
use Sammyjo20\LaravelHaystack\Concerns\Stackable;
459
+
460
+
class RetrieveDataFromApi implements ShouldQueue, StackableJob
461
+
{
462
+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Stackable
463
+
464
+
public function handle()
465
+
{
466
+
// Your application code...
467
+
468
+
$this->setHaystackData('username', 'Sammyjo20');
469
+
}
470
+
```
471
+
472
+
### Casting data
473
+
474
+
The `setHaystackData` method supports any data type. It supports fully casting your data into any of Eloquent's existing casts, or even your custom casts. Just provide a third argument to specify the cast.
475
+
476
+
```php
477
+
<?php
478
+
479
+
namespace App\Jobs;
480
+
481
+
use Sammyjo20\LaravelHaystack\Contracts\StackableJob;
482
+
use Sammyjo20\LaravelHaystack\Concerns\Stackable;
483
+
484
+
class RetrieveDataFromApi implements ShouldQueue, StackableJob
485
+
{
486
+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Stackable
487
+
488
+
public function handle()
489
+
{
490
+
// Your application code...
491
+
492
+
// Array Data, provide third argument to specify cast.
From one job you can set the data, but that data will be available to every job in the haystack there after. Just use the `getHaystackData` method to get data by key or use the `allHaystackData` to get a collection containing the haystack data.
505
+
506
+
```php
507
+
<?php
508
+
509
+
namespace App\Jobs;
510
+
511
+
use Sammyjo20\LaravelHaystack\Contracts\StackableJob;
512
+
use Sammyjo20\LaravelHaystack\Concerns\Stackable;
513
+
514
+
class RetrieveDataFromApi implements ShouldQueue, StackableJob
515
+
{
516
+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, Stackable
By default, Laravel Haystack will use events to automatically process the next job in the haystack. If you would like to disable this functionality, you will need to make sure to disable the `automatic_processing` in your config/haystack.php file. After that, you will need to make sure your jobs tell Haystack when to process the next job.
0 commit comments