-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create setup scheduler solution branch
- Loading branch information
Showing
10 changed files
with
112 additions
and
366 deletions.
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
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 |
---|---|---|
@@ -1,225 +1,74 @@ | ||
import random | ||
from django.shortcuts import render, reverse, redirect | ||
from django.shortcuts import render | ||
from tethys_sdk.routing import controller | ||
from django.http.response import HttpResponseRedirect | ||
from django.contrib import messages | ||
from tethys_sdk.gizmos import Button | ||
from tethys_sdk.gizmos import JobsTable | ||
from tethys_compute.models.dask.dask_job_exception import DaskJobException | ||
from tethysapp.dask_tutorial.app import DaskTutorial as app | ||
|
||
# get job manager for the app | ||
job_manager = app.get_job_manager() | ||
|
||
|
||
@controller | ||
def home(request): | ||
""" | ||
Controller for the app home page. | ||
""" | ||
dask_delayed_button = Button( | ||
display_text='Dask Delayed Job', | ||
name='dask_delayed_button', | ||
attributes={ | ||
'data-bs-toggle': 'tooltip', | ||
'data-bs-placement': 'top', | ||
'title': 'Dask Delayed Job' | ||
}, | ||
href=reverse('dask_tutorial:run_job', kwargs={'job_type': 'delayed'}) | ||
) | ||
|
||
dask_distributed_button = Button( | ||
display_text='Dask Distributed Job', | ||
name='dask_distributed_button', | ||
save_button = Button( | ||
display_text='', | ||
name='save-button', | ||
icon='save', | ||
style='success', | ||
attributes={ | ||
'data-bs-toggle': 'tooltip', | ||
'data-bs-placement': 'top', | ||
'title': 'Dask Future Job' | ||
}, | ||
href=reverse('dask_tutorial:run_job', kwargs={'job_type': 'distributed'}) | ||
'data-bs-toggle':'tooltip', | ||
'data-bs-placement':'top', | ||
'title':'Save' | ||
} | ||
) | ||
|
||
dask_multiple_leaf_button = Button( | ||
display_text='Dask Multiple Leaf Jobs', | ||
name='dask_multiple_leaf_button', | ||
edit_button = Button( | ||
display_text='', | ||
name='edit-button', | ||
icon='pen', | ||
style='warning', | ||
attributes={ | ||
'data-bs-toggle': 'tooltip', | ||
'data-bs-placement': 'top', | ||
'title': 'Dask Multiple Leaf Jobs' | ||
}, | ||
href=reverse('dask_tutorial:run_job', kwargs={'job_type': 'multiple-leaf'}) | ||
) | ||
|
||
jobs_button = Button( | ||
display_text='Show All Jobs', | ||
name='dask_button', | ||
attributes={ | ||
'data-bs-toggle': 'tooltip', | ||
'data-bs-placement': 'top', | ||
'title': 'Show All Jobs' | ||
}, | ||
href=reverse('dask_tutorial:jobs_table') | ||
) | ||
|
||
context = { | ||
'dask_delayed_button': dask_delayed_button, | ||
'dask_distributed_button': dask_distributed_button, | ||
'dask_multiple_leaf_button': dask_multiple_leaf_button, | ||
'jobs_button': jobs_button, | ||
} | ||
|
||
return render(request, 'dask_tutorial/home.html', context) | ||
|
||
|
||
@controller | ||
def jobs_table(request): | ||
# Use job manager to get all the jobs. | ||
jobs = job_manager.list_jobs(order_by='-id', filters=None) | ||
|
||
# Table View | ||
jobs_table_options = JobsTable( | ||
jobs=jobs, | ||
column_fields=('id', 'name', 'description', 'creation_time'), | ||
hover=True, | ||
striped=False, | ||
bordered=False, | ||
condensed=False, | ||
actions=['logs', 'delete'], | ||
results_url='dask_tutorial:result', | ||
refresh_interval=1000, | ||
show_detailed_status=True, | ||
'data-bs-toggle':'tooltip', | ||
'data-bs-placement':'top', | ||
'title':'Edit' | ||
} | ||
) | ||
|
||
home_button = Button( | ||
display_text='Home', | ||
name='home_button', | ||
remove_button = Button( | ||
display_text='', | ||
name='remove-button', | ||
icon='trash', | ||
style='danger', | ||
attributes={ | ||
'data-bs-toggle': 'tooltip', | ||
'data-bs-placement': 'top', | ||
'title': 'Home' | ||
}, | ||
href=reverse('dask_tutorial:home') | ||
'data-bs-toggle':'tooltip', | ||
'data-bs-placement':'top', | ||
'title':'Remove' | ||
} | ||
) | ||
|
||
context = {'jobs_table': jobs_table_options, 'home_button': home_button} | ||
|
||
return render(request, 'dask_tutorial/jobs_table.html', context) | ||
|
||
|
||
@controller | ||
def result(request, job_id): | ||
# Use job manager to get the given job. | ||
job = job_manager.get_job(job_id=job_id) | ||
|
||
# Get result and name | ||
job_result = job.result | ||
name = job.name | ||
|
||
home_button = Button( | ||
display_text='Home', | ||
name='home_button', | ||
previous_button = Button( | ||
display_text='Previous', | ||
name='previous-button', | ||
attributes={ | ||
'data-bs-toggle': 'tooltip', | ||
'data-bs-placement': 'top', | ||
'title': 'Home' | ||
}, | ||
href=reverse('dask_tutorial:home') | ||
'data-bs-toggle':'tooltip', | ||
'data-bs-placement':'top', | ||
'title':'Previous' | ||
} | ||
) | ||
|
||
jobs_button = Button( | ||
display_text='Show All Jobs', | ||
name='dask_button', | ||
next_button = Button( | ||
display_text='Next', | ||
name='next-button', | ||
attributes={ | ||
'data-bs-toggle': 'tooltip', | ||
'data-bs-placement': 'top', | ||
'title': 'Show All Jobs' | ||
}, | ||
href=reverse('dask_tutorial:jobs_table') | ||
'data-bs-toggle':'tooltip', | ||
'data-bs-placement':'top', | ||
'title':'Next' | ||
} | ||
) | ||
|
||
context = { | ||
'result': job_result, | ||
'name': name, | ||
'home_button': home_button, | ||
'jobs_button': jobs_button | ||
'save_button': save_button, | ||
'edit_button': edit_button, | ||
'remove_button': remove_button, | ||
'previous_button': previous_button, | ||
'next_button': next_button | ||
} | ||
|
||
return render(request, 'dask_tutorial/results.html', context) | ||
|
||
|
||
@controller | ||
def error_message(request): | ||
messages.add_message(request, messages.ERROR, 'Invalid Scheduler!') | ||
return redirect(reverse('dask_tutorial:home')) | ||
|
||
|
||
@controller | ||
def run_job(request, job_type): | ||
""" | ||
Controller for the app home page. | ||
""" | ||
# Get scheduler from dask_primary setting. | ||
scheduler = app.get_scheduler(name='dask_primary') | ||
|
||
if job_type.lower() == 'delayed': | ||
from tethysapp.dask_tutorial.job_functions import delayed_job | ||
|
||
# Create dask delayed object | ||
delayed = delayed_job() | ||
dask = job_manager.create_job( | ||
job_type='DASK', | ||
name='dask_delayed', | ||
user=request.user, | ||
scheduler=scheduler, | ||
) | ||
|
||
# Execute future | ||
dask.execute(delayed) | ||
|
||
elif job_type.lower() == 'distributed': | ||
from tethysapp.dask_tutorial.job_functions import distributed_job, convert_to_dollar_sign | ||
|
||
# Get the client to create future | ||
try: | ||
client = scheduler.client | ||
except DaskJobException: | ||
return redirect(reverse('dask_tutorial:error_message')) | ||
|
||
# Create future job instance | ||
future = distributed_job(client) | ||
dask = job_manager.create_job( | ||
job_type='DASK', | ||
name='dask_distributed', | ||
user=request.user, | ||
scheduler=scheduler, | ||
) | ||
dask.process_results_function = convert_to_dollar_sign | ||
dask.execute(future) | ||
|
||
elif job_type.lower() == 'multiple-leaf': | ||
from tethysapp.dask_tutorial.job_functions import multiple_leaf_job | ||
|
||
# Get the client to create future | ||
try: | ||
client = scheduler.client | ||
except DaskJobException: | ||
return redirect(reverse('dask_tutorial:error_message')) | ||
|
||
# Create future job instance | ||
futures = multiple_leaf_job(client) | ||
|
||
# Execute multiple future | ||
i = random.randint(1, 10000) | ||
|
||
for future in futures: | ||
i += 1 | ||
name = 'dask_leaf' + str(i) | ||
dask = job_manager.create_job( | ||
job_type='DASK', | ||
name=name, | ||
user=request.user, | ||
scheduler=scheduler, | ||
) | ||
dask.execute(future) | ||
|
||
return HttpResponseRedirect(reverse('dask_tutorial:jobs_table')) | ||
return render(request, 'dask_tutorial/home.html', context) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Oops, something went wrong.