Merge "Implement delayed echo notifications"

This commit is contained in:
jenkins-bot 2020-10-28 17:52:37 +00:00 committed by Gerrit Code Review
commit ba63c30715
4 changed files with 92 additions and 7 deletions

View file

@ -215,19 +215,39 @@ class EchoNotificationController {
) );
}
/**
* Helper function to extract event task params
* @param EchoEvent $event
* @return array Event params
*/
public static function getEventParams( EchoEvent $event ) {
$delay = $event->getExtraParam( 'delay' );
$rootJobSignature = $event->getExtraParam( 'rootJobSignature' );
$rootJobTimestamp = $event->getExtraParam( 'rootJobTimestamp' );
return [ 'eventId' => $event->getId() ]
+ ( $delay ? [ 'jobReleaseTimestamp' => (int)wfTimestamp() + $delay ] : [] )
+ ( $rootJobSignature ? [ 'rootJobSignature' => $rootJobSignature ] : [] )
+ ( $rootJobTimestamp ? [ 'rootJobTimestamp' => $rootJobTimestamp ] : [] );
}
/**
* Push $event onto the mediawiki job queue
*
* @param EchoEvent $event
*/
public static function enqueueEvent( EchoEvent $event ) {
$queue = JobQueueGroup::singleton();
$job = new EchoNotificationJob(
$event->getTitle() ?: Title::newMainPage(),
[
'eventId' => $event->getId(),
]
$event->getTitle() ?: Title::newMainPage(), self::getEventParams( $event )
);
JobQueueGroup::singleton()->push( $job );
$queue->push( $job );
if ( $job->hasRootJobParams() ) {
$queue->deduplicateRootJob( $job );
}
}
/**

View file

@ -3,7 +3,8 @@
class EchoNotificationJob extends Job {
public function __construct( Title $title, array $params ) {
parent::__construct( 'EchoNotificationJob', $title, $params );
$command = isset( $params['jobReleaseTimestamp'] ) ? 'DelayedEchoNotificationJob' : 'EchoNotificationJob';
parent::__construct( $command, $title, $params );
}
public function run() {

View file

@ -101,7 +101,19 @@ class EchoEvent extends EchoAbstractEntity implements Bundleable {
* variant: A variant of the type;
* agent: The user who caused the event;
* title: The page on which the event was triggered;
* extra: Event-specific extra information (e.g. post content)
* extra: Event-specific extra information (e.g. post content, delay time, root job params).
*
* Delayed jobs extra params:
* delay: Amount of time in seconds for the notification to be delayed
*
* Job deduplication extra params:
* rootJobSignature: The sha1 signature of the job
* rootJobTimestamp: The timestamp when the job gets submitted
*
* For example to enqueue a new `example` root job or make a parent job
* no-op when submitting a new notification you need to pass this extra params:
*
* [ 'extra' => Job::newRootJobParams('example') ]
*
* @throws MWException
* @return EchoEvent|false False if aborted via hook or Echo DB is read-only

View file

@ -261,4 +261,56 @@ class NotificationControllerTest extends MediaWikiTestCase {
$result = EchoNotificationController::getEventNotifyTypes( $type );
$this->assertEquals( $expect, $result, $message );
}
public function testEnqueueEvent() {
$event = $this->getMockBuilder( EchoEvent::class )
->disableOriginalConstructor()
->getMock();
$event->expects( $this->any() )
->method( 'getExtraParam' )
->will( $this->returnValue( null ) );
$event->expects( $this->exactly( 1 ) )
->method( 'getTitle' )
->will( $this->returnValue( Title::newFromText( 'test-title' ) ) );
$event->expects( $this->exactly( 1 ) )
->method( 'getId' )
->will( $this->returnValue( 42 ) );
EchoNotificationController::enqueueEvent( $event );
$queues = JobQueueGroup::singleton()->getQueuesWithJobs();
$this->assertCount( 1, $queues );
$this->assertEquals( 'EchoNotificationJob', $queues[0] );
$job = JobQueueGroup::singleton()->pop( 'EchoNotificationJob' );
$this->assertEquals( 'Test-title', $job->params[ 'title' ] );
$this->assertEquals( 42, $job->params[ 'eventId' ] );
}
public function testEventParams() {
$rootJobTimestamp = wfTimestamp();
MWTimestamp::setFakeTime( 0 );
$event = $this->getMockBuilder( EchoEvent::class )
->disableOriginalConstructor()
->getMock();
$event->expects( $this->any() )
->method( 'getExtraParam' )
->will( $this->returnValueMap(
[
[ 'delay', null, 10 ],
[ 'rootJobSignature', null, 'test-signature' ],
[ 'rootJobTimestamp', null, $rootJobTimestamp ]
]
) );
$event->expects( $this->exactly( 1 ) )
->method( 'getId' )
->will( $this->returnValue( 42 ) );
$params = EchoNotificationController::getEventParams( $event );
$expectedParams = [
'eventId' => 42,
'rootJobSignature' => 'test-signature',
'rootJobTimestamp' => $rootJobTimestamp,
'jobReleaseTimestamp' => 10
];
$this->assertArrayEquals( $expectedParams, $params );
}
}