1
0
Fork 0
mirror of https://github.com/archtechx/jobpipeline.git synced 2025-12-12 09:34:04 +00:00
jobpipeline/README.md
Jonathan Prass Martins d7a9e6796e
Possibility to change the default configurations for the queue jobs. (#13)
* Add $queue attribute

This code enables me to change the queue name that the JobPipeline will be pushed to.

Now I can do something like this:
```php
JobPipeline::make([
    Jobs\CreateDatabase::class,
    Jobs\MigrateDatabase::class,
    // Jobs\SeedDatabase::class,

    // Your own jobs to prepare the tenant.
    // Provision API keys, create S3 buckets, anything you want!

])->send(function (Events\TenantCreated $event)
{
    return $event->tenant;
})->onQueue('another-queue')
    ->shouldBeQueued()
```

* Add .idea directory to gitignore

* The following methods where added: onConnection, onQueue, delay, tries, shouldBeQueuedOn. The signature of shouldBeQueued was changed too.

* reuse shouldBeQueuedOn

* updating docs

* added timeout

* docs updated

* .phpunit.cache/ added to gitignore

* tests for shouldQueueOn method

* leave only shouldBeQueued method

* update README

* remove unnecessary property declarations

* Delete  jobpipelinetest.json

* add jobpipelinetest.json as a empty file

* update readme

* simplify tests

* improve code

* improve readme

---------

Co-authored-by: Samuel Štancl <samuel@archte.ch>
2023-03-16 22:14:36 +01:00

110 lines
3.7 KiB
Markdown

# Job Pipeline
<p align="center">
<img width="600" src="https://i.imgur.com/AcVXakZ.png" alt="Job Pipeline" />
</p>
The `JobPipeline` is a simple, yet **extremely powerful** class that lets you **convert any (series of) jobs into event listeners.**
You may use a job pipeline like any other listener, so you can register it in the `EventServiceProvider` using the `$listen` array, or in any other place using `Event::listen()` — up to you.
## Creating job pipelines
> These code snippets will use examples from [my multi-tenancy package](https://github.com/stancl/tenancy).
To create a job pipeline, start by specifying the jobs you want to use:
```php
<?php
use Stancl\JobPipeline\JobPipeline;
use Stancl\Tenancy\Jobs\{CreateDatabase, MigrateDatabase, SeedDatabase};
JobPipeline::make([
CreateDatabase::class,
MigrateDatabase::class,
SeedDatabase::class,
])
```
Then, specify what variable you want to pass to the jobs. This will usually come from the event.
```php
<?php
use Stancl\JobPipeline\JobPipeline;
use Stancl\Tenancy\Jobs\{CreateDatabase, MigrateDatabase, SeedDatabase};
use Stancl\Tenancy\Events\TenantCreated;
JobPipeline::make([
CreateDatabase::class,
MigrateDatabase::class,
SeedDatabase::class,
])->send(function (TenantCreated $event) {
return $event->tenant;
})
```
Next, decide if you want to queue the pipeline. By default, pipelines are synchronous (= not queued) by default.
> 🔥 If you **do** want pipelines to be queued by default, you can do that by setting a static property:
`\Stancl\JobPipeline\JobPipeline::$shouldBeQueuedByDefault = true;`
```php
<?php
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\JobPipeline\JobPipeline;
use Stancl\Tenancy\Jobs\{CreateDatabase, MigrateDatabase, SeedDatabase};
JobPipeline::make([
CreateDatabase::class,
MigrateDatabase::class,
SeedDatabase::class,
])->send(function (TenantCreated $event) {
return $event->tenant;
})->shouldBeQueued(true)
```
If you wish to push the job to a different queue, you can pass a string as the second parameter:
```php
<?php
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\JobPipeline\JobPipeline;
use Stancl\Tenancy\Jobs\{CreateDatabase, MigrateDatabase, SeedDatabase};
JobPipeline::make([
CreateDatabase::class,
MigrateDatabase::class,
SeedDatabase::class,
])->send(function (TenantCreated $event) {
return $event->tenant;
})->shouldBeQueued(true, 'another-queue');
```
This can be simplified by calling `shouldBeQueued(queue: 'another-queue')` since the first parameter defaults to `true`.
Finally, convert the pipeline to a listener and bind it to an event:
```php
<?php
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\JobPipeline\JobPipeline;
use Stancl\Tenancy\Jobs\{CreateDatabase, MigrateDatabase, SeedDatabase};
use Illuminate\Support\Facades\Event;
Event::listen(TenantCreated::class, JobPipeline::make([
CreateDatabase::class,
MigrateDatabase::class,
SeedDatabase::class,
])->send(function (TenantCreated $event) {
return $event->tenant;
})->shouldBeQueued(true)->toListener());
```
Note that you can use job pipelines even for converting single jobs to event listeners. That's useful if you have some logic in job classes and don't want to create listener classes just to be able to run these jobs as a result of an event being fired.
Tip: Returning `false` from a job cancels the execution of all following jobs in the pipeline. This can be useful to cancel a job pipeline that creates, migrates, and seeds databases if the create database job exists (e.g. because it detects that a database already exists). So it can be good to separate jobs into multiple pipelines, so that each logical category of jobs can be stopped individually.