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

Defer all stat increments to a job #17

Open
wants to merge 1 commit into
base: REL1_39
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions ServiceWiring.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@

CheevosHelper::class => static function ( MediaWikiServices $services ): CheevosHelper {
return new CheevosHelper(
$services->getService( AchievementService::class ),
$services->getMainConfig(),
$services->getService( GlobalTitleLookup::class ),
$services->getService( WikiConfigDataService::class )
$services->getService( WikiConfigDataService::class ),
$services->getJobQueueGroup()
);
},
];
35 changes: 11 additions & 24 deletions src/CheevosHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Exception;
use Fandom\Includes\Article\GlobalTitleLookup;
use Fandom\WikiConfig\WikiVariablesDataService;
use JobQueueGroup;
use MediaWiki\Linker\LinkTarget;
use MediaWiki\MediaWikiServices;
use MediaWiki\User\UserIdentity;
Expand All @@ -31,10 +32,10 @@ class CheevosHelper {
private static bool $shutdownRan = false;

public function __construct(
private AchievementService $achievementService,
private Config $config,
private GlobalTitleLookup $globalTitleLookup,
private WikiConfigDataService $wikiConfigDataService
private WikiConfigDataService $wikiConfigDataService,
private JobQueueGroup $jobQueueGroup
) {
}

Expand Down Expand Up @@ -72,29 +73,15 @@ public function increment( string $stat, int $delta, UserIdentity $user, array $
}

private function doIncrements() {
// Attempt to do it NOW. If we get an error, fall back to the SyncService job.
Copy link

@rnix-fandom rnix-fandom Jun 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for posterity: the intent of this was to make sure the stats for a page edit was processed in time for the page view immediately afterwards to show the toast notifications for any achievements earned. otherwise it was kinda weird for a first-time editor to edit a page and then at some indeterminate time later when they next made a page view get that toast. but among all the jank in the current Fandom ™️ first time editor experience, this is not as huge a deal as compared to what it was on Gamepedia.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I was to guess this might already have been broken, since this runs in a shutdown function, by which time the user will have already been redirected after a successful edit since FPM will have flushed the response due to MediaWiki running fastcgi_finish_request as part of its request termination logic.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree, it was probably always race-y, and just got less likely to do the intended behavior over time as more stuff was being done in shutdown and as external services became more numerous.

try {
self::$shutdownRan = true;
foreach ( self::$increments as $userId => $increment ) {
$return = $this->achievementService->increment( $increment );
unset( self::$increments[$userId] );
if ( isset( $return['earned'] ) ) {
foreach ( $return['earned'] as $achievement ) {
$achievement = new CheevosAchievement( $achievement );
$this->achievementService->broadcastAchievement(
$achievement,
$increment['site_key'],
$increment['user_id']
);
}
}
}
} catch ( CheevosException $e ) {
foreach ( self::$increments as $userId => $increment ) {
CheevosIncrementJob::queue( $increment );
unset( self::$increments[$userId] );
}
self::$shutdownRan = true;

$jobs = [];
foreach ( self::$increments as $userId => $increment ) {
$jobs[] = CheevosIncrementJob::newSpecification( $increment );
unset( self::$increments[$userId] );
}

$this->jobQueueGroup->push( $jobs );
}

public function getUrlOnCheevosCentralWiki( LinkTarget $target ): string {
Expand Down
7 changes: 4 additions & 3 deletions src/Job/CheevosIncrementJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@

use Cheevos\AchievementService;
use Cheevos\CheevosAchievement;
use IJobSpecification;
use Job;
use JobSpecification;
use MediaWiki\MediaWikiServices;

class CheevosIncrementJob extends Job {
private const COMMAND = 'Cheevos\Job\CheevosIncrementJob';

public static function queue( array $parameters = [] ): void {
$job = new self( self::COMMAND, $parameters );
MediaWikiServices::getInstance()->getJobQueueGroup()->push( $job );
public static function newSpecification( array $increment ): IJobSpecification {
return new JobSpecification( self::COMMAND, $increment );
}

/** @inheritDoc */
Expand Down
Loading