PHP code example of codetechnl / laravel-taskbridge

1. Go to this page and download the library: Download codetechnl/laravel-taskbridge library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codetechnl / laravel-taskbridge example snippets


use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

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

    public function handle(): void
    {
        // your logic here
    }
}

use CodeTechNL\TaskBridge\Contracts\HasPredefinedCronExpression;

class SendDailyReport implements HasPredefinedCronExpression, ShouldQueue
{
    public function cronExpression(): string
    {
        return '0 8 * * *'; // every day at 08:00
    }

    public function handle(): void { ... }
}

#[SchedulableJob(cron: '0 8 * * *')]
class SendDailyReport implements ShouldQueue { ... }

use CodeTechNL\TaskBridge\Contracts\HasPredefinedCronExpression;

class GenerateReport implements HasPredefinedCronExpression, ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function __construct(
        public readonly string $type,
        public readonly int    $rows    = 1000,
        public readonly bool   $dryRun  = false,
    ) {}

    public function cronExpression(): string { return '0 6 * * 1'; }

    public function handle(): void { /* uses $this->type, $this->rows, $this->dryRun */ }
}

public function __construct(
    public readonly ?string $recipient,  // 

public function __construct(
    public readonly ?int $retentionDays = null,  // optional — leave blank to use config default
) {}

public function handle(): void
{
    $days = $this->retentionDays ?? (int) config('taskbridge.logging.retention_days', 30);
    // ...
}

use CodeTechNL\TaskBridge\Contracts\HasCustomLabel;

class SendDailyReport implements HasCustomLabel, ShouldQueue
{
    public function taskLabel(): string
    {
        return 'Daily Report — Finance';
    }
}

use CodeTechNL\TaskBridge\Contracts\HasGroup;

class SendDailyReport implements HasGroup, ShouldQueue
{
    public function group(): string
    {
        return 'Reporting'; // Overrides the folder-based detection.
    }
}

use CodeTechNL\TaskBridge\Concerns\SkipOnMaintenance;

class SendDailyReport implements ShouldQueue
{
    use SkipOnMaintenance;

    public function handle(): void { ... }
}

use CodeTechNL\TaskBridge\Concerns\SkipOnMaintenance;
use CodeTechNL\TaskBridge\Contracts\RunsConditionally;

class SendDailyReport implements RunsConditionally, ShouldQueue
{
    use SkipOnMaintenance;

    public function shouldRun(): bool
    {
        return $this->someCondition(); // only checked when not in maintenance
    }
}

use CodeTechNL\TaskBridge\Contracts\RunsConditionally;

class SendDailyReport implements RunsConditionally, ShouldQueue
{
    public function shouldRun(): bool
    {
        return $this->someCondition();
    }
}

use CodeTechNL\TaskBridge\Concerns\HasJobOutput;
use CodeTechNL\TaskBridge\Contracts\ReportsTaskOutput;

class ImportProducts implements ReportsTaskOutput, ShouldQueue
{
    use HasJobOutput;

    public function handle(): void
    {
        $processed = 0;
        $skipped   = 0;

        // ... your import logic ...

        $this->reportOutput([
            'processed' => $processed,
            'skipped'   => $skipped,
        ]);
    }
}

// config/taskbridge.php
'auto_discovery' => [
    'mode'  => 'interface',   // default
    'paths' => [
        app_path('Jobs'),
    ],
],

'auto_discovery' => [
    'mode'  => 'attribute',
    'paths' => [
        app_path('Jobs'),
    ],
],

use CodeTechNL\TaskBridge\Attributes\SchedulableJob;

#[SchedulableJob(name: 'Daily Report', group: 'Reporting', cron: '0 8 * * *')]
class SendDailyReport implements ShouldQueue
{
    // ...
}

'auto_discovery' => [
    'mode'  => null,   // discovery disabled
    'paths' => [],
],

'jobs' => [
    \App\Jobs\SendDailyReport::class,
    \Vendor\Package\Jobs\SomeScheduledJob::class,
],

'auto_discovery' => [
    'mode'  => 'interface',
    'paths' => [app_path('Jobs')],
],

'jobs' => [
    \Vendor\Package\Jobs\SomeScheduledJob::class,
],

#[SchedulableJob(
    name:  'Daily Finance Report',   // overrides HasCustomLabel::taskLabel()
    group: 'Finance',                // overrides HasGroup::group()
    cron:  '0 8 * * 1-5',           // overrides HasPredefinedCronExpression::cronExpression()
)]
class SendDailyReport implements ShouldQueue { ... }

// config/taskbridge.php
'schedules' => [
    \App\Jobs\SendDailyReport::class => [
        'cron'      => '0 8 * * *',
        'arguments' => [],
    ],
    \App\Jobs\GenerateReport::class => [
        'cron'      => '0 6 * * 1',
        'arguments' => ['weekly', 500, false],
    ],
],

