Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def clean(self):
"""Validate before saving to the database"""
if self.status not in BrewtilsInstance.INSTANCE_STATUSES:
raise ModelValidationError(
f"Can not save Instance {self}: Invalid status '{self.status}'"
)
def monitor(self):
"""Make sure plugins stay alive.
Iterate through all plugins, testing them one at a time.
If any of them are dead restart them, otherwise just keep chugging along.
"""
for plugin in self.registry.get_all_plugins():
if self.stopped():
break
if (
plugin.process
and plugin.process.poll() is not None
and not plugin.stopped()
):
plugin_instance = db.query_unique(Instance, id=plugin.instance.id)
plugin_status = plugin_instance.status
if plugin_status == "RUNNING":
self.logger.warning(
"It looks like plugin %s has " "unexpectedly stopped running.",
plugin.unique_name,
)
self.logger.warning(
"If this is happening often, you "
"need to talk to the plugin developer."
)
self.logger.warning("Restarting plugin: %s", plugin.unique_name)
plugin_instance.status = "DEAD"
db.update(plugin_instance)
if self.output_type not in BrewtilsCommand.OUTPUT_TYPES:
raise ModelValidationError(
f"Can not save Command {self}: Invalid output type '{self.output_type}'"
)
if len(self.parameters) != len(
set(parameter.key for parameter in self.parameters)
):
raise ModelValidationError(
f"Can not save Command {self}: Contains Parameters with duplicate keys"
)
class Instance(MongoModel, Document):
brewtils_model = brewtils.models.Instance
name = StringField(required=True, default="default")
description = StringField()
status = StringField(default="INITIALIZING")
status_info = EmbeddedDocumentField("StatusInfo", default=StatusInfo())
queue_type = StringField()
queue_info = DictField()
icon_name = StringField()
metadata = DictField()
def clean(self):
"""Validate before saving to the database"""
if self.status not in BrewtilsInstance.INSTANCE_STATUSES:
raise ModelValidationError(
f"Can not save Instance {self}: Invalid status '{self.status}'"
if len(self.parameters) != len(
set(parameter.key for parameter in self.parameters)
):
raise ModelValidationError(
f"Can not save Command {self}: Contains Parameters with duplicate keys"
)
class StatusInfo:
brewtils_model = brewtils.models.StatusInfo
heartbeat = FieldBase(field_type="DATE")
class Instance:
brewtils_model = brewtils.models.Instance
name = FieldBase(field_type="STRING", required=True, default="default")
description = FieldBase(field_type="STRING")
status = FieldBase(field_type="STRING", default="INITIALIZING")
status_info = FieldBase(
field_type="StatusInfo", default=StatusInfo()
)
queue_type = FieldBase(field_type="STRING")
queue_info = FieldBase(field_type="JSON")
icon_name = FieldBase(field_type="STRING")
metadata = FieldBase(field_type="JSON")
def clean(self):
"""Validate before saving to the database"""
if self.status not in BrewtilsInstance.INSTANCE_STATUSES:
def clean(self):
"""Validate before saving to the database"""
if self.status not in BrewtilsInstance.INSTANCE_STATUSES:
raise ModelValidationError(
f"Can not save Instance {self}: Invalid status '{self.status}'"
)
def create_system(system: System) -> System:
"""Create a new System
Args:
system: The System to create
Returns:
The created System
"""
# Assign a default 'main' instance if there aren't any instances and there can
# only be one
if not system.instances or len(system.instances) == 0:
if system.max_instances is None or system.max_instances == 1:
system.instances = [Instance(name="default")]
system.max_instances = 1
else:
raise ModelValidationError(
f"Could not create system {system.name}-{system.version}: Systems with "
f"max_instances > 1 must also define their instances"
)
else:
if not system.max_instances:
system.max_instances = len(system.instances)
system = db.create(system)
return system
def check_status(self):
"""Update instance status if necessary"""
for instance in db.query(Instance):
if self.stopped():
break
if "heartbeat" in instance.status_info:
last_heartbeat = instance.status_info["heartbeat"]
if last_heartbeat:
if (
instance.status == "RUNNING"
and datetime.utcnow() - last_heartbeat >= self.timeout
):
instance.status = "UNRESPONSIVE"
db.update(instance)
elif (
instance.status
in ["UNRESPONSIVE", "STARTING", "INITIALIZING", "UNKNOWN"]
logger = logging.getLogger(__name__)
ModelType = Union[
Type[brewtils.models.Command],
Type[brewtils.models.Instance],
Type[brewtils.models.Job],
Type[brewtils.models.Request],
Type[brewtils.models.RequestTemplate],
Type[brewtils.models.System],
Type[brewtils.models.Garden],
]
ModelItem = Union[
brewtils.models.Command,
brewtils.models.Instance,
brewtils.models.Job,
brewtils.models.Request,
brewtils.models.RequestTemplate,
brewtils.models.System,
brewtils.models.Garden,
]
_model_map = beer_garden.db.sql.models.schema_mapping
engine = None
Session = None
def from_brewtils(obj: ModelItem) -> SqlModel:
"""Convert an item from its Brewtils model to its one
def get_plugin_from_instance_id(self, instance_id):
instance = db.query_unique(Instance, id=instance_id)
system = db.query_unique(System, instances__contains=instance)
unique_name = self.get_unique_name(system.name, system.version, instance.name)
return self.get_plugin(unique_name)
def initialize_instance(instance_id: str) -> Instance:
"""Initializes an instance.
This does a lot of stuff right now.
Args:
instance_id: The Instance ID
Returns:
The updated Instance
"""
instance = db.query_unique(Instance, id=instance_id)
system = db.query_unique(System, instances__contains=instance)
logger.info(
"Initializing instance %s[%s]-%s", system.name, instance.name, system.version
)
routing_words = [system.name, system.version, instance.name]
request_queue_name = get_routing_key(*routing_words)
request_queue = queue.create(
request_queue_name,
[request_queue_name],
durable=True,
arguments={"x-max-priority": 1},
)