11const createScheduler = require ( 'probot-scheduler' )
2+ const Bottleneck = require ( 'bottleneck' )
23
34const Pull = require ( './lib/pull' )
45const getConfig = require ( './lib/get-config' )
56const configureLogging = require ( './lib/logging' )
67const configureRoutes = require ( './lib/router' )
78
89module . exports = async ( app ) => {
9- app . managedAccounts = [ ]
10- app . managedRepos = [ ]
1110 app . CONFIG_FILENAME = process . env . CONFIG_FILENAME || 'pull.yml'
1211
12+ app . limiter = new Bottleneck ( {
13+ maxConcurrent : parseInt ( process . env . MAX_CONCURRENT , 10 ) || 10 ,
14+ trackDoneStatus : false
15+ } )
16+
1317 configureLogging ( app )
1418 configureRoutes ( app )
1519
16- const scheduler = createScheduler ( app , {
20+ app . scheduler = createScheduler ( app , {
1721 delay : ! process . env . DISABLE_DELAY ,
1822 interval : ( parseInt ( process . env . PULL_INTERVAL , 10 ) || 3600 ) * 1000
1923 } )
@@ -29,6 +33,16 @@ module.exports = async (app) => {
2933 }
3034
3135 async function routineCheck ( context ) {
36+ const jobId = context . payload . repository . full_name
37+ if ( ! app . limiter . jobStatus ( jobId ) ) {
38+ await app . limiter . schedule ( {
39+ expiration : 30000 ,
40+ id : jobId
41+ } , ( ) => processRoutineCheck ( context ) )
42+ }
43+ }
44+
45+ async function processRoutineCheck ( context ) {
3246 const pull = await forRepository ( context )
3347 if ( pull ) await pull . routineCheck ( )
3448 }
@@ -49,16 +63,9 @@ module.exports = async (app) => {
4963 }
5064
5165 async function forRepository ( context ) {
52- if ( app . managedAccounts . indexOf ( context . payload . repository . owner . login ) === - 1 ) {
53- app . managedAccounts . push ( context . payload . repository . owner . login )
54- }
55- if ( app . managedRepos . indexOf ( context . payload . repository . full_name ) === - 1 ) {
56- app . managedRepos . push ( context . payload . repository . full_name )
57- }
58-
5966 if ( context . payload . repository . archived ) {
6067 app . log . debug ( `[${ context . payload . repository . full_name } ] Not an active repo, unscheduled` )
61- scheduler . stop ( context . payload . repository )
68+ app . scheduler . stop ( context . payload . repository )
6269 return null
6370 }
6471
@@ -67,7 +74,7 @@ module.exports = async (app) => {
6774 config = await getConfig . getLiveConfig ( context , app . CONFIG_FILENAME )
6875 if ( ! context . payload . repository . fork && ! config ) {
6976 app . log . debug ( `[${ context . payload . repository . full_name } ] Not a forked repo and has no pull.yml, unscheduled` )
70- scheduler . stop ( context . payload . repository )
77+ app . scheduler . stop ( context . payload . repository )
7178 return null
7279 }
7380 if ( ! config ) {
@@ -78,14 +85,14 @@ module.exports = async (app) => {
7885 app . log . warn ( e , `[${ context . payload . repository . full_name } ] Repo access failed with server error ${ e . code } ` )
7986 } else {
8087 app . log . debug ( e , `[${ context . payload . repository . full_name } ] Repo is blocked, unscheduled` )
81- scheduler . stop ( context . payload . repository )
88+ app . scheduler . stop ( context . payload . repository )
8289 }
8390 return null
8491 }
8592
8693 if ( ! config ) {
8794 app . log . debug ( `[${ context . payload . repository . full_name } ] Unable to get config, unscheduled` )
88- scheduler . stop ( context . payload . repository )
95+ app . scheduler . stop ( context . payload . repository )
8996 return null
9097 }
9198 return new Pull ( context . github , context . repo ( { logger : app . log } ) , config )
0 commit comments