Coverage for watcher/common/scheduling.py: 89%

31 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2025-06-17 12:22 +0000

1# -*- encoding: utf-8 -*- 

2# Copyright (c) 2016 b<>com 

3# 

4# Authors: Vincent FRANCOISE <vincent.francoise@b-com.com> 

5# 

6# Licensed under the Apache License, Version 2.0 (the "License"); 

7# you may not use this file except in compliance with the License. 

8# You may obtain a copy of the License at 

9# 

10# http://www.apache.org/licenses/LICENSE-2.0 

11# 

12# Unless required by applicable law or agreed to in writing, software 

13# distributed under the License is distributed on an "AS IS" BASIS, 

14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 

15# implied. 

16# See the License for the specific language governing permissions and 

17# limitations under the License. 

18 

19import eventlet 

20 

21from apscheduler import events 

22from apscheduler.executors import pool as pool_executor 

23from apscheduler.schedulers import background 

24 

25import futurist 

26 

27from oslo_service import service 

28 

29from watcher import eventlet as eventlet_helper 

30 

31job_events = events 

32 

33 

34class GreenThreadPoolExecutor(pool_executor.BasePoolExecutor): 

35 """Green thread pool 

36 

37 An executor that runs jobs in a green thread pool. 

38 Plugin alias: ``threadpool`` 

39 :param max_workers: the maximum number of spawned threads. 

40 """ 

41 

42 def __init__(self, max_workers=10): 

43 pool = futurist.GreenThreadPoolExecutor(int(max_workers)) 

44 super(GreenThreadPoolExecutor, self).__init__(pool) 

45 

46 

47executors = { 

48 'default': GreenThreadPoolExecutor(), 

49} 

50 

51 

52class BackgroundSchedulerService( 

53 service.ServiceBase, background.BackgroundScheduler): 

54 def __init__(self, gconfig=None, **options): 

55 self.should_patch = eventlet_helper.is_patched() 

56 if options is None: 56 ↛ 57line 56 didn't jump to line 57 because the condition on line 56 was never true

57 options = {'executors': executors} 

58 else: 

59 if 'executors' not in options.keys(): 59 ↛ 61line 59 didn't jump to line 61 because the condition on line 59 was always true

60 options['executors'] = executors 

61 super().__init__(gconfig or {}, **options) 

62 

63 def _main_loop(self): 

64 if self.should_patch: 64 ↛ 70line 64 didn't jump to line 70 because the condition on line 64 was always true

65 # NOTE(sean-k-mooney): is_patched and monkey_patch form 

66 # watcher.eventlet check a non thread local variable to early out 

67 # as we do not use eventlet_helper.patch() here to ensure 

68 # eventlet.monkey_patch() is actually called. 

69 eventlet.monkey_patch() 

70 super()._main_loop() 

71 

72 def start(self): 

73 """Start service.""" 

74 background.BackgroundScheduler.start(self) 

75 

76 def stop(self): 

77 """Stop service.""" 

78 self.shutdown() 

79 

80 def wait(self): 

81 """Wait for service to complete.""" 

82 

83 def reset(self): 

84 """Reset service. 

85 

86 Called in case service running in daemon mode receives SIGHUP. 

87 """