Skip to content

Commit

Permalink
Add logging to API functions
Browse files Browse the repository at this point in the history
Before this patch, we lost the error log and associated stack trace from APScheduler. This patch logs these events along with the existing JSON response that the API already provides.
  • Loading branch information
pysanders committed Dec 28, 2021
1 parent de1fe6a commit d55fdf1
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions flask_apscheduler/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging

from apscheduler.jobstores.base import ConflictingIdError, JobLookupError
from collections import OrderedDict
from flask import current_app, request, Response
Expand Down Expand Up @@ -41,8 +43,10 @@ def add_job():
job = current_app.apscheduler.add_job(**data)
return jsonify(job)
except ConflictingIdError:
logging.warning(f'Job {data.get("id")} already exists.')
return jsonify(dict(error_message='Job %s already exists.' % data.get('id')), status=409)
except Exception as e:
logging.error(e, exc_info=True)
return jsonify(dict(error_message=str(e)), status=500)


Expand All @@ -53,8 +57,10 @@ def delete_job(job_id):
current_app.apscheduler.remove_job(job_id)
return Response(status=204)
except JobLookupError:
logging.warning(f'Job {job_id} not found.')
return jsonify(dict(error_message='Job %s not found' % job_id), status=404)
except Exception as e:
logging.error(e, exc_info=True)
return jsonify(dict(error_message=str(e)), status=500)


Expand All @@ -64,6 +70,7 @@ def get_job(job_id):
job = current_app.apscheduler.get_job(job_id)

if not job:
logging.warning(f'Job {job_id} not found.')
return jsonify(dict(error_message='Job %s not found' % job_id), status=404)

return jsonify(job)
Expand Down Expand Up @@ -92,8 +99,10 @@ def update_job(job_id):
job = current_app.apscheduler.get_job(job_id)
return jsonify(job)
except JobLookupError:
logging.warning(f'Job {job_id} not found.')
return jsonify(dict(error_message='Job %s not found' % job_id), status=404)
except Exception as e:
logging.error(e, exc_info=True)
return jsonify(dict(error_message=str(e)), status=500)


Expand All @@ -105,8 +114,10 @@ def pause_job(job_id):
job = current_app.apscheduler.get_job(job_id)
return jsonify(job)
except JobLookupError:
logging.warning(f'Job {job_id} not found.')
return jsonify(dict(error_message='Job %s not found' % job_id), status=404)
except Exception as e:
logging.error(e, exc_info=True)
return jsonify(dict(error_message=str(e)), status=500)


Expand All @@ -118,8 +129,10 @@ def resume_job(job_id):
job = current_app.apscheduler.get_job(job_id)
return jsonify(job)
except JobLookupError:
logging.warning(f'Job {job_id} not found.')
return jsonify(dict(error_message='Job %s not found' % job_id), status=404)
except Exception as e:
logging.error(e, exc_info=True)
return jsonify(dict(error_message=str(e)), status=500)


Expand All @@ -131,6 +144,8 @@ def run_job(job_id):
job = current_app.apscheduler.get_job(job_id)
return jsonify(job)
except JobLookupError:
logging.warning(f'Job {job_id} not found.')
return jsonify(dict(error_message='Job %s not found' % job_id), status=404)
except Exception as e:
logging.error(e, exc_info=True)
return jsonify(dict(error_message=str(e)), status=500)

0 comments on commit d55fdf1

Please sign in to comment.