Skip to content

Commit

Permalink
Refactor Spark app status retrieval and enhance error handling
Browse files Browse the repository at this point in the history
- Moved the Spark app status retrieval logic from the route handler in spark_app.py to a static method in SparkApp class for better separation of concerns.
- Improved error handling and logging in the new get_spark_app_status method, ensuring clearer responses for application not found and internal errors.
- Simplified the route handler to directly return the response from the SparkApp method, enhancing code readability and maintainability.
  • Loading branch information
xuwenyihust committed Dec 10, 2024
1 parent e3c2315 commit 95232d9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
9 changes: 1 addition & 8 deletions server/app/routes/spark_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,4 @@ def create_spark_session():
@spark_app_blueprint.route('/spark_app/<spark_app_id>/status', methods=['GET'])
def get_spark_app_status(spark_app_id):
logging.info(f"Getting spark app status for app id: {spark_app_id}")
try:
spark_app = SparkAppModel.query.get(spark_app_id)
if spark_app:
return jsonify({'status': spark_app.status}), 200
return jsonify({'message': 'Spark application not found'}), 404
except Exception as e:
logging.error(f"Error getting spark app status: {e}")
return jsonify({'message': str(e)}), 500
return SparkApp.get_spark_app_status(spark_app_id)
24 changes: 23 additions & 1 deletion server/app/services/spark_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,26 @@ def create_spark_session(notebook_path: str = None):
logger.error(f"Error creating spark session: {e}")
return Response(
response=json.dumps({'message': str(e)}),
status=500)
status=500)

@staticmethod
def get_spark_app_status(spark_app_id: str):
logger.info(f"Getting spark app status for app id: {spark_app_id}")
try:
spark_app = SparkAppModel.query.filter_by(spark_app_id=spark_app_id).first()
if spark_app is None:
logger.error("Spark application not found")
return Response(
response=json.dumps({'message': 'Spark application not found'}),
status=404
)
return Response(
response=json.dumps({'status': spark_app.status}),
status=200
)
except Exception as e:
logger.error(f"Error getting spark app status: {e}")
return Response(
response=json.dumps({'message': str(e)}),
status=500
)

0 comments on commit 95232d9

Please sign in to comment.