Skip to content

Commit

Permalink
Add events for various operations
Browse files Browse the repository at this point in the history
  • Loading branch information
z0z0r4 committed Nov 30, 2024
1 parent ae5b00a commit 0e7a67f
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 2 deletions.
1 change: 1 addition & 0 deletions prime_backup/constants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import uuid


# backup related
BACKUP_META_FILE_NAME = '.prime_backup.meta.json'

Expand Down
52 changes: 51 additions & 1 deletion prime_backup/mcdr/command/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@
from prime_backup.mcdr.task.general.show_help_task import ShowHelpTask
from prime_backup.mcdr.task.general.show_welcome_task import ShowWelcomeTask
from prime_backup.mcdr.task_manager import TaskManager
from prime_backup.mcdr.events import (
TRIGGER_BACKUP_EVENT,
TRIGGER_RESTORE_EVENT,
TRIGGER_DELETE_EVENT,
TRIGGER_IMPORT_EVENT,
TRIGGER_EXPORT_EVENT,
)
from prime_backup.types.backup_filter import BackupFilter
from prime_backup.types.backup_tags import BackupTagName
from prime_backup.types.hash_method import HashMethod
Expand All @@ -45,7 +52,6 @@
from prime_backup.utils.mcdr_utils import tr, reply_message, mkcmd
from prime_backup.utils.waitable_value import WaitableValue


class CommandManager:
def __init__(self, server: PluginServerInterface, task_manager: TaskManager, crontab_manager: CrontabManager):
self.server = server
Expand Down Expand Up @@ -424,3 +430,47 @@ def make_tag_cmd() -> Literal:
# --------------- register ---------------

self.server.register_command(root)

def register_event_listeners(self, server: PluginServerInterface):
server.register_event_listener(
TRIGGER_BACKUP_EVENT,
lambda svr, source, comment, operator: self.task_manager.add_task(CreateBackupTask(source, comment=comment, operator=operator))
)
server.register_event_listener(
TRIGGER_RESTORE_EVENT,
lambda svr, source, backup_id, needs_confirm=False, fail_soft=False, verify_blob=True: self.task_manager.add_task(
RestoreBackupTask(source, backup_id, needs_confirm=needs_confirm, fail_soft=fail_soft, verify_blob=verify_blob)
)
)
server.register_event_listener(
TRIGGER_DELETE_EVENT,
lambda svr, source, backup_ids: self.task_manager.add_task(
DeleteBackupTask(source, backup_ids)
),
)
server.register_event_listener(
TRIGGER_IMPORT_EVENT,
lambda svr, source, file_path, backup_format, ensure_meta, meta_override: self.task_manager.add_task(
ImportBackupTask(
source,
file_path,
backup_format=backup_format,
ensure_meta=ensure_meta,
meta_override=meta_override,
)
),
)
server.register_event_listener(
TRIGGER_EXPORT_EVENT,
lambda svr, source, backup_id, export_format, fail_soft, verify_blob, overwrite_existing, create_meta: self.task_manager.add_task(
ExportBackupTask(
source,
backup_id,
export_format=export_format,
fail_soft=fail_soft,
verify_blob=verify_blob,
overwrite_existing=overwrite_existing,
create_meta=create_meta,
)
),
)
15 changes: 15 additions & 0 deletions prime_backup/mcdr/events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from mcdreforged.api.event import LiteralEvent

from prime_backup.constants import PLUGIN_ID

# events
BACKUP_DONE_EVENT = LiteralEvent('{}.backup_done'.format(PLUGIN_ID)) # -> source, backup_info
RESTORE_DONE_EVENT = LiteralEvent('{}.restore_done'.format(PLUGIN_ID)) # -> source, backup_info
DELETE_DONE_EVENT = LiteralEvent('{}.delete_done'.format(PLUGIN_ID)) # -> source, backup_info
EXPORT_DONE_EVENT = LiteralEvent('{}.export_done'.format(PLUGIN_ID)) # -> source, backup_info, file_path
IMPORT_DONE_EVENT = LiteralEvent('{}.import_done'.format(PLUGIN_ID)) # -> source, backup_info
TRIGGER_BACKUP_EVENT = LiteralEvent('{}.trigger_backup'.format(PLUGIN_ID)) # <- source, comment, operator
TRIGGER_RESTORE_EVENT = LiteralEvent('{}.trigger_restore'.format(PLUGIN_ID)) # <- source, backup_id, needs_confirm, fail_soft, verify_blob
TRIGGER_DELETE_EVENT = LiteralEvent('{}.trigger_delete'.format(PLUGIN_ID)) # <- source, [backup_id]
TRIGGER_EXPORT_EVENT = LiteralEvent('{}.trigger_export'.format(PLUGIN_ID)) # <- source, backup_id, export_format, fail_soft, verify_blob, overwrite_existing, create_meta
TRIGGER_IMPORT_EVENT = LiteralEvent('{}.trigger_import'.format(PLUGIN_ID)) # <- source, file_path, backup_format, ensure_meta, meta_override
3 changes: 3 additions & 0 deletions prime_backup/mcdr/task/backup/create_backup_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from mcdreforged.api.all import *

