Skip to content

Commit

Permalink
Update models.go
Browse files Browse the repository at this point in the history
1) I've renamed things to more descriptive names and added clear explanations for others to contribute efficiently.

2) I used the time object for dates instead of a text string which helps prevent errors and makes the code more reliable.

3) I've also added the NewJobRequest function which saves time and ensures that everything is set up properly, so one doesn’t have to repeat the same setup every time.

4) Made the JSON output become less cluttered and more user-friendly.
  • Loading branch information
AMANSINGH1674 authored Dec 9, 2024
1 parent 6aaa016 commit 967c244
Showing 1 changed file with 55 additions and 35 deletions.
90 changes: 55 additions & 35 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,60 +4,80 @@ import (
"time"
)

// JobReq represents a job request.
type JobReq struct {
TaskName string `json:"task"`
JobID string `json:"job_id"`
Queue string `json:"queue"`
ETA string `json:"eta"`
Retries int `json:"retries"`
TTL int `json:"ttl"`
Args []string `json:"args"`
DB string `json:"db"`

ttlDuration time.Duration
}

// JobResp is the response sent to a job request.
type JobResp struct {
// JobRequest represents a job request with metadata and execution details.
type JobRequest struct {
TaskName string `json:"task"`
JobID string `json:"job_id"`
Queue string `json:"queue"`
ETA *time.Time `json:"eta"`
Retries int `json:"retries"`
TTL int `json:"ttl"`
Args []string `json:"args"`
DB string `json:"db"`
TTLDuration time.Duration `json:"-"` // Internal field, not serialized to JSON
}

// JobResponse represents the response sent after processing a job.
type JobResponse struct {
JobID string `json:"job_id"`
TaskName string `json:"task"`
Queue string `json:"queue"`
ETA *time.Time `json:"eta"`
Retries int `json:"retries"`
}

// JobStatusResp represents the response of a single job.
type JobStatusResp struct {
// JobStatusResponse represents the status of a single job.
type JobStatusResponse struct {
JobID string `json:"job_id"`
State string `json:"state"`
Count int `json:"count"`
Error string `json:"error"`
Error string `json:"error,omitempty"`
}

// GroupReq represents a Jobrequest group.
type GroupReq struct {
GroupID string `json:"group_id"`
Jobs []JobReq `json:"jobs"`
// GroupRequest represents a group of job requests.
type GroupRequest struct {
GroupID string `json:"group_id"`
Jobs []JobRequest `json:"jobs"`
}

// GroupResp is the response sent to a group job request.
type GroupResp struct {
GroupID string `json:"group_id"`
Jobs []JobResp `json:"jobs"`
// GroupResponse represents the response for a group job request.
type GroupResponse struct {
GroupID string `json:"group_id"`
Jobs []JobResponse `json:"jobs"`
}

// GroupStatusResp represents the status of a group job.
type GroupStatusResp struct {
GroupID string `json:"group_id"`
State string `json:"state"`
Jobs []JobStatusResp `json:"jobs"`
// GroupStatusResponse represents the status of a group job.
type GroupStatusResponse struct {
GroupID string `json:"group_id"`
State string `json:"state"`
Jobs []JobStatusResponse `json:"jobs"`
}

// HTTPResp represents a container for generic outgoing HTTP
// responses.
type HTTPResp struct {
// HTTPResponse represents a container for generic outgoing HTTP responses.
type HTTPResponse struct {
Status string `json:"status"`
Message string `json:"message,omitempty"`
Data interface{} `json:"data"`
}

// NewJobRequest creates a new JobRequest with proper initialization.
func NewJobRequest(taskName, jobID, queue, db string, eta *time.Time, retries, ttl int, args []string) JobRequest {
return JobRequest{
TaskName: taskName,
JobID: jobID,
Queue: queue,
ETA: eta,
Retries: retries,
TTL: ttl,
Args: args,
DB: db,
}
}

// NewGroupRequest creates a new GroupRequest with proper initialization.
func NewGroupRequest(groupID string, jobs []JobRequest) GroupRequest {
return GroupRequest{
GroupID: groupID,
Jobs: jobs,
}
}

0 comments on commit 967c244

Please sign in to comment.