Skip to content
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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions knowledge_base/databricks_app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

.databricks
24 changes: 24 additions & 0 deletions knowledge_base/databricks_app/README.md
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.
14 changes: 14 additions & 0 deletions knowledge_base/databricks_app/databricks.yml
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 knowledge_base/databricks_app/resources/hello_world.job.yml
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 knowledge_base/databricks_app/resources/job_manager.app.yml
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:
Copy link
Contributor

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.

- 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"
25 changes: 25 additions & 0 deletions knowledge_base/databricks_app/src/app/app.py
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 knowledge_base/databricks_app/src/app/templates/index.html
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>
3 changes: 3 additions & 0 deletions knowledge_base/databricks_app/src/job/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from cowsay import cow

cow("Hello, world!")