-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrunner.py
More file actions
124 lines (97 loc) · 4.7 KB
/
runner.py
File metadata and controls
124 lines (97 loc) · 4.7 KB
1
2
3
4
5
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
import os
import traceback
from typing import Optional
from pipebio.models.job_status import JobStatus
from pipebio.pipebio_client import PipebioClient
from custom_code import trinityJob
from exceptions import UserFacingException
def main():
"""
This is the entrypoint of the plugin.
Plugin authors should only edit the line that calls their custom code,
the rest can be seen as boilerplate that sets up the plugin.
:return:
"""
client = PipebioClient()
try:
job = client.jobs.get()
user = client.user
# Update the job, to indicate that it is running.
client.jobs.update(status=JobStatus.RUNNING,
progress=1,
messages=['Running plugin job.'])
# Add job status message, showing the user the plugin is running as.
client.jobs.update(status=JobStatus.RUNNING,
progress=10,
messages=[f"Logged in for {user['firstName']} {user['lastName']}."])
# Get the input_entity_ids from the job inputEntities.
input_entities = job['inputEntities'] if job['inputEntities'] is not None else []
input_entity_ids = list(
map(
lambda input_entity: input_entity['id'], input_entities
)
)
# Sanity check, to ensure there is at least one input document.
if len(input_entity_ids) == 0:
raise Exception("No documents found in inputEntities")
folder_key = 'TARGET_FOLDER_ID'
target_folder_id = int(os.environ[folder_key]) if folder_key in os.environ else None
# This is the line a plugin author would change, to call your custom code.
# Below are passed as args (though your use case may need different args):
# - the PipeBio client instance.
# - the ids of the entities to process (parsed from the job inputs).
# - the id of the folder to write the job results to (parsed from the job inputs).
output_entity = trinityJob.run(client, input_entity_ids, target_folder_id)
# Set job as complete, adding the entity id of the output, so it is shown in the jobs table.
output_entity_id = output_entity['id']
client.jobs.set_complete(messages=["Completed plugin job."],
output_entity_ids=[output_entity_id])
except UserFacingException as exception:
track = traceback.format_exc()
print("job_wrapper: caught SafeException!!!")
print(str(exception))
print(track)
messages = [exception.user_message]
creator = get_plugin_author(client, job)
if creator is not None:
# Allows users who experience issue to get in contact with the Plugin creator.
messages.append(f"The plugin developer {creator} may be able to help")
client.jobs.update(status=JobStatus.FAILED,
progress=100,
messages=messages)
except Exception as exception:
print("Runner caught exception")
track = traceback.format_exc()
str_exception = str(exception)
print(str_exception)
print(track)
messages = [f"Unexpected error in plugin job: {str_exception}"]
creator = get_plugin_author(client, job)
if creator is not None:
# Allows users who experience issue to get in contact with the Plugin creator.
messages.append(f"The plugin developer {creator} may be able to help")
client.jobs.update(status=JobStatus.FAILED,
progress=100,
messages=messages)
raise exception
def get_plugin_author(client, job) -> Optional[str]:
"""
Utility function to look up the creator of a plugin, useful for creating helpful error messages for users.
:param client:
:param job:
:return:
"""
plugin_id = job["params"]["pluginId"] if "params" in job and "pluginId" in job["params"] else None
organization_id = client.user["orgs"][0]["id"] if "orgs" in client.user and len(client.user["orgs"]) > 0 and "id" in client.user["orgs"][0] else None
if plugin_id is None or organization_id is None:
return None
plugin_response = client.session.get(f"organizations/{organization_id}/lists/{plugin_id}")
plugin_response_json = plugin_response.json()
creator_id = plugin_response_json["creatorId"] if "creatorId" in plugin_response_json else None
if creator_id is None:
return None
user_response = client.session.get(f"organizations/{organization_id}/users/{creator_id}")
user = user_response.json()
return user["email"] if "email" in user else None
if __name__ == '__main__':
main()