-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a run all transformer area with ui updates
- Loading branch information
Showing
12 changed files
with
243 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace App\Events; | ||
|
||
use App\Models\Project; | ||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Broadcasting\PrivateChannel; | ||
use Illuminate\Contracts\Broadcasting\ShouldBroadcast; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class TransformersDoneEvent implements ShouldBroadcast | ||
{ | ||
use Dispatchable, InteractsWithSockets, SerializesModels; | ||
|
||
/** | ||
* Create a new event instance. | ||
*/ | ||
public function __construct( | ||
public Project $project, | ||
public string $transformer_name) | ||
{ | ||
|
||
} | ||
|
||
public function broadcastWith(): array | ||
{ | ||
return [ | ||
'project_id' => $this->project->id, | ||
'transformer_name' => $this->transformer_name, | ||
]; | ||
} | ||
|
||
/** | ||
* Get the channels the event should broadcast on. | ||
* | ||
* @return array<int, \Illuminate\Broadcasting\Channel> | ||
*/ | ||
public function broadcastOn(): array | ||
{ | ||
return [ | ||
new PrivateChannel('projects.'.$this->project->id), | ||
]; | ||
} | ||
|
||
/** | ||
* The event's broadcast name. | ||
* | ||
* @return string | ||
*/ | ||
public function broadcastAs() | ||
{ | ||
return 'transformers.done'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Jobs\RunTransformerJob; | ||
use App\Models\Project; | ||
use Illuminate\Bus\Batch; | ||
use Illuminate\Support\Facades\Bus; | ||
use Throwable; | ||
|
||
class TransformersRunController extends Controller | ||
{ | ||
public function run(Project $project) | ||
{ | ||
$jobs = []; | ||
|
||
foreach ($project->transformers->sortBy('order') as $transformer) { | ||
$jobs[] = new RunTransformerJob($transformer); | ||
} | ||
|
||
$batch = Bus::batch($jobs)->then(function (Batch $batch) { | ||
// Execute when batch is finished | ||
})->catch(function (Batch $batch, Throwable $e) use ($project) { | ||
logger('Errors with project '.$project->id); | ||
})->finally(function (Batch $batch) { | ||
|
||
})->dispatch(); | ||
|
||
request()->session()->flash('flash.banner', 'Running transformers'); | ||
|
||
return back(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Events\TransformersDoneEvent; | ||
use App\Models\Transformer; | ||
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\SerializesModels; | ||
|
||
class RunTransformerJob implements ShouldQueue | ||
{ | ||
use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
/** | ||
* Create a new job instance. | ||
*/ | ||
public function __construct(public Transformer $transformer) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Execute the job. | ||
*/ | ||
public function handle(): void | ||
{ | ||
$this->transformer->run(); | ||
|
||
TransformersDoneEvent::dispatch($this->transformer->project, str($this->transformer->type->name)->headline()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
tests/Feature/Http/Controllers/TransformersRunControllerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Tests\Feature\Http\Controllers; | ||
|
||
use App\Models\Project; | ||
use App\Models\Source; | ||
use App\Models\Transformer; | ||
use App\Models\User; | ||
use Illuminate\Support\Facades\Bus; | ||
use Tests\TestCase; | ||
|
||
class TransformersRunControllerTest extends TestCase | ||
{ | ||
public function test_runs() | ||
{ | ||
Bus::fake(); | ||
$project = Project::factory()->create(); | ||
$source = Source::factory()->create([ | ||
'project_id' => $project->id, | ||
]); | ||
$transformer1 = Transformer::factory()->create([ | ||
'project_id' => $project->id, | ||
'order' => 1, | ||
]); | ||
$transformer2 = Transformer::factory()->create([ | ||
'project_id' => $project->id, | ||
'order' => 2, | ||
]); | ||
$user = User::factory()->create(); | ||
$response = $this->actingAs($user)->post(route('transformers.run', $project)); | ||
Bus::assertBatchCount(1); | ||
} | ||
} |