Skip to content

Commit

Permalink
Add bachable option to create:job command
Browse files Browse the repository at this point in the history
  • Loading branch information
damsfx committed Sep 26, 2024
1 parent fb325f5 commit b3dbda5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
6 changes: 6 additions & 0 deletions modules/system/console/CreateJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class CreateJob extends BaseScaffoldCommand
protected $signature = 'create:job
{plugin : The name of the plugin. <info>(eg: Winter.Blog)</info>}
{name : The name of the job class to generate. <info>(eg: ImportPosts)</info>}
{--b|batchable : Generates a batchable queue job.}
{--s|sync : Generates a non-queueable job.}
{--f|force : Overwrite existing files with generated files.}
{--uninspiring : Disable inspirational quotes}
Expand Down Expand Up @@ -44,6 +45,9 @@ class CreateJob extends BaseScaffoldCommand
'sync' => [
'scaffold/job/job.stub' => 'jobs/{{studly_name}}.php',
],
'batched' => [
'scaffold/job/job.batched.stub' => 'jobs/{{studly_name}}.php',
],
'queued' => [
'scaffold/job/job.queued.stub' => 'jobs/{{studly_name}}.php',
],
Expand All @@ -56,6 +60,8 @@ public function prepareVars(): array
{
if ($this->option('sync')) {
$this->stubs = $this->jobStubs['sync'];
} elseif ($this->option('batchable')) {
$this->stubs = $this->jobStubs['batched'];
} else {
$this->stubs = $this->jobStubs['queued'];
}
Expand Down
46 changes: 46 additions & 0 deletions modules/system/console/scaffold/job/job.batched.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace {{ plugin_namespace }}\Jobs;

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

class {{studly_name}} implements ShouldQueue
{
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

/**
* Create a new job instance.
*/
public function __construct()
{
//
}

/**
* Execute the job.
*/
public function handle(): void
{
//
}

/**
* Get the middleware the job should pass through.
*/
public function middleware()
{
return []; // Remove this line to activate

return [
// Instruct Laravel to not process the job if
// its corresponding batch has been cancelled
new SkipIfBatchCancelled()
];
}
}

0 comments on commit b3dbda5

Please sign in to comment.