This commit is contained in:
philipp lang 2023-02-13 00:14:51 +01:00
parent 363f51872a
commit 2efcca2b6a
5 changed files with 157 additions and 12 deletions

View File

@ -0,0 +1,57 @@
<?php
namespace App\Initialize;
use App\Actions\InsertMemberAction;
use App\Actions\PullCoursesAction;
use App\Actions\PullMembershipsAction;
use App\Nami\Api\MemberAction;
use App\Setting\NamiSettings;
use DB;
use Illuminate\Console\Command;
use Lorisleiva\Actions\Concerns\AsAction;
use Zoomyboy\LaravelNami\Api;
use Zoomyboy\LaravelNami\Data\MemberEntry as NamiMember;
use Zoomyboy\LaravelNami\Exceptions\Skippable;
class InitializeMember
{
use AsAction;
public function handle(Api $api): void
{
$allMembers = collect([]);
$jobs = $api->search([])->map(function (NamiMember $member) use ($api) {
return MemberAction::makeJob($api, $member->groupId, $member->id);
})->toArray();
$batch = Bus::batch($jobs)
// ->then(function (Batch $batch) {
// $localMember = InsertMemberAction::run();
// })->catch(function (Batch $batch, Throwable $e) {
// // First batch job failure detected...
// })->finally(function (Batch $batch) {
// // The batch has finished executing...
// })
->dispatch();
try {
} catch (Skippable $e) {
return;
}
// app(PullMembershipsAction::class)->handle($localMember);
// app(PullCoursesAction::class)->handle($localMember);
});
$batch = Bus::batch([
new ImportCsv(1, 100),
new ImportCsv(101, 200),
new ImportCsv(201, 300),
new ImportCsv(301, 400),
new ImportCsv(401, 500),
}
}

View File

@ -2,16 +2,19 @@
namespace App\Initialize;
use App\Actions\InsertMemberAction;
use App\Actions\PullCoursesAction;
use App\Actions\PullMemberAction;
use App\Actions\PullMembershipsAction;
use App\Nami\Api\CompleteMemberToRedisJob;
use App\Setting\NamiSettings;
use DB;
use Illuminate\Bus\Batch;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Redis;
use Lorisleiva\Actions\Concerns\AsAction;
use Zoomyboy\LaravelNami\Api;
use Zoomyboy\LaravelNami\Data\MemberEntry as NamiMember;
use Zoomyboy\LaravelNami\Exceptions\Skippable;
class InitializeMembers
{
@ -22,17 +25,26 @@ class InitializeMembers
public function handle(Api $api): void
{
$allMembers = collect([]);
Redis::delete('members');
$api->search([])->each(function (NamiMember $member) {
try {
$localMember = app(PullMemberAction::class)->handle($member->groupId, $member->id);
} catch (Skippable $e) {
return;
}
$jobs = $api->search([])->map(function (NamiMember $member) use ($api) {
return new CompleteMemberToRedisJob($api, $member->groupId, $member->id);
})->toArray();
app(PullMembershipsAction::class)->handle($localMember);
app(PullCoursesAction::class)->handle($localMember);
});
$batch = Bus::batch($jobs)
->finally(function (Batch $batch) {
dd(Redis::get('members'));
})
->dispatch();
// $localMember = InsertMemberAction::run();
// })->catch(function (Batch $batch, Throwable $e) {
// // First batch job failure detected...
// })->finally(function (Batch $batch) {
// // The batch has finished executing...
// })
// app(PullMembershipsAction::class)->handle($localMember);
// app(PullCoursesAction::class)->handle($localMember);
}
public function restore(): void

View File

@ -0,0 +1,37 @@
<?php
namespace App\Nami\Api;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Redis;
use Zoomyboy\LaravelNami\Api;
class CompleteMemberToRedisJob implements ShouldQueue
{
use Batchable;
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
public function __construct(
private Api $api,
private int $groupId,
private int $memberId,
) {
}
public function handle(): void
{
Redis::rpush('members', collect([
'member' => MemberAction::run($this->api, $this->groupId, $this->memberId),
'memberships' => MembershipsOfAction::run($this->api, $this->memberId),
'courses' => CoursesOfAction::run($this->api, $this->memberId),
])->toJson());
}
}

View File

@ -187,7 +187,7 @@ return [
'connection' => 'redis',
'queue' => ['default'],
'balance' => 'simple',
'processes' => 3,
'processes' => 10,
'tries' => 1,
],
'supervisor-long-running' => [

View File

@ -0,0 +1,39 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('job_batches', function (Blueprint $table) {
$table->string('id')->primary();
$table->string('name');
$table->integer('total_jobs');
$table->integer('pending_jobs');
$table->integer('failed_jobs');
$table->longText('failed_job_ids');
$table->mediumText('options')->nullable();
$table->integer('cancelled_at')->nullable();
$table->integer('created_at');
$table->integer('finished_at')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('job_batches');
}
};