Skip to content

Commit b24d095

Browse files
committed
⚡ Add bottleneck limiter
1 parent 3258d90 commit b24d095

7 files changed

Lines changed: 70 additions & 44 deletions

File tree

‎.env.example‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
APP_ID=
33
WEBHOOK_SECRET=development
44
PRIVATE_KEY=
5+
PULL_INTERVAL=3600
6+
MAX_CONCURRENT=10
7+
CONFIG_FILENAME=pull.yml
8+
DISABLE_DELAY=false
9+
DISABLE_STATS=false
510

611
# The ID of an OAuth App for making unauthenticated calls (Workaround)
712
CLIENT_ID=
@@ -14,7 +19,7 @@ LOG_LEVEL=debug
1419
WEBHOOK_PROXY_URL=
1520

1621
# Relative path of the webhook
17-
WEBHOOK_PATH=
22+
WEBHOOK_PATH=/webhook
1823

1924
# Log to remote syslog server
2025
SYSLOG_UDP_HOST=

‎Dockerfile‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ LABEL \
1010
maintainer="Wei He <docker@weispot.com>"
1111

1212
ENV \
13-
NODE_ENV=production \
14-
PORT=3000 \
15-
LOG_FORMAT=short \
16-
LOG_LEVEL=info \
17-
PULL_INTERVAL=3600 \
18-
WEBHOOK_PATH=/webhook \
1913
####################
2014
### Required ###
2115
####################
@@ -32,7 +26,16 @@ ENV \
3226
#SYSLOG_UDP_PORT= \
3327
#GHE_HOST= \
3428
#IGNORED_ACCOUNTS= \
35-
#DISABLE_STATS= \
29+
NODE_ENV=production \
30+
PORT=3000 \
31+
LOG_FORMAT=short \
32+
LOG_LEVEL=info \
33+
WEBHOOK_PATH=/webhook \
34+
PULL_INTERVAL=3600 \
35+
MAX_CONCURRENT=10 \
36+
CONFIG_FILENAME=pull.yml \
37+
DISABLE_DELAY=false \
38+
DISABLE_STATS=false \
3639
_=
3740

3841
WORKDIR /app
@@ -53,4 +56,4 @@ ARG VCS_REF
5356
ARG BUILD_DATE
5457
LABEL \
5558
org.label-schema.vcs-ref=$VCS_REF \
56-
org.label-schema.build-date=$BUILD_DATE
59+
org.label-schema.build-date=$BUILD_DATE

‎index.js‎

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
const createScheduler = require('probot-scheduler')
2+
const Bottleneck = require('bottleneck')
23

34
const Pull = require('./lib/pull')
45
const getConfig = require('./lib/get-config')
56
const configureLogging = require('./lib/logging')
67
const configureRoutes = require('./lib/router')
78

89
module.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)

‎lib/pull.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ module.exports = class Pull {
2424
}
2525

2626
async routineCheck () {
27-
// this.logger.debug(this.config, `[${this.config.repoPath}] Config`)
28-
this.logger.debug(`[${this.config.repoPath}] Routine Check`)
27+
this.logger.info(`[${this.config.repoPath}] Routine Check`)
28+
// this.logger.debug(`[${this.config.repoPath}] Config`, this.config)
2929

3030
for (let i = 0; i < this.config.rules.length; i++) {
3131
const rule = this.config.rules[i]

‎lib/router.js‎

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,46 @@ const getJSON = uri => requestPromise({
1616
module.exports = app => {
1717
const routes = app.route()
1818

19-
routes.get('/', (req, res) => res.redirect('https://github.com/wei/pull#readme'))
19+
routes.get('/', (req, res) => res.redirect('https://wei.github.io/pull'))
2020

2121
routes.get('/installations', async (req, res) => {
22-
const output = {
23-
installations: app.managedAccounts.length,
24-
repos: app.managedRepos.length
22+
if (req.query.key !== process.env.WEBHOOK_SECRET) {
23+
return res.status(403).end()
2524
}
26-
if (req.query.key === process.env.WEBHOOK_SECRET) {
27-
output.managedAccounts = app.managedAccounts.sort()
28-
output.managedRepos = app.managedRepos.sort()
25+
const repos = Object.assign({}, app.scheduler.repos)
26+
for (let key in repos) {
27+
if (repos[key] instanceof Date) {
28+
repos[key] = (repos[key].getTime() - new Date().getTime()) / 1000
29+
}
2930
}
30-
res.json(output)
31+
res.json({ repos, installs: reposToInstalls(repos), limiter: app.limiter.counts() })
3132
})
3233

3334
routes.get('/badge/:type', async (req, res) => {
3435
let params
3536
try {
3637
switch (req.params.type) {
3738
case 'installed':
39+
const installCount = Object.keys(reposToInstalls(app.scheduler.repos)).length
3840
params = ('plain' in req.query) ? {
3941
subject: '',
40-
status: `${app.managedAccounts.length}`,
42+
status: `${installCount}`,
4143
color: 'blue'
4244
} : {
4345
subject: 'installed',
44-
status: `${app.managedAccounts.length} times`,
46+
status: `${installCount} times`,
4547
color: 'blue'
4648
}
4749
break
4850
case 'managing':
51+
const repoCount = Object.keys(app.scheduler.repos).length
4952
params = ('plain' in req.query) ? {
5053
subject: '',
51-
status: `${app.managedRepos.length}`,
54+
status: `${repoCount}`,
5255
color: 'blue'
5356
} : {
5457
subject: 'managing',
55-
status: `${app.managedRepos.length} repos`,
58+
status: `${repoCount} repos`,
5659
color: 'blue'
5760
}
5861
break
@@ -146,4 +149,12 @@ module.exports = app => {
146149
res.status(output.statusCode).end(output.body)
147150
})
148151
})
152+
153+
function reposToInstalls (repos = {}) {
154+
const installs = {}
155+
for (const repo in repos) {
156+
installs[repo.split('/')[0]] = (installs[repo.split('/')[0]] || 0) + 1
157+
}
158+
return installs
159+
}
149160
}

‎package-lock.json‎

Lines changed: 5 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
},
1515
"dependencies": {
1616
"badgen": "^2.7.1",
17+
"bottleneck": "^2.18.0",
1718
"bunyan-syslog-udp": "^0.2.0",
1819
"joi": "^14.3.1",
1920
"js-yaml": "^3.13.0",
2021
"probot": "^9.2.0",
2122
"probot-config": "^1.0.1",
22-
"probot-scheduler": "^2.0.0-beta.1",
23+
"probot-scheduler": "wei/probot-scheduler#fixes",
2324
"request": "^2.88.0",
2425
"request-promise": "^4.2.4"
2526
},

0 commit comments

Comments
 (0)