Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: batch avatar generation for larger user numbers #4

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A [Flarum](http://flarum.org) extension. Replace default Flarum avatars with Bor

## About

Utilize generated "Boring Avatars" on your Flarum forum, without compromising your user privacy.
Utilize generated "Boring Avatars" on your Flarum forum, without compromising your user privacy. It is a PHP port of the [React based Boring Avatars](https://github.com/boringdesigners/boring-avatars)

## Features

Expand Down
40 changes: 40 additions & 0 deletions src/Job/AvatarGenerationBatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* This file is part of ianm/boring-avatars.
*
* Copyright (c) 2024 IanM.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace IanM\BoringAvatars\Job;

use Flarum\Database\Eloquent\Collection;
use Flarum\Queue\AbstractJob;
use IanM\BoringAvatars\BoringAvatar;
use IanM\BoringAvatars\Command\GenerateAvatar;
use Illuminate\Contracts\Bus\Dispatcher;

class AvatarGenerationBatch extends AbstractJob
{
public function __construct(
protected Collection $users,
protected bool $force = false
) {
}

public function handle(Dispatcher $bus): void
{
$this->users->each(function ($user) use ($bus) {
if ($this->force || $user->user_svg === null) {
if ($user->user_svg !== null) {
$user->user_svg = null;
$user->save();
}
$bus->dispatch(new GenerateAvatar($user, BoringAvatar::$defaultGenerationSize, BoringAvatar::$defaultSquareAvatar));
}
});
}
}
17 changes: 10 additions & 7 deletions src/Job/AvatarGenerationJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,26 @@

namespace IanM\BoringAvatars\Job;

use Flarum\Database\Eloquent\Collection;
use Flarum\Queue\AbstractJob;
use IanM\BoringAvatars\Console\GenerateBoringAvatars;
use Illuminate\Contracts\Container\Container;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Flarum\User\User;
use Illuminate\Contracts\Queue\Queue;

class AvatarGenerationJob extends AbstractJob
{
const BATCH_SIZE = 1000;

public function __construct(
protected bool $force = false
) {
}

public function handle(GenerateBoringAvatars $command, Container $container): void
public function handle(Queue $queue): void
{
$command->setLaravel($container);
$force = $this->force;

$command->run(new ArrayInput($this->force ? ['--force' => true] : []), new NullOutput());
User::query()->chunkById(self::BATCH_SIZE, function (Collection $users) use ($queue, $force) {
$queue->push(new AvatarGenerationBatch($users, $force));
});
}
}
Loading