github.com/wfusion/gofusion@v1.1.14/common/infra/asynq/asynqmon/scheduler_entry_handlers.go (about)

     1  package asynqmon
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/gorilla/mux"
     7  
     8  	"github.com/wfusion/gofusion/common/infra/asynq"
     9  	"github.com/wfusion/gofusion/common/utils/serialize/json"
    10  )
    11  
    12  // ****************************************************************************
    13  // This file defines:
    14  //   - http.Handler(s) for scheduler entry related endpoints
    15  // ****************************************************************************
    16  
    17  func newListSchedulerEntriesHandlerFunc(inspector *asynq.Inspector, pf PayloadFormatter) http.HandlerFunc {
    18  	return func(w http.ResponseWriter, r *http.Request) {
    19  		entries, err := inspector.SchedulerEntries()
    20  		if err != nil {
    21  			http.Error(w, err.Error(), http.StatusInternalServerError)
    22  			return
    23  		}
    24  		payload := make(map[string]any)
    25  		if len(entries) == 0 {
    26  			// avoid nil for the entries field in json output.
    27  			payload["entries"] = make([]*schedulerEntry, 0)
    28  		} else {
    29  			payload["entries"] = toSchedulerEntries(entries, pf)
    30  		}
    31  		if err := json.NewEncoder(w).Encode(payload); err != nil {
    32  			http.Error(w, err.Error(), http.StatusInternalServerError)
    33  			return
    34  		}
    35  	}
    36  }
    37  
    38  type listSchedulerEnqueueEventsResponse struct {
    39  	Events []*schedulerEnqueueEvent `json:"events"`
    40  }
    41  
    42  func newListSchedulerEnqueueEventsHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc {
    43  	return func(w http.ResponseWriter, r *http.Request) {
    44  		entryID := mux.Vars(r)["entry_id"]
    45  		pageSize, pageNum := getPageOptions(r)
    46  		events, err := inspector.ListSchedulerEnqueueEvents(
    47  			entryID, asynq.PageSize(pageSize), asynq.Page(pageNum))
    48  		if err != nil {
    49  			http.Error(w, err.Error(), http.StatusInternalServerError)
    50  			return
    51  		}
    52  		resp := listSchedulerEnqueueEventsResponse{
    53  			Events: toSchedulerEnqueueEvents(events),
    54  		}
    55  		if err := json.NewEncoder(w).Encode(resp); err != nil {
    56  			http.Error(w, err.Error(), http.StatusInternalServerError)
    57  			return
    58  		}
    59  	}
    60  }