aboutsummaryrefslogtreecommitdiff
path: root/modules/status.py
blob: 906c1ec16e1e2a48ddedb8ead196813ecc0da773 (plain)
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
70
71
import os
import sys
import re
import utils
from utils import conf

services = (
		utils.Service.config,
		utils.Service.init,
		utils.Service.clean,
		utils.Service.parse,
		)

__STATUSSTRINGS__ = {
		0: "Not running",
		1: "Starting",
		2: "Running",
		3: "Stopping",
		}

def config(conf):
	conf.statusFile = conf.folder + '/status'

def init():
	with open(conf.statusFile, 'w') as f:
		f.write(__STATUSSTRINGS__[1])

def clean():
	os.remove(conf.statusFile)

def parse(line):
	if ': Done' in line:
		__server_start__()
	elif ': Stopping the server' in line:
		__server_stop__()
	else:
		return False
	return True

def __server_start__():
	print("Server start.")
	with open(conf.statusFile, 'w') as f:
		f.write(__STATUSSTRINGS__[2] + '\n')
	pass

def __server_stop__():
	print("Server stop.")
	with open(conf.statusFile, 'w') as f:
		f.write(__STATUSSTRINGS__[3] + '\n')
	pass

#### For other modules ####
def get_status(conf):
	"""Returns server status as number.
	Requires conf (server configuration) set with identifier using utils.confset().
	Returns:
	  0 - Not running
	  1 - Starting
	  2 - Running
	  3 - Stopping
	 -1 - Unknown status
	"""
	conf.statusFile = conf.folder + '/status'
	if not os.path.exists(conf.statusFile):
		return 0
	with open(conf.statusFile, 'r') as f:
		status = f.readline().rstrip()
		for i in range(len(__STATUSSTRINGS__)):
			if __STATUSSTRINGS__[i] == status:
				return i
		return -1