Skip to content

Commit f9a45c1

Browse files
committed
Added --redis command line option to choose which Redis to use
1 parent dc64d51 commit f9a45c1

4 files changed

Lines changed: 52 additions & 22 deletions

File tree

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ go install github.com/kpumuk/lazykiq@latest
2929
- `/` - filter job list (case-sensitive)
3030
- `q` - quit
3131

32+
### Redis
33+
34+
Connect to a specific Redis instance with:
35+
36+
```bash
37+
lazykiq --redis redis://localhost:6379/0
38+
```
39+
3240
## Development
3341

3442
We use [`mise`](https://mise.jdx.dev/) for development. Install tooling with:
@@ -74,4 +82,4 @@ I’d love to hear your thoughts on this project. Feel free to drop a note!
7482

7583
## License
7684

77-
[MIT](https://github.com/kpumuk/lazykiq/raw/main/LICENSE).
85+
[MIT](https://github.com/kpumuk/lazykiq/raw/main/LICENSE).

cmd/lazykiq/root.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/charmbracelet/fang"
1313
"github.com/spf13/cobra"
1414

15+
"github.com/kpumuk/lazykiq/internal/sidekiq"
1516
"github.com/kpumuk/lazykiq/internal/ui"
1617
)
1718

@@ -75,12 +76,31 @@ func init() {
7576
"help for lazykiq",
7677
)
7778

79+
rootCmd.Flags().String(
80+
"redis",
81+
"redis://localhost:6379/0",
82+
"redis URL",
83+
)
84+
7885
rootCmd.RunE = func(cmd *cobra.Command, _ []string) error {
7986
cpuprofile, err := cmd.Flags().GetString("cpuprofile")
8087
if err != nil {
8188
return fmt.Errorf("parse cpuprofile flag: %w", err)
8289
}
8390

91+
redisURL, err := cmd.Flags().GetString("redis")
92+
if err != nil {
93+
return fmt.Errorf("parse redis flag: %w", err)
94+
}
95+
96+
client, err := sidekiq.NewClient(redisURL)
97+
if err != nil {
98+
return fmt.Errorf("create redis client: %w", err)
99+
}
100+
defer func() {
101+
_ = client.Close()
102+
}()
103+
84104
var profileFile *os.File
85105
if cpuprofile != "" {
86106
file, err := os.Create(cpuprofile)
@@ -98,7 +118,7 @@ func init() {
98118
}()
99119
}
100120

101-
app := ui.New()
121+
app := ui.New(client)
102122
p := tea.NewProgram(app, tea.WithAltScreen())
103123
if _, err := p.Run(); err != nil {
104124
return fmt.Errorf("run lazykiq: %w", err)

internal/sidekiq/client.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sidekiq
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"strconv"
78
"strings"
89
"time"
@@ -59,24 +60,27 @@ type Client struct {
5960
redis *redis.Client
6061
}
6162

62-
// NewClient creates a new Sidekiq client with hardcoded Redis connection
63-
func NewClient() *Client {
64-
// Disable connection pool logging by setting MaxRetries to 0
65-
// This prevents the pool from retrying and logging errors
66-
rdb := redis.NewClient(&redis.Options{
67-
Addr: "localhost:6379",
68-
Password: "",
69-
DB: 0,
70-
MaxRetries: -1, // Disable retries completely
71-
DialTimeout: 2 * time.Second, // Short timeout to fail fast
72-
ReadTimeout: 2 * time.Second,
73-
WriteTimeout: 2 * time.Second,
74-
PoolSize: 1, // Minimal pool size
75-
})
76-
77-
return &Client{
78-
redis: rdb,
63+
// NewClient creates a new Sidekiq client configured from a Redis URL.
64+
func NewClient(redisURL string) (*Client, error) {
65+
if redisURL == "" {
66+
redisURL = "redis://localhost:6379/0"
7967
}
68+
69+
opts, err := redis.ParseURL(redisURL)
70+
if err != nil {
71+
return nil, fmt.Errorf("parse redis url: %w", err)
72+
}
73+
74+
// Disable connection pool logging by disabling retries entirely.
75+
opts.MaxRetries = -1 // Disable retries completely
76+
opts.DialTimeout = 2 * time.Second // Short timeout to fail fast
77+
opts.ReadTimeout = 2 * time.Second
78+
opts.WriteTimeout = 2 * time.Second
79+
opts.PoolSize = 1 // Minimal pool size
80+
81+
rdb := redis.NewClient(opts)
82+
83+
return &Client{redis: rdb}, nil
8084
}
8185

8286
// Close closes the Redis connection

internal/ui/app.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@ type App struct {
4040
}
4141

4242
// New creates a new App instance
43-
func New() App {
43+
func New(client *sidekiq.Client) App {
4444
styles := theme.NewStyles()
4545

46-
client := sidekiq.NewClient()
47-
4846
viewList := []views.View{
4947
views.NewDashboard(client),
5048
views.NewBusy(client),

0 commit comments

Comments
 (0)