Wei-Yu Chen | 49950b9 | 2021-11-08 19:19:18 +0800 | [diff] [blame^] | 1 | """ |
| 2 | Copyright 2020 The Magma Authors. |
| 3 | |
| 4 | This source code is licensed under the BSD-style license found in the |
| 5 | LICENSE file in the root directory of this source tree. |
| 6 | |
| 7 | Unless required by applicable law or agreed to in writing, software |
| 8 | distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | See the License for the specific language governing permissions and |
| 11 | limitations under the License. |
| 12 | """ |
| 13 | |
| 14 | from common.redis.client import get_default_client |
| 15 | from common.redis.containers import RedisFlatDict |
| 16 | from common.redis.serializers import ( |
| 17 | RedisSerde, |
| 18 | get_proto_deserializer, |
| 19 | get_proto_serializer, |
| 20 | ) |
| 21 | from orc8r.protos.service_status_pb2 import ServiceExitStatus |
| 22 | |
| 23 | |
| 24 | class ServiceStateWrapper: |
| 25 | """ |
| 26 | Class wraps ServiceState interactions with redis |
| 27 | """ |
| 28 | |
| 29 | # Unique typename for Redis key |
| 30 | REDIS_VALUE_TYPE = "systemd_status" |
| 31 | |
| 32 | def __init__(self): |
| 33 | serde = RedisSerde( |
| 34 | self.REDIS_VALUE_TYPE, |
| 35 | get_proto_serializer(), |
| 36 | get_proto_deserializer(ServiceExitStatus), |
| 37 | ) |
| 38 | self._flat_dict = RedisFlatDict(get_default_client(), serde) |
| 39 | |
| 40 | def update_service_status( |
| 41 | self, service_name: str, |
| 42 | service_status: ServiceExitStatus, |
| 43 | ) -> None: |
| 44 | """ |
| 45 | Update the service exit status for a given service |
| 46 | """ |
| 47 | |
| 48 | if service_name in self._flat_dict: |
| 49 | current_service_status = self._flat_dict[service_name] |
| 50 | else: |
| 51 | current_service_status = ServiceExitStatus() |
| 52 | |
| 53 | if service_status.latest_service_result == \ |
| 54 | ServiceExitStatus.ServiceResult.Value("SUCCESS"): |
| 55 | service_status.num_clean_exits = \ |
| 56 | current_service_status.num_clean_exits + 1 |
| 57 | service_status.num_fail_exits = \ |
| 58 | current_service_status.num_fail_exits |
| 59 | else: |
| 60 | service_status.num_fail_exits = \ |
| 61 | current_service_status.num_fail_exits + 1 |
| 62 | service_status.num_clean_exits = \ |
| 63 | current_service_status.num_clean_exits |
| 64 | self._flat_dict[service_name] = service_status |
| 65 | |
| 66 | def get_service_status(self, service_name: str) -> ServiceExitStatus: |
| 67 | """ |
| 68 | Get the service status protobuf for a given service |
| 69 | @returns ServiceStatus protobuf object |
| 70 | """ |
| 71 | return self._flat_dict[service_name] |
| 72 | |
| 73 | def get_all_services_status(self) -> [str, ServiceExitStatus]: |
| 74 | """ |
| 75 | Get a dict of service name to service status |
| 76 | @return dict of service_name to service map |
| 77 | """ |
| 78 | service_status = {} |
| 79 | for k, v in self._flat_dict.items(): |
| 80 | service_status[k] = v |
| 81 | return service_status |
| 82 | |
| 83 | def cleanup_service_status(self) -> None: |
| 84 | """ |
| 85 | Cleanup service status for all services in redis, mostly using for |
| 86 | testing |
| 87 | """ |
| 88 | self._flat_dict.clear() |