【问题标题】:Wait until databrick job is completed in Devops pipeline等到 Devops 管道中的 databrick 作业完成
【发布时间】:2022-08-14 23:16:40
【问题描述】:

我在 DevOps 中有一个管道,我通过databricks jobs run-now 命令触发数据块作业

databricks jobs run-now --job-id 246

输出

{
  \"run_id\": 1010,
  \"number_in_job\": 101022 
}

随着作业被触发,代理开始执行管道中的下一个任务,而databrick作业还没有完成,这里我想等到databricks作业在databricks中完成后再进行下一个任务

  • 您可以尝试具有--trace 命令行参数的dbx 工具:dbx.readthedocs.io/en/latest/…
  • \"作业应在启动之前部署。\" ,显然要使用具有 --trace 参数的 launch 命令,它需要首先部署,这超出了我的范围

标签: azure-devops azure-pipelines databricks azure-databricks


【解决方案1】:

使用以下代码使用作业运行 ID 等待作业完成:

while True:
    status = jobs_service.get_run(run_id)
    print(status)
    result_state = status["state"].get("result_state", None)
    if result_state:
        print(result_state)
        assert result_state == "SUCCESS"
   break
    else:
        time.sleep(5)

请参阅Implementing CI/CD on Databricks Using Databricks Notebooks and Azure DevOps 了解更多信息。

【讨论】:

    【解决方案2】:

    我在我的 devops 管道中做同样的事情。我使用 powershell 并将其余的 api 用于 databricks 。正如您在下面看到的,我使用相同的“run now”rest 端点来触发作业,然后使用“runs get”端点来获取状态,并等到它“running”或“pending”。

    因此,您可以使用它来触发作业,然后等待它完成。

    param
    (
    [Parameter(Mandatory=$true)][string] $bearertoken,
    [Parameter(Mandatory=$true)][string] $jobid,
    [Parameter(Mandatory=$true)][string] $azureregion
    )
    
    $auth= @{ 'Authorization' = "Bearer $bearertoken" }
    $jobrunurl="https://$azureregion.azuredatabricks.net/api/2.0/jobs/run-now"
    $jobmonitorrunurl="https://$azureregion.azuredatabricks.net/api/2.0/jobs/runs/get"
    
    
    $payload='{"job_id":' + $jobid + '}'
    
    Write-Host $payload
    
    
    
    $resp=Invoke-RestMethod -Uri $jobrunurl -Headers $auth -Method Post -Body $payload -ContentType 'application/json' 
    
    $runid=$resp.run_id
    
    $monitorrunresp=Invoke-RestMethod -Uri $jobmonitorrunurl -Headers $auth -Method Get -Body @{'run_id'= $runid} 
    $status=""
    
    do
    {
        if ($status -ne "")
        {
           Write-Host "Waiting for the job run to finish.Sleeping for 15 seconds"
           Start-Sleep -Seconds 15
        }
    
    
        $status= $monitorrunresp.state.life_cycle_state
        $monitorrunresp=Invoke-RestMethod -Uri $jobmonitorrunurl -Headers $auth -Method Get -Body @{'run_id'= $runid} 
    
    }while ($status -eq "RUNNING" -or $status -eq "PENDING")
    
    $resultstate = $monitorrunresp.state.result_state
    
    if ($resultstate -ne "SUCCESS")
    {
        Write-Host "Job $jobid failed"
    }
    
    else
    {
        Write-Host "Job $jobid succeeded"
    }
    

    你可以命名这个脚本

    Run-Databricksjob.ps1
    

    然后使用 powershell 任务从您的 devops 管道中调用,然后提供脚本的位置和参数,例如

    -bearertoken $(BearerToken) -jobid "yourjobid" -azureregion "yourazureregion"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      • 2020-09-15
      • 1970-01-01
      • 1970-01-01
      • 2022-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多