use CodeTechNL\TaskBridge\Facades\TaskBridge;

$result = TaskBridge::sync();
// $result->created, $result->updated, $result->removed

// Run immediately (bypasses enabled/shouldRun check)
$run = TaskBridge::run(SendDailyReport::class, force: true);

// Run with enabled/shouldRun checks
$run = TaskBridge::run(SendDailyReport::class);

// Dry run — calls handle() but Bus::fake() prevents actual queue dispatches
$run = TaskBridge::run(SendDailyReport::class, dryRun: true);

// Pass constructor arguments
$run = TaskBridge::run(GenerateReport::class, force: true, arguments: ['weekly', 500, false]);

use Carbon\Carbon;

TaskBridge::scheduleOnce(GenerateReport::class, Carbon::parse('2024-06-01 09:00'));

// With constructor arguments
TaskBridge::scheduleOnce(GenerateReport::class, Carbon::parse('2024-06-01 09:00'), ['annual', 5000]);

$run = TaskBridge::scheduleOnce(WakeWorkflowRun::class, $at, [$runId], log: false);

$run->output['aws_schedule_name']; // "once-wake-workflow-run-k3j9x2ab"

TaskBridge::getEventBridge()->remove($scheduleName);

TaskBridge::enable(SendDailyReport::class);  // enables + syncs to EventBridge
TaskBridge::disable(SendDailyReport::class); // disables + removes from EventBridge

TaskBridge::overrideCron(SendDailyReport::class, '*/15 * * * *');
TaskBridge::resetCron(SendDailyReport::class); // restore original

// config/taskbridge.php

return [
    'models' => [
        // Override with your own model if needed (must extend the original)
        'scheduled_job'     => \CodeTechNL\TaskBridge\Models\ScheduledJob::class,
        'scheduled_job_run' => \CodeTechNL\TaskBridge\Models\ScheduledJobRun::class,
    ],

    // Prefix applied to all job identifiers. Defaults to slugified APP_ENV.
    'name_prefix' => env('TASKBRIDGE_NAME_PREFIX', Str::slug(env('APP_ENV', 'local'))),

    'eventbridge' => [
        'region'         => env('AWS_DEFAULT_REGION', 'eu-west-1'),
        'role_arn'       => env('TASKBRIDGE_SCHEDULER_ROLE_ARN'),
        'schedule_group' => env('TASKBRIDGE_SCHEDULE_GROUP', 'default'),
        'retry_policy'   => [
            'maximum_event_age_seconds' => env('TASKBRIDGE_RETRY_MAX_AGE_SECONDS', 86400),
            'maximum_retry_attempts'    => env('TASKBRIDGE_RETRY_MAX_ATTEMPTS', 185),
        ],
    ],

    'auto_discovery' => [
        // 'interface' — register every ShouldQueue job with a scalar constructor (default)
        // 'attribute' — register only classes carrying #[SchedulableJob]
        // null        — disable discovery; use 'jobs' array only
        'mode'  => env('TASKBRIDGE_DISCOVERY_MODE', 'interface'),
        'paths' => [
            app_path('Jobs'),
        ],
    ],

    'jobs' => [],

    // Predefined schedules — imported via `php artisan taskbridge:import-schedules`
    // Each entry must be an array with a 'cron' key. Plain-string shorthand is not supported.
    'schedules' => [
        // \App\Jobs\SendDailyReport::class => [
        //     'cron'      => '0 8 * * *',
        //     'arguments' => [],
        // ],
    ],

    'logging' => [
        'enabled'        => env('TASKBRIDGE_LOGGING_ENABLED', true),
        'retention_days' => env('TASKBRIDGE_RUN_RETENTION_DAYS', 30),
    ],
];

use CodeTechNL\TaskBridge\Events\JobExecutionFailed;

Event::listen(JobExecutionFailed::class, function (JobExecutionFailed $event) {
    // $event->job       — ScheduledJob model
    // $event->run       — ScheduledJobRun model
    // $event->exception — Throwable
    Notification::send(...);
});

// app/Models/ScheduledJob.php
class ScheduledJob extends \CodeTechNL\TaskBridge\Models\ScheduledJob
{
    // custom scopes, relations, etc.
}

// config/taskbridge.php
'models' => [
    'scheduled_job'     => App\Models\ScheduledJob::class,
    'scheduled_job_run' => App\Models\ScheduledJobRun::class,
],
bash
php artisan vendor:publish --tag=taskbridge-migrations
php artisan migrate
bash
php artisan vendor:publish --tag=taskbridge-config
bash
php artisan vendor:publish --tag=taskbridge-lang