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

[FEAT][API] Add an API to cancel jobs #66

Open
youen opened this issue Mar 31, 2024 · 0 comments
Open

[FEAT][API] Add an API to cancel jobs #66

youen opened this issue Mar 31, 2024 · 0 comments

Comments

@youen
Copy link
Member

youen commented Mar 31, 2024

Description

Add an API to cancel jobs.

Request

Method

POST

URL

/jobs/{id}/cancel

Request Parameters

None

Request Body

None

Result

Result parameters

None

Result Body

None

Example Curl Request

Request

curl -X POST "http://localhost:8080/jobs/{id}/cancel"

Curl Response

HTTP/1.1 200 OK
Content-Length: 0

Go server

Struct

type cancelJobRequest struct{}

Handler

func (h *handler) cancelJob(w http.ResponseWriter, r *http.Request) {
    ctx := r.Context()
    params := mux.Vars(r)
    jobID := params["id"]

    job, err := h.store.GetJob(ctx, jobID)
    if err != nil {
        h.log.WithError(err).Errorf("failed to get job %s", jobID)
        http.Error(w, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
        return
    }

    if err := h.store.CancelJob(ctx, jobID); err != nil {
        h.log.WithError(err).Errorf("failed to cancel job %s", jobID)
        http.Error(w, http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
        return
    }

    w.WriteHeader(http.StatusOK)
}
```training",
  "created_at": "2022-08-09T10:21:31.863Z",
  "status": "canceled",
  "error_message": null,
  "traceback": null,
  "result": null,
  "parameters": {
    "model_id": "my-model",
    "dataset_id": "my-dataset",
    "column_to_predict": "target"
  },
  "current_progress": null
}

Go server

Struct

import (
	"time"

	"github.com/google/uuid"
	"github.com/octopize/go-avatar/avatar"
	"github.com/octopize/go-avatar/avatar/models"
)

// Job represents a job in the system.
type Job struct {
	ID            uuid.UUID       `json:"id"`
	Kind          models.JobKind   `json:"kind"`
	CreatedAt     time.Time       `json:"created_at"`
	Status        models.JobStatus `json:"status"`
	ErrorMessage  string          `json:"error_message"`
	Traceback     string          `json:"traceback"`
	Result        interface{}     `json:"result"`
	Parameters    avatar.Parameters `json:"parameters"`
	CurrentStatus models.JobProgress `json:"current_progress"`
}

Handler

import (
	"context"
	"net/http"

	"github.com/google/uuid"
	"github.com/gorilla/mux"

	"github.com/octopize/go-avatar/avatar"
	"github.com/octopize/go-avatar/avatar/models"
)

// cancelJob cancels a job.
func (h *Handler) cancelJob(w http.ResponseWriter, r *http.Request) {
	// Get job ID from the URL.
	id := mux.Vars(r)["id"]

	// Get job from the database.
	job, err := h.jobRepo.GetByID(context.Background(), id)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// Cancel the job.
	if err := h.jobService.Cancel(job.ID); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// Write the job to the response.
	if err := json.NewEncoder(w).Encode(job); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}

Links

Automated Issue Details

Dear visitor,

This issue has been automatically generated from the Octopize project avatar-python to make SIGO compatible. Please vote with a thumbs up or thumbs down to assess the quality of the automatic generation.

Best regards,
The SIGO Team

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant