Skip to content

Commit

Permalink
prevent idling when downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
maple3142 committed Feb 7, 2020
1 parent 3a0d4a4 commit 4646898
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
3 changes: 3 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
"repository": "https://github.com/maple3142/heroku-aria2c",
"keywords": ["aria2c"],
"env": {
"HEROKU_APP_NAME": {
"description": "This should be the same as \"App name\" above."
},
"ARIA2C_SECRET": {
"description": "Aria2c's secret",
"value": "DEFAULT_SECRET"
Expand Down
32 changes: 29 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ const express = require('express')
const request = require('request')
const httpsrv = require('httpsrv')
const fs = require('fs')
const ENCODED_SECRET = Buffer.from(
/rpc-secret=(.*)/.exec(fs.readFileSync('aria2c.conf', 'utf-8'))[1]
).toString('base64')
const SECRET = /rpc-secret=(.*)/.exec(
fs.readFileSync('aria2c.conf', 'utf-8')
)[1]
const ENCODED_SECRET = Buffer.from(SECRET).toString('base64')

const PORT = process.env.PORT || 1234
const app = express()
Expand Down Expand Up @@ -42,3 +43,28 @@ app.get('/', (req, res) => {
`)
})
server.listen(PORT, () => console.log(`Listening on http://localhost:${PORT}`))

const APP_URL = `https://${process.env.HEROKU_APP_NAME}.herokuapp.com`
const preventIdling = () => {
request.post(
'http://localhost:6800/jsonrpc',
{
json: {
jsonrpc: '2.0',
method: 'aria2.getGlobalStat',
id: 'preventIdling',
params: [`token:${SECRET}`]
}
},
(err, resp, body) => {
console.log('preventIdling: getGlobalStat response', body)
const { numActive, numWaiting } = body.result
if (parseInt(numActive) + parseInt(numWaiting) > 0) {
console.log('preventIdling: make request to prevent idling')
request(APP_URL)
}
}
)
setTimeout(preventIdling, 15 * 60 * 1000) // 15 min
}
preventIdling()

0 comments on commit 4646898

Please sign in to comment.