-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron.py
executable file
·119 lines (88 loc) · 2.35 KB
/
cron.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env python
import os
import django
import time
import argparse
import sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "okerr.settings")
#from django.conf import settings
django.setup()
from myauth.models import SignupRequest
from okerrui.models import (
LogRecord,
Membership,
Policy,
ProjectInvite,
Project,
IChange,
Indicator,
Profile,
Throttle,
DynDNSRecord,
BonusActivation,
UpdateLog
)
from moveauth.models import MoveAuthTicket
# from okerrui.bonuscode import BonusActivation
# from transaction.models import TransactionServer
from okerrui.impex import Impex
lastcron = 0
cronperiod = 30
classes = [
Membership,
Indicator,
ProjectInvite,
Project,
LogRecord,
Policy,
SignupRequest,
IChange,
BonusActivation,
# TransactionServer,
MoveAuthTicket,
Impex,
Profile,
Throttle,
DynDNSRecord,
UpdateLog
]
class nocron:
def info(self, msg):
self.out(msg)
def debug(self, msg):
self.out(msg)
def out(self,msg):
print(msg)
def clscron(cls,log):
mname = cls.__name__
s = time.time()
r = cls.cron()
log.debug('cronmodel {} {:.2f}: {}'.format(mname, time.time() - s, r))
def cron(log=None, models=None):
global lastcron, cronperiod
if time.time() <= lastcron + cronperiod:
# too early
return
if log is None:
log = nocron()
started = time.time()
# log.debug("cron started {:.2f}".format(started))
for cls in classes:
if not models or cls.__name__ in models:
clscron(cls, log)
lastcron = time.time()
log.info("cron took {:.2f}".format(lastcron - started))
if __name__ == '__main__':
django.setup()
parser = argparse.ArgumentParser(description='standalone okerr cron')
parser.add_argument('--loop',action='store_true', default=False, help='run forever in loop')
parser.add_argument('-m', '--model', default=[], action='append', help='process only this model')
parser.add_argument('-p', '--period', type=int, default=30, help='period (in seconds, def: 30)')
args = parser.parse_args()
cronperiod = args.period
while True:
cron(models = args.model)
if not args.loop:
# not loop
sys.exit(0)
time.sleep(1)