Skip to content

Commit 5830932

Browse files
authored
Add more examples
1 parent 76ffec5 commit 5830932

File tree

95 files changed

+13281
-7
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+13281
-7
lines changed

.env

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
APP_NAME=Laravel
22
APP_ENV=local
3-
APP_KEY=
3+
APP_KEY=base64:YzfitatnBPwXyR6TMQdaNkQ816HHtXePMyOM3EW9XgM=
44
APP_DEBUG=true
55
APP_URL=http://localhost
66

@@ -26,7 +26,7 @@ DB_CONNECTION=mysql
2626
DB_HOST=mysql
2727
DB_PORT=3306
2828
DB_DATABASE=sample
29-
DB_USERNAME=sail
29+
DB_USERNAME=laravel
3030
DB_PASSWORD=password
3131

3232
SESSION_DRIVER=database
@@ -65,3 +65,9 @@ AWS_BUCKET=
6565
AWS_USE_PATH_STYLE_ENDPOINT=false
6666

6767
VITE_APP_NAME="${APP_NAME}"
68+
69+
SHARED_DB_HOST=mysql
70+
SHARED_DB_PORT=3306
71+
SHARED_DB_DATABASE=sample
72+
SHARED_DB_USERNAME=laravel
73+
SHARED_DB_PASSWORD=password

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,19 @@ php artisan test
6262
```
6363

6464
That's it! You can now create and test workflows.
65+
66+
----
67+
68+
#### More Workflows to Explore
69+
70+
In addition to the basic example workflow, you can try these other workflows included in this sample app:
71+
72+
* `php artisan app:elapsed` – Demonstrates how to correctly track start and end times to measure execution duration.
73+
74+
* `php artisan app:microservice` – A fully working example of a workflow that spans multiple Laravel applications using a shared database and queue.
75+
76+
* `php artisan app:playwright` – Runs a Playwright automation, captures a WebM video, encodes it to MP4 using FFmpeg, and then cleans up the WebM file.
77+
78+
* `php artisan app:webhook` – Showcases how to use the built-in webhook system for triggering workflows externally.
79+
80+
Try them out to see Laravel Workflow in action across different use cases!

app/Console/Commands/Init.php

-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ class Init extends Command
2727
*/
2828
public function handle()
2929
{
30-
$this->info('Setting APP_KEY...');
31-
Artisan::call('key:generate');
32-
3330
$this->info('Setting ASSET_URL...');
3431
$this->setAssetUrl();
3532

app/Console/Commands/Microservice.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Workflows\Microservice\MicroserviceWorkflow;
6+
use Illuminate\Console\Command;
7+
use Workflow\WorkflowStub;
8+
9+
class Microservice extends Command
10+
{
11+
/**
12+
* The name and signature of the console command.
13+
*
14+
* @var string
15+
*/
16+
protected $signature = 'app:microservice';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Runs a microservice workflow';
24+
25+
/**
26+
* Execute the console command.
27+
*/
28+
public function handle()
29+
{
30+
$workflow = WorkflowStub::make(MicroserviceWorkflow::class);
31+
$workflow->start();
32+
while ($workflow->running());
33+
$this->info($workflow->output());
34+
}
35+
}

app/Models/StoredWorkflow.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Workflow\Models\StoredWorkflow as BaseStoredWorkflow;
6+
7+
class StoredWorkflow extends BaseStoredWorkflow
8+
{
9+
protected $connection = 'shared';
10+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Workflow\Models\StoredWorkflowException as BaseStoredWorkflowException;
6+
7+
class StoredWorkflowException extends BaseStoredWorkflowException
8+
{
9+
protected $connection = 'shared';
10+
}

app/Models/StoredWorkflowLog.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Workflow\Models\StoredWorkflowLog as BaseStoredWorkflowLog;
6+
7+
class StoredWorkflowLog extends BaseStoredWorkflowLog
8+
{
9+
protected $connection = 'shared';
10+
}

app/Models/StoredWorkflowSignal.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Workflow\Models\StoredWorkflowSignal as BaseStoredWorkflowSignal;
6+
7+
class StoredWorkflowSignal extends BaseStoredWorkflowSignal
8+
{
9+
protected $connection = 'shared';
10+
}

app/Models/StoredWorkflowTimer.php

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Workflow\Models\StoredWorkflowTimer as BaseStoredWorkflowTimer;
6+
7+
class StoredWorkflowTimer extends BaseStoredWorkflowTimer
8+
{
9+
protected $connection = 'shared';
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Workflows\Microservice;
6+
7+
use Workflow\Activity;
8+
9+
class MicroserviceActivity extends Activity
10+
{
11+
public $queue = 'activity';
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Workflows\Microservice;
6+
7+
use Workflow\Activity;
8+
9+
class MicroserviceOtherActivity extends Activity
10+
{
11+
public $queue = 'activity';
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Workflows\Microservice;
6+
7+
use Workflow\ActivityStub;
8+
use Workflow\Workflow;
9+
10+
class MicroserviceWorkflow extends Workflow
11+
{
12+
public function execute()
13+
{
14+
$result = yield ActivityStub::make(MicroserviceActivity::class);
15+
16+
$otherResult = yield ActivityStub::make(MicroserviceOtherActivity::class, 'other');
17+
18+
return 'workflow_' . $result . '_' . $otherResult;
19+
}
20+
}

config/database.php

+20
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,26 @@
6262
]) : [],
6363
],
6464

65+
'shared' => [
66+
'driver' => 'mysql',
67+
'url' => env('SHARED_DB_URL'),
68+
'host' => env('SHARED_DB_HOST', '127.0.0.1'),
69+
'port' => env('SHARED_DB_PORT', '3306'),
70+
'database' => env('SHARED_DB_DATABASE', 'laravel'),
71+
'username' => env('SHARED_DB_USERNAME', 'root'),
72+
'password' => env('SHARED_DB_PASSWORD', ''),
73+
'unix_socket' => env('SHARED_DB_SOCKET', ''),
74+
'charset' => env('SHARED_DB_CHARSET', 'utf8mb4'),
75+
'collation' => env('SHARED_DB_COLLATION', 'utf8mb4_unicode_ci'),
76+
'prefix' => '',
77+
'prefix_indexes' => true,
78+
'strict' => true,
79+
'engine' => null,
80+
'options' => extension_loaded('pdo_mysql') ? array_filter([
81+
PDO::MYSQL_ATTR_SSL_CA => env('SHARED_MYSQL_ATTR_SSL_CA'),
82+
]) : [],
83+
],
84+
6585
'mariadb' => [
6686
'driver' => 'mariadb',
6787
'url' => env('DB_URL'),

config/workflows.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
'workflows_folder' => 'Workflows',
7+
8+
'base_model' => Illuminate\Database\Eloquent\Model::class,
9+
10+
'stored_workflow_model' => App\Models\StoredWorkflow::class,
11+
12+
'stored_workflow_exception_model' => App\Models\StoredWorkflowException::class,
13+
14+
'stored_workflow_log_model' => App\Models\StoredWorkflowLog::class,
15+
16+
'stored_workflow_signal_model' => App\Models\StoredWorkflowSignal::class,
17+
18+
'stored_workflow_timer_model' => App\Models\StoredWorkflowTimer::class,
19+
20+
'workflow_relationships_table' => 'workflow_relationships',
21+
22+
'serializer' => Workflow\Serializers\Y::class,
23+
24+
'prune_age' => '1 month',
25+
26+
'webhooks_route' => env('WORKFLOW_WEBHOOKS_ROUTE', 'webhooks'),
27+
28+
'webhook_auth' => [
29+
'method' => env('WORKFLOW_WEBHOOKS_AUTH_METHOD', 'none'),
30+
31+
'signature' => [
32+
'header' => env('WORKFLOW_WEBHOOKS_SIGNATURE_HEADER', 'X-Signature'),
33+
'secret' => env('WORKFLOW_WEBHOOKS_SECRET'),
34+
],
35+
36+
'token' => [
37+
'header' => env('WORKFLOW_WEBHOOKS_TOKEN_HEADER', 'Authorization'),
38+
'token' => env('WORKFLOW_WEBHOOKS_TOKEN'),
39+
],
40+
],
41+
42+
'monitor' => env('WORKFLOW_MONITOR', false),
43+
44+
'monitor_url' => env('WORKFLOW_MONITOR_URL'),
45+
46+
'monitor_api_key' => env('WORKFLOW_MONITOR_API_KEY'),
47+
48+
'monitor_connection' => env('WORKFLOW_MONITOR_CONNECTION', config('queue.default')),
49+
50+
'monitor_queue' => env(
51+
'WORKFLOW_MONITOR_QUEUE',
52+
config('queue.connections.' . config('queue.default') . '.queue', 'default')
53+
),
54+
];

database/migrations/2022_01_01_000000_create_workflows_table.php

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
final class CreateWorkflowsTable extends Migration
1010
{
11+
protected $connection = 'shared';
12+
1113
/**
1214
* Run the migrations.
1315
*/

database/migrations/2022_01_01_000001_create_workflow_logs_table.php

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
final class CreateWorkflowLogsTable extends Migration
1010
{
11+
protected $connection = 'shared';
12+
1113
/**
1214
* Run the migrations.
1315
*/

database/migrations/2022_01_01_000002_create_workflow_signals_table.php

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
final class CreateWorkflowSignalsTable extends Migration
1010
{
11+
protected $connection = 'shared';
12+
1113
/**
1214
* Run the migrations.
1315
*/

database/migrations/2022_01_01_000003_create_workflow_timers_table.php

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
final class CreateWorkflowTimersTable extends Migration
1010
{
11+
protected $connection = 'shared';
12+
1113
/**
1214
* Run the migrations.
1315
*/

database/migrations/2022_01_01_000004_create_workflow_exceptions_table.php

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
final class CreateWorkflowExceptionsTable extends Migration
1010
{
11+
protected $connection = 'shared';
12+
1113
/**
1214
* Run the migrations.
1315
*/

database/migrations/2022_01_01_000005_create_workflow_relationships_table.php

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
final class CreateWorkflowRelationshipsTable extends Migration
1010
{
11+
protected $connection = 'shared';
12+
1113
/**
1214
* Run the migrations.
1315
*/

docker-compose.yml

+16-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ services:
33
build:
44
context: ./.devcontainer/docker
55
dockerfile: Dockerfile
6-
args:
7-
WWWGROUP: '${WWWGROUP}'
86
extra_hosts:
97
- 'host.docker.internal:host-gateway'
108
ports:
@@ -17,6 +15,22 @@ services:
1715
depends_on:
1816
- mysql
1917
- redis
18+
microservice:
19+
build:
20+
context: ./.devcontainer/docker
21+
dockerfile: Dockerfile
22+
extra_hosts:
23+
- 'host.docker.internal:host-gateway'
24+
ports:
25+
- '8001:80'
26+
volumes:
27+
- './microservice:/var/www/html'
28+
networks:
29+
- laravel
30+
depends_on:
31+
- mysql
32+
- redis
33+
command: ["php", "artisan", "queue:listen", "shared", "--queue=activity"]
2034
mysql:
2135
image: 'mysql/mysql-server:8.0'
2236
ports:

0 commit comments

Comments
 (0)