RunJobs.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. const MWBot = require( 'mwbot' ),
  2. Page = require( 'wdio-mediawiki/Page' ),
  3. FRONTPAGE_REQUESTS_MAX_RUNS = 10; // (arbitrary) safe-guard against endless execution
  4. /**
  5. * Trigger the execution of jobs
  6. *
  7. * @see https://www.mediawiki.org/wiki/Manual:Job_queue/For_developers#Execution_of_jobs
  8. *
  9. * Use RunJobs.run() to ensure that jobs are executed before making assertions that depend on it.
  10. *
  11. * Systems that are selenium-tested are usually provisioned for that purpose, see no organic
  12. * traffic, consequently typical post-send job queue processing rarely happens. Additionally,
  13. * test set-up is often done through the API, requests to which do not trigger job queue
  14. * processing at all.
  15. *
  16. * This can lead to an accumulation of unprocessed jobs, which in turn would render certain
  17. * assertions impossible - e.g. checking a page is listed on Special:RecentChanges right
  18. * after creating it.
  19. *
  20. * This class will try to trigger job execution through
  21. * repeated blunt requests against the wiki's home page to trigger them at a rate
  22. * of $wgJobRunRate per request.
  23. */
  24. class RunJobs {
  25. static run() {
  26. browser.call( () => {
  27. return this.runThroughFrontPageRequests();
  28. } );
  29. }
  30. static getJobCount() {
  31. let bot = new MWBot( {
  32. apiUrl: `${browser.options.baseUrl}/api.php`
  33. } );
  34. return new Promise( ( resolve ) => {
  35. return bot.request( {
  36. action: 'query',
  37. meta: 'siteinfo',
  38. siprop: 'statistics'
  39. } ).then( ( response ) => {
  40. resolve( response.query.statistics.jobs );
  41. } );
  42. } );
  43. }
  44. static runThroughFrontPageRequests( runCount = 1 ) {
  45. let page = new Page();
  46. this.log( `through requests to the front page (run ${runCount}).` );
  47. page.openTitle( '' );
  48. return this.getJobCount().then( ( jobCount ) => {
  49. if ( jobCount === 0 ) {
  50. this.log( 'found no more queued jobs.' );
  51. return;
  52. }
  53. this.log( `detected ${jobCount} more queued job(s).` );
  54. if ( runCount >= FRONTPAGE_REQUESTS_MAX_RUNS ) {
  55. this.log( 'stopping requests to the front page due to reached limit.' );
  56. return;
  57. }
  58. return this.runThroughFrontPageRequests( ++runCount );
  59. } );
  60. }
  61. static log( message ) {
  62. process.stdout.write( `RunJobs ${message}\n` );
  63. }
  64. }
  65. module.exports = RunJobs;