| 1 |
'use strict'; |
| 2 |
|
| 3 |
const MWBot = require( 'mwbot' ); |
| 4 |
const Page = require( './Page' ); |
| 5 |
const MAINPAGE_REQUESTS_MAX_RUNS = 10; |
| 6 |
|
| 7 |
function getJobCount() { |
| 8 |
const bot = new MWBot( { |
| 9 |
apiUrl: `${ browser.config.baseUrl }/api.php` |
| 10 |
} ); |
| 11 |
return bot.request( { |
| 12 |
action: 'query', |
| 13 |
meta: 'siteinfo', |
| 14 |
siprop: 'statistics' |
| 15 |
} ).then( ( response ) => response.query.statistics.jobs ); |
| 16 |
} |
| 17 |
|
| 18 |
function log( message ) { |
| 19 |
process.stdout.write( `RunJobs ${ message }\n` ); |
| 20 |
} |
| 21 |
|
| 22 |
function runThroughMainPageRequests( runCount = 1 ) { |
| 23 |
const page = new Page(); |
| 24 |
log( `through requests to the main page (run ${ runCount }).` ); |
| 25 |
|
| 26 |
page.openTitle( '' ); |
| 27 |
|
| 28 |
return getJobCount().then( ( jobCount ) => { |
| 29 |
if ( jobCount === 0 ) { |
| 30 |
log( 'found no more queued jobs.' ); |
| 31 |
return; |
| 32 |
} |
| 33 |
log( `detected ${ jobCount } more queued job(s).` ); |
| 34 |
if ( runCount >= MAINPAGE_REQUESTS_MAX_RUNS ) { |
| 35 |
log( 'stopping requests to the main page due to reached limit.' ); |
| 36 |
return; |
| 37 |
} |
| 38 |
return runThroughMainPageRequests( ++runCount ); |
| 39 |
} ); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
class RunJobs { |
| 62 |
|
| 63 |
static run() { |
| 64 |
browser.call( () => runThroughMainPageRequests() ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
module.exports = RunJobs; |