Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2418 from pypeclub/feature/OP-1172_Start-timer-po…
Browse files Browse the repository at this point in the history
…st-launch-hook

TimersManager: Start timer post launch hook
  • Loading branch information
iLLiCiTiT authored Dec 20, 2021
2 parents 86fc268 + 9605abf commit 9c39f8f
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 68 deletions.
13 changes: 9 additions & 4 deletions openpype/lib/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,10 @@ def app_group(self):
def app_name(self):
return getattr(self.application, "full_name", None)

@property
def modules_manager(self):
return getattr(self.launch_context, "modules_manager", None)

def validate(self):
"""Optional validation of launch hook on initialization.
Expand Down Expand Up @@ -702,9 +706,13 @@ class ApplicationLaunchContext:
"""

def __init__(self, application, executable, **data):
from openpype.modules import ModulesManager

# Application object
self.application = application

self.modules_manager = ModulesManager()

# Logger
logger_name = "{}-{}".format(self.__class__.__name__, self.app_name)
self.log = PypeLogger.get_logger(logger_name)
Expand Down Expand Up @@ -812,10 +820,7 @@ def paths_to_launch_hooks(self):
paths.append(path)

# Load modules paths
from openpype.modules import ModulesManager

manager = ModulesManager()
paths.extend(manager.collect_launch_hook_paths())
paths.extend(self.modules_manager.collect_launch_hook_paths())

return paths

Expand Down
9 changes: 6 additions & 3 deletions openpype/lib/avalon_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -1433,7 +1433,11 @@ def get_creator_by_name(creator_name, case_sensitive=False):

@with_avalon
def change_timer_to_current_context():
"""Called after context change to change timers"""
"""Called after context change to change timers.
TODO:
- use TimersManager's static method instead of reimplementing it here
"""
webserver_url = os.environ.get("OPENPYPE_WEBSERVER_URL")
if not webserver_url:
log.warning("Couldn't find webserver url")
Expand All @@ -1448,8 +1452,7 @@ def change_timer_to_current_context():
data = {
"project_name": avalon.io.Session["AVALON_PROJECT"],
"asset_name": avalon.io.Session["AVALON_ASSET"],
"task_name": avalon.io.Session["AVALON_TASK"],
"hierarchy": get_hierarchy()
"task_name": avalon.io.Session["AVALON_TASK"]
}

requests.post(rest_api_url, json=data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def execute(self):
)
if entity:
self.ftrack_status_change(session, entity, project_name)
self.start_timer(session, entity, ftrack_api)

except Exception:
self.log.warning(
"Couldn't finish Ftrack procedure.", exc_info=True
Expand Down Expand Up @@ -160,26 +160,3 @@ def ftrack_status_change(self, session, entity, project_name):
" on Ftrack entity type \"{}\""
).format(next_status_name, entity.entity_type)
self.log.warning(msg)

def start_timer(self, session, entity, _ftrack_api):
"""Start Ftrack timer on task from context."""
self.log.debug("Triggering timer start.")

user_entity = session.query("User where username is \"{}\"".format(
os.environ["FTRACK_API_USER"]
)).first()
if not user_entity:
self.log.warning(
"Couldn't find user with username \"{}\" in Ftrack".format(
os.environ["FTRACK_API_USER"]
)
)
return

try:
user_entity.start_timer(entity, force=True)
session.commit()
self.log.debug("Timer start triggered successfully.")

except Exception:
self.log.warning("Couldn't trigger Ftrack timer.", exc_info=True)
3 changes: 3 additions & 0 deletions openpype/modules/default_modules/timers_manager/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class InvalidContextError(ValueError):
"""Context for which the timer should be started is invalid."""
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from openpype.lib import PostLaunchHook


class PostStartTimerHook(PostLaunchHook):
"""Start timer with TimersManager module.
This module requires enabled TimerManager module.
"""
order = None

def execute(self):
project_name = self.data.get("project_name")
asset_name = self.data.get("asset_name")
task_name = self.data.get("task_name")

missing_context_keys = set()
if not project_name:
missing_context_keys.add("project_name")
if not asset_name:
missing_context_keys.add("asset_name")
if not task_name:
missing_context_keys.add("task_name")

if missing_context_keys:
missing_keys_str = ", ".join([
"\"{}\"".format(key) for key in missing_context_keys
])
self.log.debug("Hook {} skipped. Missing data keys: {}".format(
self.__class__.__name__, missing_keys_str
))
return

timers_manager = self.modules_manager.modules_by_name.get(
"timers_manager"
)
if not timers_manager or not timers_manager.enabled:
self.log.info((
"Skipping starting timer because"
" TimersManager is not available."
))
return

timers_manager.start_timer_with_webserver(
project_name, asset_name, task_name, logger=self.log
)
22 changes: 14 additions & 8 deletions openpype/modules/default_modules/timers_manager/rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,23 @@ def register(self):
async def start_timer(self, request):
data = await request.json()
try:
project_name = data['project_name']
asset_name = data['asset_name']
task_name = data['task_name']
hierarchy = data['hierarchy']
project_name = data["project_name"]
asset_name = data["asset_name"]
task_name = data["task_name"]
except KeyError:
log.error("Payload must contain fields 'project_name, " +
"'asset_name', 'task_name', 'hierarchy'")
return Response(status=400)
msg = (
"Payload must contain fields 'project_name,"
" 'asset_name' and 'task_name'"
)
log.error(msg)
return Response(status=400, message=msg)

self.module.stop_timers()
self.module.start_timer(project_name, asset_name, task_name, hierarchy)
try:
self.module.start_timer(project_name, asset_name, task_name)
except Exception as exc:
return Response(status=404, message=str(exc))

return Response(status=200)

async def stop_timer(self, request):
Expand Down
Loading

0 comments on commit 9c39f8f

Please sign in to comment.