Skip to content

Commit

Permalink
feat: Expose web ui router for external server, and switch to *http.S…
Browse files Browse the repository at this point in the history
…erveMux from gocraft/web (#44)

feat: Expose web ui router for external server, and switch to *http.ServeMux from gocraft/web

- Update minimum supported golang version to 1.22
- Use explicit relative path on webui
- Cleanup & refactoring

---------

Co-authored-by: Anmol Chopra <anmol.chopra@gojek.com>
  • Loading branch information
vin-rmdn and chopraanmol1 authored Oct 3, 2024
1 parent 53d536c commit ad0f5c1
Show file tree
Hide file tree
Showing 13 changed files with 814 additions and 332 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [1.18.x, 1.19.x, 1.20.x, 1.21.x]
go-version: [1.22.x, 1.23.x]
steps:
- name: Install Go@v${{ matrix.go-version }}
uses: actions/setup-go@v2
Expand Down
4 changes: 2 additions & 2 deletions dead_pool_reaper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestDeadPoolReaper(t *testing.T) {

// Without this, the assertion for deadPools fails on GitHub CI. Could have
// something to do with in-memory redis instance.
time.Sleep(10*time.Millisecond)
time.Sleep(10 * time.Millisecond)

// Test getting dead pool
reaper := newDeadPoolReaper(ns, pool, []string{})
Expand Down Expand Up @@ -215,7 +215,7 @@ func TestDeadPoolReaperNoJobTypes(t *testing.T) {

// Without this, the assertion for deadPools fails on GitHub CI. Could have
// something to do with in-memory redis instance.
time.Sleep(10*time.Millisecond)
time.Sleep(10 * time.Millisecond)

// Test getting dead pool
reaper := newDeadPoolReaper(ns, pool, []string{})
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
module github.com/gojek/work

go 1.18
go 1.22

require (
github.com/alicebob/miniredis/v2 v2.14.3
github.com/braintree/manners v0.0.0-20160418043613-82a8879fc5fd
github.com/gocraft/web v0.0.0-20190207150652-9707327fb69b
github.com/gomodule/redigo v1.8.9
github.com/rafaeljusto/redigomock v2.4.0+incompatible
github.com/robfig/cron/v3 v3.0.1
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5P
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gocraft/web v0.0.0-20190207150652-9707327fb69b h1:g2Qcs0B+vOQE1L3a7WQ/JUUSzJnHbTz14qkJSqEWcF4=
github.com/gocraft/web v0.0.0-20190207150652-9707327fb69b/go.mod h1:Ag7UMbZNGrnHwaXPJOUKJIVgx4QOWMOWZngrvsN6qak=
github.com/gomodule/redigo v1.8.9 h1:Sl3u+2BI/kk+VEatbj0scLdrFhjPmbxOc1myhDP41ws=
github.com/gomodule/redigo v1.8.9/go.mod h1:7ArFNvsTjH8GMMzB4uy1snslv2BwmginuMs06a1uzZE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
9 changes: 5 additions & 4 deletions priority_sampler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ func (s *prioritySampler) add(priority uint, redisJobs, redisJobsInProg, redisJo
// sample re-sorts s.samples, modifying it in-place. Higher weighted things will tend to go towards the beginning.
// NOTE: as written currently makes 0 allocations.
// NOTE2: this is an O(n^2 algorithm) that is:
// 5492ns for 50 jobs (50 is a large number of unique jobs in my experience)
// 54966ns for 200 jobs
// ~1ms for 1000 jobs
// ~4ms for 2000 jobs
//
// 5492ns for 50 jobs (50 is a large number of unique jobs in my experience)
// 54966ns for 200 jobs
// ~1ms for 1000 jobs
// ~4ms for 2000 jobs
func (s *prioritySampler) sample() []sampleItem {
lenSamples := len(s.samples)
remaining := lenSamples
Expand Down
35 changes: 35 additions & 0 deletions webui/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package webui

import (
"net/http"

"github.com/gojek/work"
)

// NewHandler return *http.ServeMux for the given work.Client.
// The web UI rely on relative path, so http.Handler should be mounted on a path with trailing `/`.
// For example, if you want to mount the web UI on `/workerui`, you should use:
// ```
// handler := webui.NewHandler(client)
// mux.Handle("/workerui/", http.StripPrefix("/workerui", handler))
// ```
func NewHandler(client *work.Client) *http.ServeMux {
ctx := context{client: client}

mux := http.NewServeMux()
mux.HandleFunc("GET /ping", ctx.ping)
mux.HandleFunc("GET /queues", ctx.queues)
mux.HandleFunc("GET /worker_pools", ctx.workerPools)
mux.HandleFunc("GET /busy_workers", ctx.busyWorkers)
mux.HandleFunc("GET /retry_jobs", ctx.retryJobs)
mux.HandleFunc("GET /scheduled_jobs", ctx.scheduledJobs)
mux.HandleFunc("GET /dead_jobs", ctx.deadJobs)
mux.HandleFunc("POST /delete_dead_job/{died_at}/{job_id}", ctx.deleteDeadJob)
mux.HandleFunc("POST /retry_dead_job/{died_at}/{job_id}", ctx.retryDeadJob)
mux.HandleFunc("POST /delete_all_dead_jobs", ctx.deleteAllDeadJobs)
mux.HandleFunc("POST /retry_all_dead_jobs", ctx.retryAllDeadJobs)
mux.HandleFunc("GET /", ctx.indexPage)
mux.HandleFunc("GET /work.js", ctx.workJS)

return mux
}
Loading

0 comments on commit ad0f5c1

Please sign in to comment.