-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvirtual_site.py
242 lines (193 loc) · 7.06 KB
/
virtual_site.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import logging
import asyncio
import click
import httpx
import arrow
log = logging.getLogger(__name__)
@click.command()
@click.option(
"--name",
help="name of this site (e.g. 'ogg')",
required=True,
default="ogg"
)
@click.option(
"--api-url",
help="OCS Portal API root URL (e.g. 'http://localhost:8000/api/'",
required=True,
)
@click.option("--api-token", help="OCS Portal API auth token", required=True)
@click.option(
"--log-level",
help="log level",
type=click.Choice(["critical", "error", "warning", "info", "debug"]),
default="info",
)
def cli(name, api_url, api_token, log_level):
"""
A virtual controller that fulfills observation requests for a site.
"""
# Just setting up logging.
logging.basicConfig(level=log_level.upper())
s = Site(name, api_url, api_token)
# We're using asyncio just to make concurrent programming a bit
# more legible. You can ignore the async/await syntax for the most part.
asyncio.run(s.run())
class Site:
name: str
# Arrow is just like a datetime object but w/ full ISO 8601
last_sync_time: arrow.Arrow
observation_tasks: list["ObservationTask"]
api_client: httpx.AsyncClient
def __init__(self, name: str, api_url: str, api_token: str):
self.name = name
# setup a HTTP client we'll use to talk with API
self.api_client = httpx.AsyncClient(
base_url=api_url,
headers={
"Authorization": f"Token {api_token}"
},
follow_redirects=True
)
# this datetime is used to keep track of when to poll for a new schedule
# set to "0" (epoch start) initially to always sync the schedule on
# start-up
self.last_sync_time = arrow.get(0)
# a list of running or scheduled observation tasks
self.observation_tasks = []
async def run(self) -> None:
"""
Run site tasks.
"""
try:
# we start polling the schedule in a coroutine
await self.poll_schedule()
except asyncio.CancelledError: # raised on CTRL-C
# gracefully shut down the HTTP client
await self.api_client.aclose()
async def poll_schedule(self) -> None:
"""
Poll the schedule every 5 seconds and schedule observations locally
whenever a new schedule is detected.
"""
while True:
log.info("checking for new schedule")
r = await self.api_client.get("/last_scheduled/")
last_scheduled_time = arrow.get(r.json()["last_schedule_time"])
if self.last_sync_time <= last_scheduled_time:
log.info("found new schedule")
await self.sync_schedule()
self.last_sync_time = last_scheduled_time
await asyncio.sleep(5)
async def sync_schedule(self) -> None:
"""
Retrieve the schedule from the API and create a ObservationTask for each
observation.
"""
log.info("synchronizing schedule")
now = arrow.utcnow()
schedule = (await self.api_client.get(
f"/schedule/",
params={
"site": self.name,
"start_after": now.isoformat(),
"start_before": now.shift(days=7).isoformat(),
"state": "PENDING",
}
)).json()
log.info("cancelling previously scheduled tasks")
for ot in self.observation_tasks:
ot.cancel()
log.info("scheduling new tasks")
observation_tasks = []
for obs in schedule["results"]:
ot = ObservationTask(spec=obs, api_client=self.api_client)
observation_tasks.append(ot)
self.observation_tasks = observation_tasks
log.info("schedule synchronized")
class ObservationTask:
"""
A ObservationTask carries out the observation on the telescope/instruments
(although in this case we just sleep).
On instantiation, it creates a asyncio.Task to run in the background.
"""
def __init__(self, spec: dict, api_client: httpx.AsyncClient):
self.spec = spec
self.api_client = api_client
self.started = False
self.log = logging.getLogger(repr(self))
self.task = asyncio.create_task(self.run())
async def run(self):
"""
Perform an "observation".
This task sits idle until the observation start time and then mocks
the observation.
"""
start_time = arrow.get(self.spec["start"])
self.log.info(
f"waiting until observation start time: "
f"{start_time} ({start_time.humanize()})"
)
await asyncio.sleep((start_time - arrow.utcnow()).total_seconds())
self.started = True
self.log.info("observation started")
for config in self.spec["request"]["configurations"]:
config_id = config["id"]
status_id = config["configuration_status"]
run_time = sum(
ic["exposure_time"] * ic["exposure_count"]
for ic in config["instrument_configs"]
)
self.log.info(
f"notify OCS that we're about to start config {config_id}"
)
r = await self.api_client.patch(
f"/configurationstatus/{status_id}/",
json={"state": "ATTEMPTED"}
)
self.log.info(f"OCS response: {r.content}")
if not r.is_success:
return
self.log.info(f"beep bop taking some pictures, wait {run_time} sec")
config_start = arrow.utcnow()
await asyncio.sleep(run_time)
config_end = arrow.utcnow()
time_completed = (config_end - config_start).total_seconds()
self.log.info(
f"notify OCS that we're done with config {config_id}"
)
r = await self.api_client.patch(
f"/configurationstatus/{status_id}/",
json={
"state": "COMPLETED",
"summary": {
"state": "COMPLETED",
"start": str(config_start),
"end": str(config_end),
"time_completed": time_completed,
"reason": "",
"events": {
"msg": "completed by virtual-site",
"events": ["payload", "can", "be whatever you like"]
}
}
}
)
self.log.info(f"OCS response: {r.content}")
if not r.is_success:
return
self.log.info("observation finished")
def cancel(self) -> None:
"""
Cancel this observation if it hasn't started yet.
"""
if not self.started:
self.log.info(
"skip cancelling because observation has already started"
)
return
self.log.info("cancelling")
self.task.cancel()
def __repr__(self):
fqn = f"{self.__module__}.{self.__class__.__qualname__}"
return f"<{fqn} id={self.spec['id']!r} name={self.spec['name']!r}>"