Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Commit

Permalink
user actions: testing
Browse files Browse the repository at this point in the history
  • Loading branch information
DonHaul committed Jul 25, 2024
1 parent 68cd819 commit a0d762e
Show file tree
Hide file tree
Showing 9 changed files with 1,231 additions and 3 deletions.
63 changes: 62 additions & 1 deletion backoffice/backoffice/workflows/api/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import logging

import requests
from django.shortcuts import get_object_or_404
from django_elasticsearch_dsl_drf.viewsets import BaseDocumentViewSet
from requests.exceptions import RequestException
from rest_framework import status, viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
Expand All @@ -14,7 +16,7 @@
WorkflowSerializer,
WorkflowTicketSerializer,
)
from backoffice.workflows.constants import WORKFLOW_DAG, ResolutionDags
from backoffice.workflows.constants import AUTHOR_DAGS, WORKFLOW_DAG, ResolutionDags
from backoffice.workflows.documents import WorkflowDocument
from backoffice.workflows.models import Workflow, WorkflowTicket

Expand Down Expand Up @@ -127,6 +129,65 @@ def resolve(self, request, pk=None):
ResolutionDags[serializer.validated_data["value"]].label, pk, extra_data
)

@action(detail=True, methods=["post"])
def restart(self, request, pk=None):
params = request.data.get("params")
restart_current_task = request.data.get("restart_current_task", False)

workflow = Workflow.objects.get(id=pk)

data = {"dry_run": False, "dag_run_id": pk, "reset_dag_runs": True}

executed_dags_for_workflow = {}
# find dags that were executed
for dag_id in AUTHOR_DAGS[workflow.workflow_type]:
response = requests.get(
f"{airflow_utils.AIRFLOW_BASE_URL}/api/v1/dags/{dag_id}/dagRuns/{pk}",
json=data,
headers=airflow_utils.AIRFLOW_HEADERS,
)
if response.status_code == status.HTTP_200_OK:
executed_dags_for_workflow[dag_id] = response.content

# assumes current task is one of the failed tasks
if restart_current_task:
data = {
"dry_run": False,
"dag_run_id": pk,
"reset_dag_runs": False,
"only_failed": True,
}

try:
response = requests.post(
f"{airflow_utils.AIRFLOW_BASE_URL}/api/v1/dags/{dag_id}/clearTaskInstances",
json=data,
headers=airflow_utils.AIRFLOW_HEADERS,
)
response.raise_for_status()

except RequestException:
data = {"error": response.json()}
return Response(data, status=status.HTTP_424_FAILED_DEPENDENCY)

return Response(response.json(), status=status.HTTP_200_OK)

# delete every executed_dag for this workflow
for dag_id in executed_dags_for_workflow:
# delete all executions of workflow
response = requests.delete(
f"{airflow_utils.AIRFLOW_BASE_URL}/api/v1/dags/{dag_id}/dagRuns/{pk}",
headers=airflow_utils.AIRFLOW_HEADERS,
)

return airflow_utils.trigger_airflow_dag(
WORKFLOW_DAG[workflow.workflow_type], pk, params
)

return Response(
{"error": "Failed to restart"}, status=status.HTTP_424_FAILED_DEPENDENCY
)


class WorkflowDocumentView(BaseDocumentViewSet):
def __init__(self, *args, **kwargs):
Expand Down
13 changes: 13 additions & 0 deletions backoffice/backoffice/workflows/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,16 @@ class WorkflowType(models.TextChoices):
class ResolutionDags(models.TextChoices):
accept = "accept", "author_create_approved_dag"
reject = "reject", "author_create_rejected_dag"


