forked from colonyos/pycolonies
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdes_executor.py
More file actions
81 lines (64 loc) · 2.83 KB
/
Copy pathdes_executor.py
File metadata and controls
81 lines (64 loc) · 2.83 KB
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
from pycolonies import Crypto
from pycolonies import colonies_client
import signal
import os
def calc_ndvi(polygon, product, time):
print("Calculation NDVI for polygon", polygon, "product", product, "time", time)
return [0.1, 0.2, 0.3, 0.4, 0.5]
class PythonExecutor:
def __init__(self):
colonies, colonyname, colony_prvkey, _, _ = colonies_client()
self.colonies = colonies
self.colonyname = colonyname
self.colony_prvkey = colony_prvkey
self.executorname = "des-executor"
self.executortype = "des-executor"
crypto = Crypto()
self.executor_prvkey = crypto.prvkey()
self.executorid = crypto.id(self.executor_prvkey)
self.register()
def register(self):
executor = {
"executorname": self.executorname,
"executorid": self.executorid,
"colonyname": self.colonyname,
"executortype": self.executortype
}
try:
executor = self.colonies.add_executor(executor, self.colony_prvkey)
self.colonies.approve_executor(self.colonyname, self.executorname, self.colony_prvkey)
self.colonies.add_function(self.colonyname,
self.executorname,
"calc_ts",
self.executor_prvkey)
except Exception as err:
print(err)
os._exit(0)
print("Executor", self.executorname, "registered")
def start(self):
while (True):
try:
process = self.colonies.assign(self.colonyname, 3, self.executor_prvkey)
print("Process", process.processid, "is assigned to executor")
self.colonies.add_log(process.processid, "Calculating NDVI\n", self.executor_prvkey)
if process.spec is None or len(process.spec.kwargs) == 0:
print("invalid process")
continue
polygon = process.spec.kwargs["polygon"]
product = process.spec.kwargs["product"]
time = process.spec.kwargs["time"]
ndvi_serie = calc_ndvi(polygon, product, time)
if process.spec.funcname == "calc_ts":
self.colonies.close(process.processid, ndvi_serie, self.executor_prvkey)
except Exception as err:
pass
def unregister(self):
self.colonies.remove_executor(self.colonyname, self.executorname, self.colony_prvkey)
print("Executor", self.executorname, "unregistered")
os._exit(0)
def sigint_handler(signum, frame):
executor.unregister()
if __name__ == '__main__':
signal.signal(signal.SIGINT, sigint_handler)
executor = PythonExecutor()
executor.start()