mirror of
https://github.com/hibiken/asynq.git
synced 2025-04-22 16:50:18 +08:00
Add nil check for message encoding helpers
This commit is contained in:
parent
e136e26b7e
commit
3c7327e182
@ -200,6 +200,9 @@ type TaskMessage struct {
|
|||||||
|
|
||||||
// EncodeMessage marshals the given task message and returns an encoded bytes.
|
// EncodeMessage marshals the given task message and returns an encoded bytes.
|
||||||
func EncodeMessage(msg *TaskMessage) ([]byte, error) {
|
func EncodeMessage(msg *TaskMessage) ([]byte, error) {
|
||||||
|
if msg == nil {
|
||||||
|
return nil, fmt.Errorf("cannot encode nil message")
|
||||||
|
}
|
||||||
payload, err := json.Marshal(msg.Payload)
|
payload, err := json.Marshal(msg.Payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -229,16 +232,16 @@ func DecodeMessage(data []byte) (*TaskMessage, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &TaskMessage{
|
return &TaskMessage{
|
||||||
Type: pbmsg.Type,
|
Type: pbmsg.GetType(),
|
||||||
Payload: payload,
|
Payload: payload,
|
||||||
ID: uuid.MustParse(pbmsg.Id),
|
ID: uuid.MustParse(pbmsg.GetId()),
|
||||||
Queue: pbmsg.Queue,
|
Queue: pbmsg.GetQueue(),
|
||||||
Retry: int(pbmsg.Retry),
|
Retry: int(pbmsg.GetRetry()),
|
||||||
Retried: int(pbmsg.Retried),
|
Retried: int(pbmsg.GetRetried()),
|
||||||
ErrorMsg: pbmsg.ErrorMsg,
|
ErrorMsg: pbmsg.GetErrorMsg(),
|
||||||
Timeout: pbmsg.Timeout,
|
Timeout: pbmsg.GetTimeout(),
|
||||||
Deadline: pbmsg.Deadline,
|
Deadline: pbmsg.GetDeadline(),
|
||||||
UniqueKey: pbmsg.UniqueKey,
|
UniqueKey: pbmsg.GetUniqueKey(),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -322,11 +325,14 @@ type ServerInfo struct {
|
|||||||
|
|
||||||
// EncodeServerInfo marshals the given ServerInfo and returns the encoded bytes.
|
// EncodeServerInfo marshals the given ServerInfo and returns the encoded bytes.
|
||||||
func EncodeServerInfo(info *ServerInfo) ([]byte, error) {
|
func EncodeServerInfo(info *ServerInfo) ([]byte, error) {
|
||||||
|
if info == nil {
|
||||||
|
return nil, fmt.Errorf("cannot encode nil server info")
|
||||||
|
}
|
||||||
queues := make(map[string]int32)
|
queues := make(map[string]int32)
|
||||||
for q, p := range info.Queues {
|
for q, p := range info.Queues {
|
||||||
queues[q] = int32(p)
|
queues[q] = int32(p)
|
||||||
}
|
}
|
||||||
ts, err := ptypes.TimestampProto(info.Started)
|
started, err := ptypes.TimestampProto(info.Started)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -338,7 +344,7 @@ func EncodeServerInfo(info *ServerInfo) ([]byte, error) {
|
|||||||
Queues: queues,
|
Queues: queues,
|
||||||
StrictPriority: info.StrictPriority,
|
StrictPriority: info.StrictPriority,
|
||||||
Status: info.Status,
|
Status: info.Status,
|
||||||
StartTime: ts,
|
StartTime: started,
|
||||||
ActiveWorkerCount: int32(info.ActiveWorkerCount),
|
ActiveWorkerCount: int32(info.ActiveWorkerCount),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -358,15 +364,15 @@ func DecodeServerInfo(b []byte) (*ServerInfo, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &ServerInfo{
|
return &ServerInfo{
|
||||||
Host: pbmsg.Host,
|
Host: pbmsg.GetHost(),
|
||||||
PID: int(pbmsg.Pid),
|
PID: int(pbmsg.GetPid()),
|
||||||
ServerID: pbmsg.ServerId,
|
ServerID: pbmsg.GetServerId(),
|
||||||
Concurrency: int(pbmsg.Concurrency),
|
Concurrency: int(pbmsg.GetConcurrency()),
|
||||||
Queues: queues,
|
Queues: queues,
|
||||||
StrictPriority: pbmsg.StrictPriority,
|
StrictPriority: pbmsg.GetStrictPriority(),
|
||||||
Status: pbmsg.Status,
|
Status: pbmsg.GetStatus(),
|
||||||
Started: startTime,
|
Started: startTime,
|
||||||
ActiveWorkerCount: int(pbmsg.ActiveWorkerCount),
|
ActiveWorkerCount: int(pbmsg.GetActiveWorkerCount()),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -386,6 +392,9 @@ type WorkerInfo struct {
|
|||||||
|
|
||||||
// EncodeWorkerInfo marshals the given WorkerInfo and returns the encoded bytes.
|
// EncodeWorkerInfo marshals the given WorkerInfo and returns the encoded bytes.
|
||||||
func EncodeWorkerInfo(info *WorkerInfo) ([]byte, error) {
|
func EncodeWorkerInfo(info *WorkerInfo) ([]byte, error) {
|
||||||
|
if info == nil {
|
||||||
|
return nil, fmt.Errorf("cannot encode nil worker info")
|
||||||
|
}
|
||||||
payload, err := json.Marshal(info.Payload)
|
payload, err := json.Marshal(info.Payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -440,13 +449,13 @@ func DecodeWorkerInfo(b []byte) (*WorkerInfo, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &WorkerInfo{
|
return &WorkerInfo{
|
||||||
Host: pbmsg.Host,
|
Host: pbmsg.GetHost(),
|
||||||
PID: int(pbmsg.Pid),
|
PID: int(pbmsg.GetPid()),
|
||||||
ServerID: pbmsg.ServerId,
|
ServerID: pbmsg.GetServerId(),
|
||||||
ID: pbmsg.TaskId,
|
ID: pbmsg.GetTaskId(),
|
||||||
Type: pbmsg.TaskType,
|
Type: pbmsg.GetTaskType(),
|
||||||
Payload: payload,
|
Payload: payload,
|
||||||
Queue: pbmsg.Queue,
|
Queue: pbmsg.GetQueue(),
|
||||||
Started: startTime,
|
Started: startTime,
|
||||||
Deadline: deadline,
|
Deadline: deadline,
|
||||||
}, nil
|
}, nil
|
||||||
@ -479,6 +488,9 @@ type SchedulerEntry struct {
|
|||||||
|
|
||||||
// EncodeSchedulerEntry marshals the given entry and returns an encoded bytes.
|
// EncodeSchedulerEntry marshals the given entry and returns an encoded bytes.
|
||||||
func EncodeSchedulerEntry(entry *SchedulerEntry) ([]byte, error) {
|
func EncodeSchedulerEntry(entry *SchedulerEntry) ([]byte, error) {
|
||||||
|
if entry == nil {
|
||||||
|
return nil, fmt.Errorf("cannot encode nil scheduler entry")
|
||||||
|
}
|
||||||
payload, err := json.Marshal(entry.Payload)
|
payload, err := json.Marshal(entry.Payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -521,11 +533,11 @@ func DecodeSchedulerEntry(b []byte) (*SchedulerEntry, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &SchedulerEntry{
|
return &SchedulerEntry{
|
||||||
ID: pbmsg.Id,
|
ID: pbmsg.GetId(),
|
||||||
Spec: pbmsg.Spec,
|
Spec: pbmsg.GetSpec(),
|
||||||
Type: pbmsg.TaskType,
|
Type: pbmsg.GetTaskType(),
|
||||||
Payload: payload,
|
Payload: payload,
|
||||||
Opts: pbmsg.EnqueueOptions,
|
Opts: pbmsg.GetEnqueueOptions(),
|
||||||
Next: next,
|
Next: next,
|
||||||
Prev: prev,
|
Prev: prev,
|
||||||
}, nil
|
}, nil
|
||||||
@ -543,6 +555,9 @@ type SchedulerEnqueueEvent struct {
|
|||||||
// EncodeSchedulerEnqueueEvent marshals the given event
|
// EncodeSchedulerEnqueueEvent marshals the given event
|
||||||
// and returns an encoded bytes.
|
// and returns an encoded bytes.
|
||||||
func EncodeSchedulerEnqueueEvent(event *SchedulerEnqueueEvent) ([]byte, error) {
|
func EncodeSchedulerEnqueueEvent(event *SchedulerEnqueueEvent) ([]byte, error) {
|
||||||
|
if event == nil {
|
||||||
|
return nil, fmt.Errorf("cannot encode nil enqueue event")
|
||||||
|
}
|
||||||
enqueuedAt, err := ptypes.TimestampProto(event.EnqueuedAt)
|
enqueuedAt, err := ptypes.TimestampProto(event.EnqueuedAt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -565,7 +580,7 @@ func DecodeSchedulerEnqueueEvent(b []byte) (*SchedulerEnqueueEvent, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &SchedulerEnqueueEvent{
|
return &SchedulerEnqueueEvent{
|
||||||
TaskID: pbmsg.TaskId,
|
TaskID: pbmsg.GetTaskId(),
|
||||||
EnqueuedAt: enqueuedAt,
|
EnqueuedAt: enqueuedAt,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user