Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thread interface #1990

Merged
merged 8 commits into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions azurelinuxagent/common/interfaces.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Microsoft Azure Linux Agent
#
# Copyright 2020 Microsoft Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Requires Python 2.6+ and Openssl 1.0+
#


class ThreadHandlerInterface(object):
"""
Interface for all thread handlers created and maintained by the GuestAgent.
"""

@staticmethod
def get_thread_name():
raise NotImplementedError("get_thread_name() not implemented")

def run(self):
raise NotImplementedError("run() not implemented")

def is_alive(self):
raise NotImplementedError("is_alive() not implemented")

def start(self):
raise NotImplementedError("start() not implemented")

def stop(self):
raise NotImplementedError("stop() not implemented")
3 changes: 2 additions & 1 deletion azurelinuxagent/ga/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from azurelinuxagent.common.dhcp import get_dhcp_handler
from azurelinuxagent.common.event import add_periodic, WALAEventOperation
from azurelinuxagent.common.future import ustr
from azurelinuxagent.common.interfaces import ThreadHandlerInterface
from azurelinuxagent.common.osutil import get_osutil
from azurelinuxagent.common.protocol.util import get_protocol_util
from azurelinuxagent.common.utils.archive import StateArchiver
Expand All @@ -46,7 +47,7 @@ def get_env_handler():
return EnvHandler()


class EnvHandler(object): # pylint: disable=R0902
class EnvHandler(ThreadHandlerInterface): # pylint: disable=R0902
"""
Monitor changes to dhcp and hostname.
If dhcp client process re-start has occurred, reset routes, dhcp with fabric.
Expand Down
3 changes: 2 additions & 1 deletion azurelinuxagent/ga/extension_telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
TELEMETRY_LOG_PROVIDER_ID, add_event, WALAEventOperation, add_log_event, get_event_logger
from azurelinuxagent.common.exception import InvalidExtensionEventError
from azurelinuxagent.common.future import ustr
from azurelinuxagent.common.interfaces import ThreadHandlerInterface
from azurelinuxagent.common.telemetryevent import TelemetryEventList, TelemetryEvent, TelemetryEventParam, \
GuestAgentGenericLogsSchema
from azurelinuxagent.ga.exthandlers import HANDLER_NAME_PATTERN
Expand Down Expand Up @@ -347,7 +348,7 @@ def add_common_params_to_extension_event(event, event_time):
reporter.add_common_event_parameters(event, event_time)


class ExtensionTelemetryHandler(object):
class ExtensionTelemetryHandler(ThreadHandlerInterface):
"""
This Handler takes care of fetching the Extension Telemetry events from the {extension_events_dir} and sends it to
Kusto for advanced debuggability.
Expand Down
10 changes: 5 additions & 5 deletions azurelinuxagent/ga/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,18 @@
import threading
import uuid

from azurelinuxagent.common.utils import textutil # pylint: disable=W0611

import azurelinuxagent.common.logger as logger
import azurelinuxagent.common.utils.networkutil as networkutil
from azurelinuxagent.common.cgroupconfigurator import CGroupConfigurator
from azurelinuxagent.common.cgroupstelemetry import CGroupsTelemetry
from azurelinuxagent.common.errorstate import ErrorState
from azurelinuxagent.common.event import add_event, WALAEventOperation, report_metric, collect_events
from azurelinuxagent.common.future import ustr
from azurelinuxagent.common.interfaces import ThreadHandlerInterface
from azurelinuxagent.common.osutil import get_osutil
from azurelinuxagent.common.protocol.util import get_protocol_util
from azurelinuxagent.common.protocol.healthservice import HealthService
from azurelinuxagent.common.protocol.imds import get_imds_client
from azurelinuxagent.common.protocol.util import get_protocol_util
from azurelinuxagent.common.utils.restutil import IOErrorCounter
from azurelinuxagent.common.utils.textutil import hash_strings
from azurelinuxagent.common.version import AGENT_NAME, CURRENT_VERSION
Expand Down Expand Up @@ -157,7 +156,7 @@ def _operation_impl(self):
self.last_nic_state = nic_state


class MonitorHandler(object): # pylint: disable=R0902
class MonitorHandler(ThreadHandlerInterface): # pylint: disable=R0902
# telemetry
EVENT_COLLECTION_PERIOD = datetime.timedelta(minutes=1)
# host plugin
Expand Down Expand Up @@ -225,7 +224,8 @@ def init_imds_client(self):
def is_alive(self):
return self.event_thread is not None and self.event_thread.is_alive()

def start(self, init_data=False):
# W0221: arguments-differ - This is a special case, we need to init_data only when run first time.
larohra marked this conversation as resolved.
Show resolved Hide resolved
def start(self, init_data=False): # pylint: disable=arguments-differ
self.event_thread = threading.Thread(target=self.daemon, args=(init_data,))
self.event_thread.setDaemon(True)
self.event_thread.setName(self.get_thread_name())
Expand Down