-
Notifications
You must be signed in to change notification settings - Fork 189
Description
An issue by arthurh4 at 2019-12-15 00:10:09+00:00
Original URL: zalando-incubator/kopf#271
Hello, very nice project ! I would like to run a function to run to some checks based on a timer (eg: every 30 seconds) is there any existing handler I could use ? Thanks
Commented by nolar at 2019-12-16 09:37:57+00:00
Hello. Thanks.
I'm currently working on this for #19, #150 and related issues, which will make it possible and easier. A draft is already functional, but I was slightly "distracted" by sickness.
Meanwhile, you can use this pattern:
import asyncio
import kopf
@kopf.on.create('zalando.org', 'v1', 'kopfexamples')
@kopf.on.resume('zalando.org', 'v1', 'kopfexamples')
def spawn_the_monitor(namespace, name, memo, logger, **kwargs):
if not hasattr(memo, 'monitor'):
memo.monitor = asyncio.create_task(monitor_me(namespace, name, logger))
@kopf.on.delete('zalando.org', 'v1', 'kopfexamples')
def stop_the_monitor(memo, **kwargs):
if hasattr(memo, 'monitor'):
memo.monitor.cancel()
async def monitor_me(namespace, name, logger):
while True:
logger.info('Checking...')
# check it here (BEWARE: sync calls block the whole operator)
await asyncio.sleep(30)The same would be with the threading.Thread() and a sync-function, except that the thread is harder to terminate — needs an externally provided event-flag to indicate that it should stop (few more lines to the this example).
Commented by nolar at 2020-04-01 11:53:18+00:00
Daemons and timers are added in kopf>=0.27rc1.
Release notes:
Docs:
Beware: the changes are quite massive, and the daemons/timers are a new feature, they are not tested thoroughly yet, can behave weird. Either test carefully in an isolated cluster/namespace, or wait for some time until the feature is tested by others (e.g. by us).
In your case, it would be:
import kopf
@kopf.timer(..., interval=30)
def fn(**kwargs):
passFor now, I close this issue as it is presumably solved. Any bugs or misbehaviour or misunderstanding should be reported separately.