# author dags for each workflow type
AUTHOR_DAGS = {
WorkflowType.HEP_CREATE: "",
WorkflowType.HEP_UPDATE: "",
WorkflowType.AUTHOR_CREATE: (
"author_create_initialization_dag",
"author_create_approved_dag",
"author_create_rejected_dag",
),
WorkflowType.AUTHOR_UPDATE: ("author_update_dag",),
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
interactions:
- request:
body: '{"index":{"_id":"46f92be8-b7c4-4ab3-8496-83374558b3bc","_index":"backoffice-backend-test-workflows"}}
{"id":"46f92be8-b7c4-4ab3-8496-83374558b3bc","workflow_type":"AUTHOR_CREATE","data":{},"status":"running","is_update":false,"_created_at":"2024-07-25T15:29:20.618363+00:00","_updated_at":"2024-07-25T15:29:20.618369+00:00"}
'
headers:
connection:
- keep-alive
content-type:
- application/json
user-agent:
- opensearch-py/2.6.0 (Python 3.11.6)
method: POST
uri: http://opensearch:9200/_bulk?refresh=true
response:
body:
string: '{"took":14,"errors":false,"items":[{"index":{"_index":"backoffice-backend-test-workflows","_id":"46f92be8-b7c4-4ab3-8496-83374558b3bc","_version":1,"result":"created","forced_refresh":true,"_shards":{"total":2,"successful":1,"failed":0},"_seq_no":27,"_primary_term":1,"status":201}}]}'
headers:
content-length:
- '284'
content-type:
- application/json; charset=UTF-8
status:
code: 200
message: OK
- request:
body: '{"dag_run_id": "46f92be8-b7c4-4ab3-8496-83374558b3bc", "conf": {"workflow_id":
"46f92be8-b7c4-4ab3-8496-83374558b3bc"}}'
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Authorization:
- Basic YWlyZmxvdzphaXJmbG93
Connection:
- keep-alive
Content-Length:
- '119'
Content-Type:
- application/json
User-Agent:
- python-requests/2.31.0
method: POST
uri: http://host.docker.internal:8080/api/v1/dags/author_create_initialization_dag/dagRuns
response:
body:
string: "{\n \"conf\": {\n \"workflow_id\": \"46f92be8-b7c4-4ab3-8496-83374558b3bc\"\n
\ },\n \"dag_id\": \"author_create_initialization_dag\",\n \"dag_run_id\":
\"46f92be8-b7c4-4ab3-8496-83374558b3bc\",\n \"data_interval_end\": \"2024-07-25T15:29:20.702377+00:00\",\n
\ \"data_interval_start\": \"2024-07-25T15:29:20.702377+00:00\",\n \"end_date\":
null,\n \"execution_date\": \"2024-07-25T15:29:20.702377+00:00\",\n \"external_trigger\":
true,\n \"last_scheduling_decision\": null,\n \"logical_date\": \"2024-07-25T15:29:20.702377+00:00\",\n
\ \"note\": null,\n \"run_type\": \"manual\",\n \"start_date\": null,\n
\ \"state\": \"queued\"\n}\n"
headers:
Connection:
- close
Content-Length:
- '579'
Content-Type:
- application/json
Date:
- Thu, 25 Jul 2024 15:29:20 GMT
Server:
- gunicorn
X-Robots-Tag:
- noindex, nofollow
status:
code: 200
message: OK
- request:
body: '{"dry_run": false, "dag_run_id": "46f92be8-b7c4-4ab3-8496-83374558b3bc",
"reset_dag_runs": true}'
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Authorization:
- Basic YWlyZmxvdzphaXJmbG93
Connection:
- keep-alive
Content-Length:
- '96'
Content-Type:
- application/json
User-Agent:
- python-requests/2.31.0
method: GET
uri: http://host.docker.internal:8080/api/v1/dags/author_create_initialization_dag/dagRuns/46f92be8-b7c4-4ab3-8496-83374558b3bc
response:
body:
string: "{\n \"conf\": {\n \"workflow_id\": \"46f92be8-b7c4-4ab3-8496-83374558b3bc\"\n
\ },\n \"dag_id\": \"author_create_initialization_dag\",\n \"dag_run_id\":
\"46f92be8-b7c4-4ab3-8496-83374558b3bc\",\n \"data_interval_end\": \"2024-07-25T15:29:20.702377+00:00\",\n
\ \"data_interval_start\": \"2024-07-25T15:29:20.702377+00:00\",\n \"end_date\":
null,\n \"execution_date\": \"2024-07-25T15:29:20.702377+00:00\",\n \"external_trigger\":
true,\n \"last_scheduling_decision\": null,\n \"logical_date\": \"2024-07-25T15:29:20.702377+00:00\",\n
\ \"note\": null,\n \"run_type\": \"manual\",\n \"start_date\": null,\n
\ \"state\": \"queued\"\n}\n"
headers:
Connection:
- close
Content-Length:
- '579'
Content-Type:
- application/json
Date:
- Thu, 25 Jul 2024 15:29:20 GMT
Server:
- gunicorn
X-Robots-Tag:
- noindex, nofollow
status:
code: 200
message: OK
- request:
body: '{"dry_run": false, "dag_run_id": "46f92be8-b7c4-4ab3-8496-83374558b3bc",
"reset_dag_runs": true}'
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Authorization:
- Basic YWlyZmxvdzphaXJmbG93
Connection:
- keep-alive
Content-Length:
- '96'
Content-Type:
- application/json
User-Agent:
- python-requests/2.31.0
method: GET
uri: http://host.docker.internal:8080/api/v1/dags/author_create_approved_dag/dagRuns/46f92be8-b7c4-4ab3-8496-83374558b3bc
response:
body:
string: "{\n \"detail\": \"DAGRun with DAG ID: 'author_create_approved_dag'
and DagRun ID: '46f92be8-b7c4-4ab3-8496-83374558b3bc' not found\",\n \"status\":
404,\n \"title\": \"DAGRun not found\",\n \"type\": \"https://airflow.apache.org/docs/apache-airflow/2.8.3/stable-rest-api-ref.html#section/Errors/NotFound\"\n}\n"
headers:
Connection:
- close
Content-Length:
- '294'
Content-Type:
- application/problem+json
Date:
- Thu, 25 Jul 2024 15:29:20 GMT
Server:
- gunicorn
X-Robots-Tag:
- noindex, nofollow
status:
code: 404
message: NOT FOUND
- request:
body: '{"dry_run": false, "dag_run_id": "46f92be8-b7c4-4ab3-8496-83374558b3bc",
"reset_dag_runs": true}'
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Authorization:
- Basic YWlyZmxvdzphaXJmbG93
Connection:
- keep-alive
Content-Length:
- '96'
Content-Type:
- application/json
User-Agent:
- python-requests/2.31.0
method: GET
uri: http://host.docker.internal:8080/api/v1/dags/author_create_rejected_dag/dagRuns/46f92be8-b7c4-4ab3-8496-83374558b3bc
response:
body:
string: "{\n \"detail\": \"DAGRun with DAG ID: 'author_create_rejected_dag'
and DagRun ID: '46f92be8-b7c4-4ab3-8496-83374558b3bc' not found\",\n \"status\":
404,\n \"title\": \"DAGRun not found\",\n \"type\": \"https://airflow.apache.org/docs/apache-airflow/2.8.3/stable-rest-api-ref.html#section/Errors/NotFound\"\n}\n"
headers:
Connection:
- close
Content-Length:
- '294'
Content-Type:
- application/problem+json
Date:
- Thu, 25 Jul 2024 15:29:20 GMT
Server:
- gunicorn
X-Robots-Tag:
- noindex, nofollow
status:
code: 404
message: NOT FOUND
- request:
body: null
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Authorization:
- Basic YWlyZmxvdzphaXJmbG93
Connection:
- keep-alive
Content-Length:
- '0'
Content-Type:
- application/json
User-Agent:
- python-requests/2.31.0
method: DELETE
uri: http://host.docker.internal:8080/api/v1/dags/author_create_initialization_dag/dagRuns/46f92be8-b7c4-4ab3-8496-83374558b3bc
response:
body:
string: ''
headers:
Connection:
- close
Content-Type:
- application/json
Date:
- Thu, 25 Jul 2024 15:29:20 GMT
Server:
- gunicorn
X-Robots-Tag:
- noindex, nofollow
status:
code: 204
message: NO CONTENT
- request:
body: '{"dag_run_id": "46f92be8-b7c4-4ab3-8496-83374558b3bc", "conf": {"workflow_id":
"46f92be8-b7c4-4ab3-8496-83374558b3bc"}}'
headers:
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Authorization:
- Basic YWlyZmxvdzphaXJmbG93
Connection:
- keep-alive
Content-Length:
- '119'
Content-Type:
- application/json
User-Agent:
- python-requests/2.31.0
method: POST
uri: http://host.docker.internal:8080/api/v1/dags/author_create_initialization_dag/dagRuns
response:
body:
string: "{\n \"conf\": {\n \"workflow_id\": \"46f92be8-b7c4-4ab3-8496-83374558b3bc\"\n
\ },\n \"dag_id\": \"author_create_initialization_dag\",\n \"dag_run_id\":
\"46f92be8-b7c4-4ab3-8496-83374558b3bc\",\n \"data_interval_end\": \"2024-07-25T15:29:21.049469+00:00\",\n
\ \"data_interval_start\": \"2024-07-25T15:29:21.049469+00:00\",\n \"end_date\":
null,\n \"execution_date\": \"2024-07-25T15:29:21.049469+00:00\",\n \"external_trigger\":
true,\n \"last_scheduling_decision\": null,\n \"logical_date\": \"2024-07-25T15:29:21.049469+00:00\",\n
\ \"note\": null,\n \"run_type\": \"manual\",\n \"start_date\": null,\n
\ \"state\": \"queued\"\n}\n"
headers:
Connection:
- close
Content-Length:
- '579'
Content-Type:
- application/json
Date:
- Thu, 25 Jul 2024 15:29:21 GMT
Server:
- gunicorn
X-Robots-Tag:
- noindex, nofollow
status:
code: 200
message: OK
version: 1
Loading

0 comments on commit a0d762e

Please sign in to comment.