mirror of
https://github.com/netbirdio/netbird.git
synced 2026-07-20 15:31:30 +02:00
clean up
This commit is contained in:
@@ -68,7 +68,7 @@ func (h *Handler) CreateJob(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
if err := h.accountManager.CreateJob(ctx, userAuth.AccountId, peerID, userAuth.UserId, job); err != nil {
|
||||
util.WriteErrorResponse(fmt.Sprintf("failed to create job %v", err), http.StatusBadRequest, w)
|
||||
util.WriteErrorResponse(fmt.Sprintf("failed to create job %v", err), http.StatusInternalServerError, w)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -76,7 +76,8 @@ func (h *Handler) CreateJob(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (h *Handler) ListJobs(w http.ResponseWriter, r *http.Request) {
|
||||
userAuth, err := nbcontext.GetUserAuthFromContext(r.Context())
|
||||
ctx := r.Context()
|
||||
userAuth, err := nbcontext.GetUserAuthFromContext(ctx)
|
||||
if err != nil {
|
||||
util.WriteError(r.Context(), err, w)
|
||||
return
|
||||
@@ -85,13 +86,13 @@ func (h *Handler) ListJobs(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
peerID := vars["peerId"]
|
||||
|
||||
jobs, err := h.accountManager.GetAllJobs(r.Context(), userAuth.AccountId, userAuth.UserId, peerID)
|
||||
jobs, err := h.accountManager.GetAllJobs(ctx, userAuth.AccountId, userAuth.UserId, peerID)
|
||||
if err != nil {
|
||||
util.WriteErrorResponse(fmt.Sprintf("failed to fetch jobs %v", err), http.StatusBadRequest, w)
|
||||
util.WriteErrorResponse(fmt.Sprintf("failed to fetch jobs %v", err), http.StatusInternalServerError, w)
|
||||
return
|
||||
}
|
||||
|
||||
util.WriteJSONObject(r.Context(), w, jobs)
|
||||
util.WriteJSONObject(ctx, w, jobs)
|
||||
}
|
||||
|
||||
func (h *Handler) GetJob(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -108,7 +109,7 @@ func (h *Handler) GetJob(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
job, err := h.accountManager.GetJobByID(ctx, userAuth.AccountId, userAuth.UserId, peerID, jobID)
|
||||
if err != nil {
|
||||
util.WriteErrorResponse(fmt.Sprintf("failed to fetch job %v", err), http.StatusBadRequest, w)
|
||||
util.WriteErrorResponse(fmt.Sprintf("failed to fetch job %v", err), http.StatusInternalServerError, w)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -334,10 +334,6 @@ func (am *DefaultAccountManager) UpdatePeer(ctx context.Context, accountID, user
|
||||
}
|
||||
|
||||
func (am *DefaultAccountManager) CreateJob(ctx context.Context, accountID, peerID, userID string, job *types.Job) error {
|
||||
if err := job.ValidateJobRequest(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
unlock := am.Store.AcquireWriteLockByUID(ctx, accountID)
|
||||
defer unlock()
|
||||
|
||||
|
||||
@@ -142,15 +142,14 @@ func (s *SqlStore) SaveJob(ctx context.Context, job *types.Job) error {
|
||||
|
||||
// job was pending for too long and has been cancelled
|
||||
// todo call it when we first start the jobChannel to make sure no stuck jobs
|
||||
func (s *SqlStore) MarkPendingJobsAsFailed(ctx context.Context, triggeredBy string) error {
|
||||
func (s *SqlStore) MarkPendingJobsAsFailed(ctx context.Context) error {
|
||||
now := time.Now().UTC()
|
||||
friendlyReason := "Pending job cleanup: marked as failed automatically due to being stuck too long"
|
||||
return s.db.WithContext(ctx).
|
||||
Model(&types.Job{}).
|
||||
Where("status = ?", types.JobStatusPending).
|
||||
Updates(map[string]any{
|
||||
"status": types.JobStatusFailed,
|
||||
"failed_reason": friendlyReason,
|
||||
"failed_reason": "Pending job cleanup: marked as failed automatically due to being stuck too long",
|
||||
"completed_at": now,
|
||||
}).Error
|
||||
}
|
||||
|
||||
@@ -209,7 +209,7 @@ type Store interface {
|
||||
GetJobByID(ctx context.Context, accountID, jobID string) (*types.Job, error)
|
||||
GetJobs(ctx context.Context, accountID, peerID string) ([]*types.Job, error)
|
||||
CompleteJob(ctx context.Context, accountID, jobID, result string, failedReason string) error
|
||||
MarkPendingJobsAsFailed(ctx context.Context, triggeredBy string) error
|
||||
MarkPendingJobsAsFailed(ctx context.Context) error
|
||||
}
|
||||
|
||||
const (
|
||||
|
||||
@@ -82,7 +82,7 @@ func NewJob(triggeredBy, accountID, peerID string, jobType JobType, parameters m
|
||||
}
|
||||
|
||||
// Validate job
|
||||
if err := job.ValidateJobRequest(); err != nil {
|
||||
if err := job.validateJobRequest(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -110,10 +110,6 @@ func (j *Job) encodeParameters(params map[string]any) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// IsPending returns true if job is pending
|
||||
func (j *Job) IsPending() bool {
|
||||
return j.Status == JobStatusPending
|
||||
}
|
||||
|
||||
// MarkSucceeded sets job as completed successfully
|
||||
func (j *Job) MarkSucceeded(result string) {
|
||||
@@ -131,12 +127,8 @@ func (j *Job) MarkFailed(reason string) {
|
||||
j.CompletedAt = &now
|
||||
}
|
||||
|
||||
// HasCompleted checks if job is completed (success or fail)
|
||||
func (j *Job) HasCompleted() bool {
|
||||
return j.Status == JobStatusSucceeded || j.Status == JobStatusFailed
|
||||
}
|
||||
|
||||
func (j *Job) ValidateJobRequest() error {
|
||||
func (j *Job) validateJobRequest() error {
|
||||
if j == nil {
|
||||
return fmt.Errorf("job cannot be nil")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user