-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabricks-submit-job.ps1
67 lines (50 loc) · 2 KB
/
databricks-submit-job.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
param (
[Parameter(Mandatory=$true)]
[string]$token,
[Parameter(Mandatory=$true)]
[string]$notebook
)
$urlRunsSubmit = "https://eastus2.azuredatabricks.net/api/2.0/jobs/runs/submit"
$urlRunsGet = "https://eastus2.azuredatabricks.net/api/2.0/jobs/runs/get"
$ContentType = "application/json"
try{
$json = @"
{
"run_name": "my spark task",
"existing_cluster_id": "0410-232610-acid490",
"timeout_seconds": 3600,
"notebook_task": {
"notebook_path": "$($notebook)"
}
}
"@
Write-Host("Running notebook: " + $notebook + " -- " + $(Get-Date -Format o))
$post = Invoke-RestMethod -Method Post -Uri $urlRunsSubmit -Body $json -ContentType $ContentType -Headers @{"Authorization"="Bearer $token"}
$urlRunsGet = $urlRunsGet + "?run_id=" + $post.run_id
$message = ""
DO {
if ($message -ne "") {
Start-Sleep -s 5
}
$post = Invoke-RestMethod -Method Get -Uri $urlRunsGet -ContentType $ContentType -Headers @{"Authorization"="Bearer $token"}
if ($message -ne "Run ID: " + $post.run_id + " - Life cycle state: " + $post.state.life_cycle_state) {
$message = "Run ID: " + $post.run_id + " - Life cycle state: " + $post.state.life_cycle_state
Write-Host( $message + " -- " + $(Get-Date -Format o))
}
} While ( ($post.state.life_cycle_state -ne "TERMINATED") -and ($post.state.life_cycle_state -ne "INTERNAL_ERROR"))
Write-Host("Life result state: " + $post.state.result_state + " -- " + $(Get-Date -Format o))
if ($post.state.result_state -eq "SUCCESS") {
$LastExitCode = 1
}else{
$LastExitCode = 0
throw "Error: " + $post.state.state_message
}
} catch {
write-Error (Convertto-Json $_.Exception.Response)
#$headers = $_.Exception.Response.Headers
#$cookies = $_.Exception.Response.Cookies
#$headers |% { write-host "$_=$($headers[$_])"}
#$cookies |% { write-host "$_=$($cookies[$_])"}
$LastExitCode = 0
}
exit $LastExitCode