66 "fmt"
77 "log"
88 "net/http"
9+ "time"
910
1011 "github.com/go-redis/redis/v8"
1112 "go-redis-queue/redisqueue"
@@ -27,6 +28,7 @@ func main() {
2728
2829 // Set up API routes
2930 http .HandleFunc ("/push" , handlePushRequest )
31+ http .HandleFunc ("/queue-stats" , handleQueueStatsRequest )
3032
3133 // Start the API server
3234 log .Fatal (http .ListenAndServe (":8080" , nil ))
@@ -63,14 +65,52 @@ func handlePushRequest(w http.ResponseWriter, r *http.Request) {
6365 json .NewEncoder (w ).Encode (response )
6466}
6567
68+ func handleQueueStatsRequest (w http.ResponseWriter , r * http.Request ) {
69+ // Create a Redis client to get the queue stats
70+ client := redis .NewClient (& redis.Options {
71+ Addr : "localhost:6379" ,
72+ Password : "" ,
73+ DB : 0 ,
74+ })
75+
76+ // Get the size of the queue
77+ queueSize , err := client .LLen (context .Background (), "queue1" ).Result ()
78+ if err != nil {
79+ http .Error (w , "Failed to get queue size" , http .StatusInternalServerError )
80+ return
81+ }
82+
83+ // Get the queue stats
84+ queueStats , err := client .LRange (context .Background (), "queue1" , 0 , - 1 ).Result ()
85+ if err != nil {
86+ http .Error (w , "Failed to get queue stats" , http .StatusInternalServerError )
87+ return
88+ }
89+
90+ // Return the queue stats as the response
91+ response := map [string ]interface {}{
92+ "queue" : "queue1" ,
93+ "size" : queueSize ,
94+ "stats" : queueStats ,
95+ }
96+ w .Header ().Set ("Content-Type" , "application/json" )
97+ json .NewEncoder (w ).Encode (response )
98+ }
99+
66100func workerFunction1 (ctx context.Context , task string ) {
67101 // Implement the worker function for queue1
68102 fmt .Printf ("Worker1 processing task from queue1: %s\n " , task )
69103 // Perform the required task processing logic for queue1
104+
105+ // Simulate some work
106+ time .Sleep (1 * time .Second )
70107}
71108
72109func workerFunction2 (ctx context.Context , task string ) {
73110 // Implement the worker function for queue2
74111 fmt .Printf ("Worker2 processing task from queue2: %s\n " , task )
75112 // Perform the required task processing logic for queue2
113+
114+ // Simulate some work
115+ time .Sleep (1 * time .Second )
76116}
0 commit comments