-
Notifications
You must be signed in to change notification settings - Fork 0
/
ping.py
69 lines (58 loc) · 2.39 KB
/
ping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
################################################################################
### NZBGET API SCRIPT ###
################################################################################
# Ping script by ssnjr for heroku.
#
# Checks if server is in standby or not, and pings if it isn't in standby.
#
# Server is in standby when there are no downloads in progress (server paused
# or all jobs completed).
#
# A web dyno sleeps if it doesn't receive any traffic within 30 mins so the
# script pings every 30 minutes when the server is not in standby to prevent
# nzbget from dying during an ongoing process.
#
# To save dyno hours the script doesn't ping when server is in standby i.e.
# when it is not in use.
#
# The script can keep an instance alive for 24 hrs.
################################################################################
### NZBGET API SCRIPT ###
################################################################################
import os
import re
import sys
import requests
from time import sleep
from xmlrpc.client import ServerProxy
host = '127.0.0.1'
port = os.environ.get('PORT')
username = os.environ.get('CTRL_USERNAME')
password = os.environ.get('CTRL_PASS')
app_url = os.environ.get('APP_URL')
# Build an URL for XML-RPC requests
rpcUrl = f'http://{username}:{password}@{host}:{port}/xmlrpc'
server = ServerProxy(rpcUrl) # Create remote server object
def ping():
try:
response = requests.get(app_url)
response_time = int(response.elapsed.total_seconds()*1000)
message = f'PING: {response.url} - ' + \
f'pinged sucessfully ({response_time}ms)'
server.writelog('INFO', message)
except:
message = f'Can\'t ping "{app_url}" please ensure the app url ' + \
'is entered in correctly'
server.writelog('ERROR', message)
while True:
sleep(1798)
# Fetches bool Status for the following:
ServerStandBy = server.status()['ServerStandBy']
DownloadPaused = server.status()['DownloadPaused']
# Pings the server if server is not in standby or if download is paused:
if not ServerStandBy or DownloadPaused:
ping()
# Opposite of the above condition:
elif ServerStandBy and not DownloadPaused:
server.writelog('INFO', 'Server is in standby')