PHP code example of schoolpalm / queued-jobs

1. Go to this page and download the library: Download schoolpalm/queued-jobs 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/ */

    

schoolpalm / queued-jobs example snippets




namespace App\Jobs;

use SchoolPalm\QueuedJobs\Jobs\ContextAwareJob;

class GenerateReport extends ContextAwareJob
{
    public function __construct(
        private readonly array $reportData
    ) {}

    public function handle(): void
    {
        // Context is automatically available
        $context = $this->getQueueContext();

        $schoolId = $context['school_id'];
        $userId   = $context['user_id'];
        $module   = $context['module'];

        // ... your job logic
    }
}



namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use SchoolPalm\QueuedJobs\Facades\QueuedJobs;

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        QueuedJobs::resolveContextUsing(function () {
            return [
                'tenant_id' => tenancy()->tenant?->id,
                'school_id' => session('school_id'),
                'user_id'   => auth()->id(),
                'module'    => request()->route('module'),
                'metadata'  => [
                    'ip' => request()->ip(),
                ],
            ];
        });
    }
}

QueuedJobs::restoreContextUsing(function (QueueContext $context) {
    // Switch tenant database
    if ($context->tenantId()) {
        tenancy()->initialize($context->tenantId());
    }

    // Set current school
    if ($context->schoolId()) {
        session(['school_id' => $context->schoolId()]);
    }

    // Authenticate user
    if ($context->userId()) {
        auth()->loginUsingId($context->userId());
    }
});

use SchoolPalm\QueuedJobs\Facades\QueuedJobs;

// Basic dispatch — context is captured automatically
QueuedJobs::job(new GenerateReport($data))->dispatch();

// With fluent context overrides
QueuedJobs::job(new GenerateReport($data))
    ->withTenant(1)
    ->withSchool(20)
    ->withUser(50)
    ->withModule('reports')
    ->withMetadata(['source' => 'api'])
    ->onQueue('high')
    ->onConnection('redis')
    ->delay(now()->addMinutes(5))
    ->dispatch();

$builder = QueuedJobs::job(new GenerateReport($data))
    ->withSchool(10)
    ->dispatch();

// Access the created result directly after dispatch
$result = $builder->result();                 // QueueJobResult model
$resource = $builder->resultResource();       // JobResultResource
$array = $builder->resultArray();             // API-friendly array

use SchoolPalm\QueuedJobs\Managers\JobResultManager;

$manager = app(JobResultManager::class);

$result = $manager->create($job, [
    'school_id' => 10,
    'user_id' => 5,
]);

// Inside your job's handle() method
$this->completeResult(['file' => 'report.pdf', 'pages' => 10]);
$this->failResult('Processing failed: memory limit exceeded');

use SchoolPalm\QueuedJobs\Facades\QueuedJobs;

// Get results builder
$jobs = QueuedJobs::jobs();

// Filtering
$jobs->forSchool(10)
     ->forUser(5)
     ->forModule('reports')
     ->completed()
     ->latest();

// Execute
$results = $jobs->get();
$result  = $jobs->first();
$paginated = $jobs->paginate(20);
$count   = $jobs->count();
$exists  = $jobs->exists();

// Or obtain API-friendly resources directly
$resources = $jobs->resources();        // array of JobResultResource arrays
$firstResource = $jobs->firstResource(); // single array or null
bash
php artisan vendor:publish --tag=queued-jobs-config
bash
php artisan migrate