Skip to content

Commit

Permalink
Minor improvements to tag scheduler (#3244)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvilanova authored Apr 7, 2023
1 parent 4aa29e3 commit 5bc1320
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/dispatch/tag/scheduled.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,41 @@
"""
import logging

from schedule import every
from typing import NoReturn

from dispatch.database.core import SessionLocal
from dispatch.decorators import scheduled_project_task
from dispatch.incident import service as incident_service
from dispatch.plugin import service as plugin_service
from dispatch.project.models import Project
from dispatch.scheduler import scheduler
from dispatch.incident import service as incident_service
from dispatch.tag import service as tag_service
from dispatch.tag.models import TagCreate
from dispatch.tag.recommender import build_model
from schedule import every

log = logging.getLogger(__name__)


@scheduler.add(every(1).hour, name="tag-sync")
@scheduled_project_task
def sync_tags(db_session: SessionLocal, project: Project):
def sync_tags(db_session: SessionLocal, project: Project) -> NoReturn:
"""Syncs tags from external sources."""
plugin = plugin_service.get_active_instance(
db_session=db_session, plugin_type="tag", project_id=project.id
db_session=db_session,
project_id=project.id,
plugin_type="tag",
)

if not plugin:
log.debug(f"No active plugins were found. PluginType: 'tag' ProjectId: {project.id}")
log.warning(
f"Tags not synced using external sources. No tag plugin enabled in {project.name} project."
)
return

log.debug(f"Getting tags via: {plugin.plugin.slug}")
log.debug(f"Fetching tags using {plugin.plugin.slug} plugin...")
for t in plugin.instance.get():
log.debug(f"Adding Tag. Tag: {t}")
log.debug(f"Adding tag {t}...")

# we always use the plugin project when syncing
t["tag_type"].update({"project": project})
Expand All @@ -44,7 +50,7 @@ def sync_tags(db_session: SessionLocal, project: Project):

@scheduler.add(every(1).hour, name="tag-model-builder")
@scheduled_project_task
def build_tag_models(db_session: SessionLocal, project: Project):
def build_tag_models(db_session: SessionLocal, project: Project) -> NoReturn:
"""Builds the intensive tag recommendation models."""
# incident model
incidents = incident_service.get_all(db_session=db_session, project_id=project.id).all()
Expand Down

0 comments on commit 5bc1320

Please sign in to comment.