How can I make the @timer(interval=30) to be configurable via config or environment variable?
at runtime? say, i want to change the timer value to be 600 later by just uploading one of the environment variables, is it possible?
This is possible in Nameko 3 with the addition of the global config object. You could do simply:
from nameko import config
class Service:
name = "timer"
@timer(interval=config.get('TIMER_INTERVAL'))
def ping(self):
pass
See the PR here for the implementation.
Nameko 3 is available as a pre-release. pip install --pre nameko
to get it.
Yes, the globally available config object is real game changer.
Not only one can have dynamic parameters of services, but services can be even dynamically generated from iterables in the config object.
We are going to use it in use case, where we have to monitor bunch of different urls in different intervals and store fetched content in S3 for other processing.
Sorry for digging this out but if I’m not mistaken, there’s an easy solution for Nameko v2 users.
In case, anyone is interested:
from nameko.timer import Timer
class MyTimer(Timer):
def __init__(self, eager=False, **kwargs):
super().__init__(0, eager, **kwargs)
def start(self):
self.interval = self.container.config.get('INTERVAL')
super().start()
mytimer = MyTimer.decorator
class Service:
name ="service"
@mytimer
def ping(self):
print("pong")
J.