Skip to content

Commit

Permalink
Add create_spark_app endpoint and enhance error handling in SparkApp …
Browse files Browse the repository at this point in the history
…service
  • Loading branch information
xuwenyihust committed Dec 10, 2024
1 parent b433992 commit 7ff6ddd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
9 changes: 8 additions & 1 deletion server/app/routes/spark_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@ 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}")
return SparkApp.get_spark_app_status(spark_app_id)
return SparkApp.get_spark_app_status(spark_app_id)

@spark_app_blueprint.route('/spark_app/<spark_app_id>', methods=['POST'])
def create_spark_app(spark_app_id):
logging.info(f"Creating spark app with id: {spark_app_id}")
data = request.get_json()
notebook_path = data.get('notebookPath')
return SparkApp.create_spark_app(spark_app_id=spark_app_id, notebook_path=notebook_path)
37 changes: 36 additions & 1 deletion server/app/services/spark_app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from app.models.spark_app import SparkAppModel
from app.models.notebook import NotebookModel
from app.models.spark_app_config import SparkAppConfigModel
from flask import Response
from flask import g, Response
from datetime import datetime
import json
from database import db
Expand Down Expand Up @@ -192,6 +192,41 @@ def get_spark_app_status(spark_app_id: str):
)
except Exception as e:
logger.error(f"Error getting spark app status: {e}")
return Response(
response=json.dumps({'message': str(e)}),
status=500
)

@staticmethod
def create_spark_app(spark_app_id: str, notebook_path: str):
logger.info(f"Creating spark app with id: {spark_app_id} for notebook: {notebook_path}")
try:
# Get the notebook
notebook = NotebookModel.query.filter_by(path=notebook_path).first()
if notebook is None:
logger.error("Notebook not found")
return Response(
response=json.dumps({'message': 'Notebook not found'}),
status=404
)

# Create new spark app
spark_app = SparkAppModel(
spark_app_id=spark_app_id,
notebook_id=notebook.id,
user_id=g.user.id,
created_at=datetime.now()
)

db.session.add(spark_app)
db.session.commit()

return Response(
response=json.dumps(spark_app.to_dict()),
status=200
)
except Exception as e:
logger.error(f"Error creating spark app: {e}")
return Response(
response=json.dumps({'message': str(e)}),
status=500
Expand Down

0 comments on commit 7ff6ddd

Please sign in to comment.