Start by installing the package via Composer:
composer require badrshs/laravel-data-jobs
Run the installation command to set up the database table:
php artisan data-jobs:install
First Use Case: Create a simple data migration command.
php artisan make:command MigrateCustomerData
DataJobable trait and define the migration logic:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Badrshs\LaravelDataJobs\Contracts\DataJobable;
class MigrateCustomerData extends Command
{
use DataJobable;
protected $signature = 'data:migrate-customers';
protected $description = 'Migrate customer data to new schema';
public function handle()
{
// Your migration logic here
$this->info('Customer data migrated successfully!');
return self::SUCCESS;
}
}
php artisan data:run-jobs
Job Creation:
DataJobable trait for all one-time data migration commands.getJobPriority() (lower numbers run first).getJobParameters() for job-specific configurations.Execution:
php artisan data:run-jobs
php artisan data:run-jobs --job=MigrateCustomerData
php artisan data:run-jobs --force
Tracking and Debugging:
data_jobs_log table.--verbose flag for detailed output:
php artisan data:run-jobs --verbose
Laravel Queues: Integrate with Laravel’s queue system for asynchronous execution by dispatching jobs in the handle() method:
public function handle()
{
dispatch(new MigrateCustomerDataJob());
}
Priority Management: Use priority to control execution order for dependent jobs:
public function getJobPriority(): int
{
return 5; // Higher priority than default (100)
}
Conditional Execution: Disable jobs temporarily using isEnabled():
public function isEnabled(): bool
{
return env('RUN_MIGRATIONS', false);
}
Custom Logging: Extend the logging mechanism by publishing the config and customizing the log_table or adding additional columns to the data_jobs_log table.
Database Schema Conflicts:
data_jobs_log table migration is run before executing jobs. If the table is missing, the package will throw an error.php artisan migrate if the table is not created.Job Discovery Issues:
DataJobable trait. If jobs aren’t running, verify:
app/Console/Kernel.php.php artisan optimize:clear
Priority Collisions:
Logging Disabled:
logging_enabled is set to false in the config, jobs will run without tracking.logging_enabled is true in config/data-jobs.php for proper tracking.Check Job Statuses:
php artisan tinker
\Badrshs\LaravelDataJobs\Models\DataJobLog::all();
Verbose Output:
Use the --verbose flag to see detailed execution logs:
php artisan data:run-jobs --verbose
Force Fresh Run: Clear existing logs and run jobs fresh:
php artisan data:run-jobs --fresh
Custom Job Parameters:
Extend the getJobParameters() method to pass dynamic data to jobs:
public function getJobParameters(): array
{
return [
'batch_size' => 1000,
'start_date' => now()->subDays(7)
];
}
Custom Status Handling:
Override the default statuses (pending, running, completed, failed) by extending the DataJobLog model or adding custom columns to the data_jobs_log table.
Event Listeners:
Add event listeners for job status changes by publishing the package’s event classes and registering listeners in EventServiceProvider:
protected $listen = [
\Badrshs\LaravelDataJobs\Events\JobStarted::class => [
\App\Listeners\LogJobStart::class,
],
\Badrshs\LaravelDataJobs\Events\JobCompleted::class => [
\App\Listeners\NotifyJobCompletion::class,
],
];
Retry Mechanism:
Implement a retry logic for failed jobs by catching exceptions in the handle() method and re-running the job:
public function handle()
{
try {
// Migration logic
} catch (\Exception $e) {
$this->error('Migration failed: ' . $e->getMessage());
return self::FAILURE;
}
}
Environment-Specific Jobs:
Use the isEnabled() method to control job execution based on environment variables:
public function isEnabled(): bool
{
return app()->environment('production');
}
How can I help you explore Laravel packages today?