Show started time in ActiveTasksTable

This commit is contained in:
Ken Hibino 2021-01-23 12:06:50 -08:00
parent 1eafcbeed5
commit c08addc7b3
4 changed files with 61 additions and 19 deletions

View File

@ -89,6 +89,13 @@ type BaseTask struct {
type ActiveTask struct { type ActiveTask struct {
*BaseTask *BaseTask
// Started time indicates when a worker started working on ths task.
//
// Value is either time formatted in RFC3339 format, or "-" which indicates
// a worker started working on the task only a few moments ago, and started time
// data is not available.
Started string `json:"start_time"`
} }
func toActiveTask(t *asynq.ActiveTask) *ActiveTask { func toActiveTask(t *asynq.ActiveTask) *ActiveTask {
@ -98,7 +105,7 @@ func toActiveTask(t *asynq.ActiveTask) *ActiveTask {
Payload: t.Payload, Payload: t.Payload,
Queue: t.Queue, Queue: t.Queue,
} }
return &ActiveTask{base} return &ActiveTask{BaseTask: base}
} }
func toActiveTasks(in []*asynq.ActiveTask) []*ActiveTask { func toActiveTasks(in []*asynq.ActiveTask) []*ActiveTask {

View File

@ -5,6 +5,7 @@ import (
"log" "log"
"net/http" "net/http"
"strconv" "strconv"
"time"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/hibiken/asynq" "github.com/hibiken/asynq"
@ -15,11 +16,17 @@ import (
// - http.Handler(s) for task related endpoints // - http.Handler(s) for task related endpoints
// **************************************************************************** // ****************************************************************************
type ListActiveTasksResponse struct {
Tasks []*ActiveTask `json:"tasks"`
Stats *QueueStateSnapshot `json:"stats"`
}
func newListActiveTasksHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc { func newListActiveTasksHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
qname := vars["qname"] qname := vars["qname"]
pageSize, pageNum := getPageOptions(r) pageSize, pageNum := getPageOptions(r)
tasks, err := inspector.ListActiveTasks( tasks, err := inspector.ListActiveTasks(
qname, asynq.PageSize(pageSize), asynq.Page(pageNum)) qname, asynq.PageSize(pageSize), asynq.Page(pageNum))
if err != nil { if err != nil {
@ -31,15 +38,35 @@ func newListActiveTasksHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
payload := make(map[string]interface{}) servers, err := inspector.Servers()
if len(tasks) == 0 { if err != nil {
// avoid nil for the tasks field in json output. http.Error(w, err.Error(), http.StatusInternalServerError)
payload["tasks"] = make([]*ActiveTask, 0) return
} else {
payload["tasks"] = toActiveTasks(tasks)
} }
payload["stats"] = toQueueStateSnapshot(stats) // m maps taskID to started time.
if err := json.NewEncoder(w).Encode(payload); err != nil { m := make(map[string]time.Time)
for _, srv := range servers {
for _, w := range srv.ActiveWorkers {
if w.Task.Queue == qname {
m[w.Task.ID] = w.Started
}
}
}
activeTasks := toActiveTasks(tasks)
for _, t := range activeTasks {
started, ok := m[t.ID]
if ok {
t.Started = started.Format(time.RFC3339)
} else {
t.Started = "-"
}
}
resp := ListActiveTasksResponse{
Tasks: activeTasks,
Stats: toQueueStateSnapshot(stats),
}
if err := json.NewEncoder(w).Encode(resp); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }

View File

@ -243,6 +243,7 @@ interface BaseTask {
export interface ActiveTask extends BaseTask { export interface ActiveTask extends BaseTask {
id: string; id: string;
queue: string; queue: string;
start_time: string;
} }
export interface PendingTask extends BaseTask { export interface PendingTask extends BaseTask {

View File

@ -37,7 +37,7 @@ import TablePaginationActions, {
import TableActions from "./TableActions"; import TableActions from "./TableActions";
import { usePolling } from "../hooks"; import { usePolling } from "../hooks";
import { ActiveTaskExtended } from "../reducers/tasksReducer"; import { ActiveTaskExtended } from "../reducers/tasksReducer";
import { uuidPrefix } from "../utils"; import { timeAgo, uuidPrefix } from "../utils";
import { TableColumn } from "../types/table"; import { TableColumn } from "../types/table";
const useStyles = makeStyles((theme) => ({ const useStyles = makeStyles((theme) => ({
@ -66,6 +66,15 @@ const mapDispatchToProps = {
cancelAllActiveTasksAsync, cancelAllActiveTasksAsync,
}; };
const columns: TableColumn[] = [
{ key: "icon", label: "", align: "left" },
{ key: "id", label: "ID", align: "left" },
{ key: "type", label: "Type", align: "left" },
{ key: "status", label: "Status", align: "left" },
{ key: "start-time", label: "Started", align: "left" },
{ key: "actions", label: "Actions", align: "center" },
];
const connector = connect(mapStateToProps, mapDispatchToProps); const connector = connect(mapStateToProps, mapDispatchToProps);
type ReduxProps = ConnectedProps<typeof connector>; type ReduxProps = ConnectedProps<typeof connector>;
@ -131,14 +140,6 @@ function ActiveTasksTable(props: Props & ReduxProps) {
); );
} }
const columns: TableColumn[] = [
{ key: "icon", label: "", align: "left" },
{ key: "id", label: "ID", align: "left" },
{ key: "type", label: "Type", align: "left" },
{ key: "status", label: "Status", align: "left" },
{ key: "actions", label: "Actions", align: "center" },
];
const rowCount = props.tasks.length; const rowCount = props.tasks.length;
const numSelected = selectedIds.length; const numSelected = selectedIds.length;
return ( return (
@ -294,6 +295,9 @@ function Row(props: RowProps) {
</TableCell> </TableCell>
<TableCell>{task.type}</TableCell> <TableCell>{task.type}</TableCell>
<TableCell>{task.canceling ? "Canceling" : "Running"}</TableCell> <TableCell>{task.canceling ? "Canceling" : "Running"}</TableCell>
<TableCell>
{task.start_time === "-" ? "just now" : timeAgo(task.start_time)}
</TableCell>
<TableCell <TableCell
align="center" align="center"
onMouseEnter={props.onActionCellEnter} onMouseEnter={props.onActionCellEnter}
@ -319,7 +323,10 @@ function Row(props: RowProps) {
</TableCell> </TableCell>
</TableRow> </TableRow>
<TableRow selected={props.isSelected}> <TableRow selected={props.isSelected}>
<TableCell style={{ paddingBottom: 0, paddingTop: 0 }} colSpan={6}> <TableCell
style={{ paddingBottom: 0, paddingTop: 0 }}
colSpan={columns.length + 1}
>
<Collapse in={open} timeout="auto" unmountOnExit> <Collapse in={open} timeout="auto" unmountOnExit>
<Box margin={1}> <Box margin={1}>
<Typography variant="h6" gutterBottom component="div"> <Typography variant="h6" gutterBottom component="div">