Initial commit
[ta/config-manager.git] / cmframework / src / cmframework / server / cmactivator.py
1 # Copyright 2019 Nokia
2
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
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14 from Queue import Queue
15
16 from cmframework.server import cmeventletrwlock
17 from cmframework.server import cmactivatorworker
18
19
20 class CMActivator(object):
21     def __init__(self, worker_count):
22         self.works = Queue()
23         self.node_works = Queue()
24         self.handlers = []
25         self.workers = []
26         self.worker_count = worker_count
27         self.lock = cmeventletrwlock.CMEventletRWLock()
28
29     def add_handler(self, handler):
30         self.handlers.append(handler)
31
32     def get_parallel_work(self):
33         return self.node_works.get()
34
35     def get_work(self):
36         return self.works.get()
37
38     def add_work(self, work):
39         work.release()
40         if not work.get_target():
41             self.works.put(work)
42         else:
43             self.node_works.put(work)
44
45     def get_handlers(self):
46         return self.handlers
47
48     def start(self):
49         worker = cmactivatorworker.CMActivatorWorker(self, 0, self.lock)
50         worker.start()
51         self.workers.append(worker)
52
53         for i in range(1, self.worker_count+1):
54             worker = cmactivatorworker.CMActivatorWorker(self, i, self.lock, parallel=True)
55             worker.start()
56             self.workers.append(worker)