6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 | class JobKiller(LocalAction):
"""Kill jobs that are marked as running."""
identifier = "ayon.job.killer"
label = "AYON Admin"
variant = "- Job Killer"
description = "Killing selected running jobs"
icon = get_ftrack_icon_url("AYONAdmin.svg")
settings_key = "job_killer"
def discover(self, session, entities, event):
"""Check if action is available for user role."""
return self.valid_roles(session, entities, event)
def interface(self, session, entities, event):
if event["data"].get("values"):
return
title = "Select jobs to kill"
jobs = session.query(
"select id, user_id, status, created_at, data from Job"
" where status in (\"queued\", \"running\")"
).all()
if not jobs:
return {
"success": True,
"message": "Didn't found any running jobs"
}
# Collect user ids from jobs
user_ids = set()
for job in jobs:
user_id = job["user_id"]
if user_id:
user_ids.add(user_id)
# Store usernames by their ids
usernames_by_id = {}
if user_ids:
users = session.query(
"select id, username from User where id in ({})".format(
self.join_query_keys(user_ids)
)
).all()
for user in users:
usernames_by_id[user["id"]] = user["username"]
items = []
for job in jobs:
try:
data = json.loads(job["data"])
desctiption = data["description"]
except Exception:
desctiption = "*No description*"
user_id = job["user_id"]
username = usernames_by_id.get(user_id) or "Unknown user"
created = job["created_at"].strftime("%d.%m.%Y %H:%M:%S")
label = "{} - {} - {}".format(
username, desctiption, created
)
item_label = {
"type": "label",
"value": label
}
item = {
"name": job["id"],
"type": "boolean",
"value": False
}
if len(items) > 0:
items.append({"type": "label", "value": "---"})
items.append(item_label)
items.append(item)
return {
"items": items,
"title": title
}
def launch(self, session, entities, event):
if "values" not in event["data"]:
return
values = event["data"]["values"]
if len(values) < 1:
return {
"success": True,
"message": "No jobs to kill!"
}
job_ids = set()
for job_id, kill_job in values.items():
if kill_job:
job_ids.add(job_id)
jobs = session.query(
"select id, status from Job where id in ({})".format(
self.join_query_keys(job_ids)
)
).all()
# Update all the queried jobs, setting the status to failed.
for job in jobs:
try:
origin_status = job["status"]
self.log.debug((
'Changing Job ({}) status: {} -> failed'
).format(job["id"], origin_status))
job["status"] = "failed"
session.commit()
except Exception:
session.rollback()
self.log.warning((
"Changing Job ({}) has failed"
).format(job["id"]))
self.log.info("All selected jobs were killed Successfully!")
return {
"success": True,
"message": "All selected jobs were killed Successfully!"
}
|