annotate background_loop.py @ 14:64eddfd8b17c 1.3.0

release 1.3.0
author drewp@bigasterisk.com
date Sun, 24 Apr 2022 15:08:00 -0700
parents 263ccb9e00df
children a1ed58edfccc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
1 # dev copy
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
2 import asyncio
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
3 import logging
0
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
4 import time
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
5 import traceback
9
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
6 from typing import Awaitable, Callable
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
7
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
8 from prometheus_client import Gauge, Summary
0
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
9
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
10 log = logging.getLogger()
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
11
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
12
9
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
13 # throw this away (when net_routes is rewritten)
0
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
14 def loop_forever_sync(func, sleep_period, up_metric):
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
15 first_run = True
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
16 while True:
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
17 try:
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
18 func(first_run)
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
19 up_metric.set(1)
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
20 first_run = False
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
21 except Exception as ex:
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
22 log.error(ex)
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
23 traceback.print_exc()
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
24 up_metric.set(0)
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
25 time.sleep(sleep_period)
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
26
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
27
9
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
28 async def loop_forever(
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
29 func: Callable[[bool], Awaitable[None]],
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
30 sleep_period: float,
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
31 up_metric: Gauge,
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
32 call_metric: Summary,
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
33 ):
0
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
34 """
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
35 sleep_period is the sleep time after however long func takes to run
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
36 """
9
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
37 @call_metric.time()
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
38 async def call_func(first_run):
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
39 if asyncio.iscoroutinefunction(func):
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
40 await func(first_run)
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
41 else:
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
42 func(first_run)
0
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
43
9
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
44 first_run = True
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
45 while True:
0
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
46 try:
9
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
47 await call_func(first_run)
0
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
48 up_metric.set(1)
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
49 first_run = False
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
50 except Exception as ex:
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
51 log.error(ex)
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
52 traceback.print_exc()
a340a9bdaedb factor this out from wherever it was
drewp@bigasterisk.com
parents:
diff changeset
53 up_metric.set(0)
9
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
54 # todo: something that reveals error ratio
263ccb9e00df port to asyncio and pdm
drewp@bigasterisk.com
parents: 0
diff changeset
55 await asyncio.sleep(sleep_period)