from prime_backup.action.create_backup_action import CreateBackupAction
from prime_backup.mcdr.events import BACKUP_DONE_EVENT
from prime_backup.mcdr.task import TaskEvent
from prime_backup.mcdr.task.basic_task import HeavyTask
from prime_backup.mcdr.text_components import TextComponents
Expand Down Expand Up @@ -66,6 +67,8 @@ def run(self):
TextComponents.backup_size(backup),
TextComponents.blob_list_summary_store_size(bls),
))

self.server.dispatch_event(BACKUP_DONE_EVENT, (self.source, backup.id))
finally:
if applied_auto_save_off and len(cmds.auto_save_on) > 0:
self.server.execute(cmds.auto_save_on)
Expand Down
3 changes: 3 additions & 0 deletions prime_backup/mcdr/task/backup/delete_backup_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from prime_backup.action.delete_backup_action import DeleteBackupAction
from prime_backup.action.get_backup_action import GetBackupAction
from prime_backup.mcdr.events import DELETE_DONE_EVENT
from prime_backup.mcdr.task.basic_task import HeavyTask
from prime_backup.mcdr.text_components import TextComponents
from prime_backup.utils import collection_utils
Expand Down Expand Up @@ -42,3 +43,5 @@ def run(self):
TextComponents.backup_id(backup_id, hover=False, click=False),
TextComponents.blob_list_summary_store_size(dr.bls),
)

self.server.dispatch_event(DELETE_DONE_EVENT, (self.source, self.backup_ids))
3 changes: 3 additions & 0 deletions prime_backup/mcdr/task/backup/export_backup_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from prime_backup.action.export_backup_action import ExportBackupToZipAction, ExportBackupToTarAction
from prime_backup.action.get_backup_action import GetBackupAction
from prime_backup.mcdr.events import EXPORT_DONE_EVENT
from prime_backup.mcdr.task.basic_task import HeavyTask
from prime_backup.mcdr.text_components import TextComponents
from prime_backup.types.standalone_backup_format import ZipFormat, StandaloneBackupFormat
Expand Down Expand Up @@ -78,3 +79,5 @@ def make_output(extension: str) -> Path:
self.reply_tr('failures', len(failures))
for line in failures.to_lines():
self.reply(line)

self.server.dispatch_event(EXPORT_DONE_EVENT, (self.source, backup.id, path.absolute().as_posix()))
3 changes: 3 additions & 0 deletions prime_backup/mcdr/task/backup/import_backup_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from mcdreforged.api.all import *

from prime_backup.action.import_backup_action import ImportBackupAction, BackupMetadataNotFound
from prime_backup.mcdr.events import IMPORT_DONE_EVENT
from prime_backup.mcdr import mcdr_globals
from prime_backup.mcdr.task.basic_task import HeavyTask
from prime_backup.mcdr.text_components import TextComponents, TextColors
Expand Down Expand Up @@ -49,3 +50,5 @@ def run(self) -> None:
self.reply_tr('backup_metadata_not_found.suggestion', name=mcdr_globals.metadata.name)
else:
self.reply_tr('done', t_fp, TextComponents.backup_id(backup))

self.server.dispatch_event(IMPORT_DONE_EVENT, (self.source, backup.id))
3 changes: 3 additions & 0 deletions prime_backup/mcdr/task/backup/restore_backup_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from prime_backup.action.export_backup_action import ExportBackupToDirectoryAction
from prime_backup.action.get_backup_action import GetBackupAction
from prime_backup.action.list_backup_action import ListBackupAction
from prime_backup.mcdr.events import RESTORE_DONE_EVENT
from prime_backup.mcdr.task.basic_task import HeavyTask
from prime_backup.mcdr.text_components import TextComponents
from prime_backup.types.backup_filter import BackupFilter
Expand Down Expand Up @@ -117,3 +118,5 @@ def run(self):
logger.info('{} restored world to backup #{} (date={}, comment={!r}), pre-restore temp backup: {}'.format(
self.source, backup.id, backup.date_str, backup.comment, pre_restore_backup_id,
))

self.server.dispatch_event(RESTORE_DONE_EVENT, (self.source, backup.id))
5 changes: 4 additions & 1 deletion prime_backup/types/backup_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ def date(self) -> datetime.datetime:
def date_str(self) -> str:
return conversion_utils.timestamp_to_local_date_str(self.timestamp_ns)

def to_dict(self) -> dict:
return dataclasses.asdict(self)

@classmethod
def of(cls, backup: schema.Backup, *, with_files: bool = False) -> 'Self':
"""
Expand All @@ -53,4 +56,4 @@ def of(cls, backup: schema.Backup, *, with_files: bool = False) -> 'Self':
raw_size=backup.file_raw_size_sum or 0,
stored_size=backup.file_stored_size_sum or 0,
files=list(map(FileInfo.of, backup.files)) if with_files else [],
)
)

0 comments on commit 0e7a67f

Please sign in to comment.