forked from openedx-unsupported/edx-certificates
-
Notifications
You must be signed in to change notification settings - Fork 2
/
certificate_agent.py
179 lines (156 loc) · 6.12 KB
/
certificate_agent.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# -*- coding: utf-8 -*-
from argparse import ArgumentParser, RawTextHelpFormatter
import logging.config
import json
import sys
import os
import time
import settings
from openedx_certificates.queue_xqueue import XQueuePullManager
from gen_cert import CertificateGen
logging.config.dictConfig(settings.LOGGING)
log = logging.getLogger('certificates: ' + __name__)
def parse_args(args=sys.argv[1:]):
parser = ArgumentParser(description="""
Generate edX certificates
-------------------------
This script will continuously monitor a queue
for certificate generation, it does the following:
* Connect to the xqueue server
* Pull a single certificate request
* Process the request
* Post a result back to the xqueue server
A global exception handler will catch any error
during the certificate generation process and
post a result back to the LMS indicating there
was a problem.
""", formatter_class=RawTextHelpFormatter)
parser.add_argument(
'--aws-id',
default=settings.CERT_AWS_ID,
help='AWS ID for write access to the S3 bucket',
)
parser.add_argument(
'--aws-key',
default=settings.CERT_AWS_KEY,
help='AWS KEY for write access to the S3 bucket',
)
return parser.parse_args()
def main():
manager = XQueuePullManager(settings.QUEUE_URL, settings.QUEUE_NAME,
settings.QUEUE_AUTH_USER,
settings.QUEUE_AUTH_PASS,
settings.QUEUE_USER, settings.QUEUE_PASS)
while True:
if manager.get_length() == 0:
log.debug("{0} has no jobs".format(str(manager)))
time.sleep(settings.QUEUE_POLL_FREQUENCY)
continue
else:
log.debug('queue length: {0}'.format(manager.get_length()))
certdata = manager.get_submission()
log.debug('xqueue response: {0}'.format(certdata))
try:
xqueue_body = json.loads(certdata['xqueue_body'])
xqueue_header = json.loads(certdata['xqueue_header'])
action = xqueue_body['action']
username = xqueue_body['username'].encode('utf-8')
course_id = xqueue_body['course_id'].encode('utf-8')
course_name = xqueue_body['course_name'].encode('utf-8')
name = xqueue_body['name'].encode('utf-8')
template_pdf = xqueue_body.get('template_pdf', None)
grade = xqueue_body.get('grade', None)
if grade:
grade = grade.encode('utf-8')
issued_date = xqueue_body.get('issued_date', None)
designation = xqueue_body.get('designation', None)
if designation:
designation = designation.encode('utf-8')
cert = CertificateGen(
course_id,
template_pdf,
aws_id=args.aws_id,
aws_key=args.aws_key,
long_course=course_name,
issued_date=issued_date,
designation=designation,
)
except (TypeError, ValueError, KeyError, IOError) as e:
log.critical('Unable to parse queue submission ({0}) : {1}'.format(e, certdata))
if settings.DEBUG:
raise
else:
continue
try:
log.info(
"Generating certificate for {username} ({name}), "
"in {course_id}, with grade {grade} and designation {designation}".format(
username=username,
name=name,
course_id=course_id,
grade=grade,
designation=designation,
)
)
(download_uuid,
verify_uuid,
download_url) = cert.create_and_upload(name, grade=grade)
except Exception as e:
# global exception handler, if anything goes wrong
# during the generation of the pdf we will let the LMS
# know so it can be re-submitted, the LMS will update
# the state to error
# get as much info as possible about the exception
# for the post back to the LMS
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
error_reason = (
"({username} {course_id}) "
"{exception_type}: {exception}: "
"{file_name}:{line_number}".format(
username=username,
course_id=course_id,
exception_type=exc_type,
exception=e,
file_name=fname,
line_number=exc_tb.tb_lineno,
)
)
log.critical(
'An error occurred during certificate generation {reason}'.format(
reason=error_reason,
)
)
xqueue_reply = {
'xqueue_header': json.dumps(xqueue_header),
'xqueue_body': json.dumps({
'error': 'There was an error processing the certificate request: {error}'.format(
error=e,
),
'username': username,
'course_id': course_id,
'error_reason': error_reason,
}),
}
manager.respond(xqueue_reply)
if settings.DEBUG:
raise
else:
continue
# post result back to the LMS
xqueue_reply = {
'xqueue_header': json.dumps(xqueue_header),
'xqueue_body': json.dumps({
'action': action,
'download_uuid': download_uuid,
'verify_uuid': verify_uuid,
'username': username,
'course_id': course_id,
'url': download_url,
}),
}
log.info("Posting result to the LMS: {0}".format(xqueue_reply))
manager.respond(xqueue_reply)
if __name__ == '__main__': # pragma: no cover
args = parse_args()
main()