1 # Copyright (C) 2021 Wind River Systems, Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 # from o2common.service.unit_of_work import AbstractUnitOfWork
18 from o2common.service.watcher.base import WatcherTree
20 from o2common.helper import o2logging
21 logger = o2logging.get_logger(__name__)
24 class PollWorker(object):
25 def __init__(self, interval=10, bus=None) -> None:
28 self.schedinstance = sched.scheduler(time.time, time.sleep)
29 self.schedinterval = interval
33 def set_interval(self, interval):
35 self.schedinterval = interval
37 raise Exception("Invalid interval:" + interval)
39 def add_watcher(self, watcher: WatcherTree):
40 self.watchers.append(watcher)
43 logger.debug("_repeat started")
46 for w in self.watchers:
48 # logger.debug("about to probe:"+w)
50 except Exception as ex:
51 logger.warning("Worker raises exception:" + str(ex))
55 if self._bus is not None:
56 events = self._bus.uow.collect_new_events()
58 self._bus.handle(event)
60 self.schedinstance.enter(self.schedinterval, 1, self._repeat)
62 # note the sched run will block current thread
65 logger.debug('about to start sched task')
66 self.schedinstance.enter(self.schedinterval, 1, self._repeat)
67 self.schedinstance.run()