-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example of Databricks Apps which manages a job created by the same bundle #48
Open
andrewnester
wants to merge
3
commits into
databricks:main
Choose a base branch
from
andrewnester:apps
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+128
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
|
||
.databricks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Databricks App for working with Databricks jobs | ||
|
||
This example demonstrates how to define an Databricks App in a Databricks Asset Bundle. | ||
|
||
It includes and deploys an example app and a job managed by DABs to a Databricks workspace. | ||
The app shows current status of the job and lists all existing runs. | ||
|
||
For more information about Databricks Apps, please refer to the [documentation](https://docs.databricks.com/en/dev-tools/databricks-apps/index.html). | ||
|
||
## Prerequisites | ||
|
||
* Databricks CLI v0.238.0 or above | ||
|
||
## Usage | ||
|
||
Modify `databricks.yml`: | ||
* Update the `host` field under `workspace` to the Databricks workspace to deploy to. | ||
|
||
Run `databricks bundle deploy` to deploy the app. | ||
|
||
Run `databricks bundle run job_manager` to start the app and execute app deployment. | ||
When you change app code or config, you need to run `databricks bundle deploy` and `databricks bundle run job_manager` to update the app source code and configuration. | ||
|
||
Run `databricks bundle open` to navigate to the deployed app in your browser. Alternatively, run `databricks bundle summary` to display its URL. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
bundle: | ||
name: databricks_app | ||
|
||
include: | ||
- resources/*.yml | ||
|
||
|
||
#workspace: | ||
# host: https://myworkspace.databricks.com | ||
|
||
targets: | ||
dev: | ||
default: true | ||
mode: development |
16 changes: 16 additions & 0 deletions
16
knowledge_base/databricks_app/resources/hello_world.job.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
resources: | ||
jobs: | ||
hello_world: | ||
name: Serverless Hello World job | ||
tasks: | ||
- task_key: task | ||
spark_python_task: | ||
python_file: ../src/job/main.py | ||
environment_key: default | ||
|
||
environments: | ||
- environment_key: default | ||
spec: | ||
client: "1" | ||
dependencies: | ||
- cowsay |
29 changes: 29 additions & 0 deletions
29
knowledge_base/databricks_app/resources/job_manager.app.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
resources: | ||
apps: | ||
job_manager: | ||
name: "job_manager" | ||
description: "App which manages job created by this bundle" | ||
|
||
# The location of the source code for the app. | ||
source_code_path: ../src/app | ||
|
||
# The configuration for the app. This allows to pass environment variables to the app and | ||
# configure how to run the app. | ||
config: | ||
command: | ||
- flask | ||
- --app | ||
- app | ||
- run | ||
- --debug | ||
env: | ||
- name: JOB_ID | ||
value: ${resources.jobs.hello_world.id} | ||
|
||
# The resources which this app have an access to. | ||
resources: | ||
- name: "app-job" | ||
description: "A job for app to be able to work with" | ||
job: | ||
id: ${resources.jobs.hello_world.id} | ||
permission: "CAN_MANAGE_RUN" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import os | ||
|
||
from databricks.sdk import WorkspaceClient | ||
from flask import Flask, render_template, redirect, url_for | ||
|
||
app = Flask(__name__) | ||
|
||
|
||
@app.route("/") | ||
def home(): | ||
job_id = os.getenv("JOB_ID") | ||
|
||
w = WorkspaceClient() | ||
job = w.jobs.get(job_id) | ||
runs = w.jobs.list_runs(job_id=job_id) | ||
return render_template("index.html", job_name=job.settings.name, runs=runs) | ||
|
||
|
||
@app.route("/run") | ||
def run_job(): | ||
job_id = os.getenv("JOB_ID") | ||
|
||
w = WorkspaceClient() | ||
w.jobs.run_now(job_id=job_id) | ||
return redirect(url_for("home"), code=302) |
15 changes: 15 additions & 0 deletions
15
knowledge_base/databricks_app/src/app/templates/index.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<html> | ||
<head> | ||
<title>Databricks App managed by DABs</title> | ||
</head> | ||
<body> | ||
<h1>This app manages job "{{job_name}}"</h1> | ||
<h2>Job runs</h2> | ||
<ul> | ||
{% for run in runs %} | ||
<li>{{ run.job_run_id }}: {{ run.status.state }} (<a href="{{ run.run_page_url }}">Open this job run</a>)</li> | ||
{% endfor %} | ||
</ul> | ||
<a href="/run">Run job</a> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from cowsay import cow | ||
|
||
cow("Hello, world!") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of scope for this PR: we really need to find a different name for this property.