Skip to content

Commit

Permalink
Replace oneshot_bulk_delete with more capable oneshot_delete
Browse files Browse the repository at this point in the history
  • Loading branch information
bmeagherix committed Sep 3, 2024
1 parent 956b96d commit 8ba204a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 46 deletions.
59 changes: 14 additions & 45 deletions src/middlewared/middlewared/plugins/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
ProThreadedAlertService,
)
from middlewared.alert.base import UnavailableException, AlertService as _AlertService
from middlewared.schema import accepts, Any, Bool, Datetime, Dict, Int, List, Patch, returns, Ref, Str
from middlewared.schema import accepts, Any, Bool, Datetime, Dict, Int, List, OROperator, Patch, returns, Ref, Str
from middlewared.service import (
ConfigService, CRUDService, Service, ValidationErrors,
job, periodic, private,
Expand Down Expand Up @@ -931,61 +931,30 @@ async def oneshot_create(self, job, klass, args):

await self.middleware.call("alert.send_alerts")

@private
@accepts(Str("klass"), Any("query", null=True, default=None))
@job(lock="process_alerts", transient=True)
async def oneshot_delete(self, job, klass, query):
"""
Deletes one-shot alerts of specified `klass`, passing `query` to `klass.delete` method.
It's not an error if no alerts matching delete `query` exist.
:param klass: one-shot alert class name (without the `AlertClass` suffix).
:param query: `query` that will be passed to `klass.delete` method.
"""

try:
klass = AlertClass.class_by_name[klass]
except KeyError:
raise CallError(f"Invalid alert source: {klass!r}")

if not issubclass(klass, OneShotAlertClass):
raise CallError(f"Alert class {klass!r} is not a one-shot alert source")

related_alerts, unrelated_alerts = bisect(lambda a: (a.node, a.klass) == (self.node, klass),
self.alerts)
left_alerts = await klass(self.middleware).delete(related_alerts, query)
deleted = False
for deleted_alert in related_alerts:
if deleted_alert not in left_alerts:
self.alerts.remove(deleted_alert)
deleted = True

if deleted:
# We need to flush alerts to the database immediately after deleting oneshot alerts.
# Some oneshot alerts can only de deleted programmatically (i.e. cloud sync oneshot alerts are deleted
# when deleting cloud sync task). If we delete a cloud sync task and then reboot the system abruptly,
# the alerts won't be flushed to the database and on next boot an alert for nonexisting cloud sync task
# will appear, and it won't be deletable.
await self.middleware.call("alert.flush_alerts")

await self.middleware.call("alert.send_alerts")

@private
@accepts(
List('klasses', items=[Str('klass')], required=True),
OROperator(
Str("klass"),
List('klass', items=[Str('klassname')], default=None),
),
Any("query", null=True, default=None))
@job(lock="process_alerts", transient=True)
async def oneshot_bulk_delete(self, job, klasses, query):
async def oneshot_delete(self, job, klass, query):
"""
Deletes one-shot alerts of specified `klasses`, passing `query` to `klass.delete` method.
Deletes one-shot alerts of specified `klass` or klasses, passing `query`
to `klass.delete` method.
It's not an error if no alerts matching delete `query` exist.
:param klasses: list of one-shot alert class names (without the `AlertClass` suffix).
:param klass: either one-shot alert class name (without the `AlertClass` suffix), or list thereof.
:param query: `query` that will be passed to `klass.delete` method.
"""

if isinstance(klass, list):
klasses = klass
else:
klasses = [klass]

deleted = False
for klassname in klasses:
try:
Expand Down
2 changes: 1 addition & 1 deletion src/middlewared/middlewared/plugins/ups.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ async def alerts_mapping(self):
@private
async def dismiss_alerts(self):
alerts = list((await self.alerts_mapping()).values())
await self.middleware.call('alert.oneshot_bulk_delete', alerts)
await self.middleware.call('alert.oneshot_delete', alerts)

@private
@accepts(
Expand Down

0 comments on commit 8ba204a

Please sign in to comment.