-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds examples for the server and dequeuer at the top level. This made it clear that the JobProcessor should have a function that provides it with a reasonable default for the timeout, instead of 0. Adds NewJobProcessor to do that, and updates some tests and other code to use it. Fixes more package-level documentation.
- Loading branch information
Kevin Burke
committed
May 2, 2016
1 parent
0291244
commit db4b4a3
Showing
9 changed files
with
160 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Run the rickover dequeuer. | ||
// | ||
// All of the project defaults are used. There is one authenticated user for | ||
// basic auth, the user is "test" and the password is "hymanrickover". You will | ||
// want to copy this binary and add your own authentication scheme. | ||
|
||
package rickover | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"os/signal" | ||
"sync" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/Shyp/go-simple-metrics" | ||
"github.com/Shyp/rickover/config" | ||
"github.com/Shyp/rickover/dequeuer" | ||
"github.com/Shyp/rickover/models/db" | ||
"github.com/Shyp/rickover/services" | ||
"github.com/Shyp/rickover/setup" | ||
) | ||
|
||
func checkError(err error) { | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
func Example_dequeuer() { | ||
dbConns, err := config.GetInt("PG_WORKER_POOL_SIZE") | ||
if err != nil { | ||
log.Printf("Error getting database pool size: %s. Defaulting to 20", err) | ||
dbConns = 20 | ||
} | ||
|
||
err = setup.DB(db.DefaultConnection, dbConns) | ||
checkError(err) | ||
|
||
go setup.MeasureActiveQueries(1 * time.Second) | ||
go setup.MeasureQueueDepth(5 * time.Second) | ||
go setup.MeasureInProgressJobs(1 * time.Second) | ||
|
||
// Every minute, check for in-progress jobs that haven't been updated for | ||
// 7 minutes, and mark them as failed. | ||
go services.WatchStuckJobs(1*time.Minute, 7*time.Minute) | ||
|
||
metrics.Namespace = "rickover.dequeuer" | ||
metrics.Start("worker") | ||
|
||
downstreamUrl := config.GetURLOrBail("DOWNSTREAM_URL") | ||
downstreamPassword := os.Getenv("DOWNSTREAM_WORKER_AUTH") | ||
jp := services.NewJobProcessor(downstreamUrl.String(), downstreamPassword) | ||
|
||
// CreatePools will read all job types out of the jobs table, then start | ||
// all dequeuers for those jobs. | ||
pools, err := dequeuer.CreatePools(jp) | ||
checkError(err) | ||
|
||
sigterm := make(chan os.Signal, 1) | ||
signal.Notify(sigterm, syscall.SIGTERM) | ||
sig := <-sigterm | ||
fmt.Printf("Caught signal %v, shutting down...\n", sig) | ||
var wg sync.WaitGroup | ||
for _, p := range pools { | ||
if p != nil { | ||
wg.Add(1) | ||
go func(p *dequeuer.Pool) { | ||
err = p.Shutdown() | ||
if err != nil { | ||
log.Printf("Error shutting down pool: %s\n", err.Error()) | ||
} | ||
wg.Done() | ||
}(p) | ||
} | ||
} | ||
wg.Wait() | ||
fmt.Println("All pools shut down. Quitting.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Run the rickover server. | ||
// | ||
// All of the project defaults are used. There is one authenticated user for | ||
// basic auth, the user is "test" and the password is "hymanrickover". You will | ||
// want to copy this binary and add your own authentication scheme. | ||
package rickover | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/Shyp/go-simple-metrics" | ||
"github.com/Shyp/rickover/config" | ||
"github.com/Shyp/rickover/models/db" | ||
"github.com/Shyp/rickover/server" | ||
"github.com/Shyp/rickover/setup" | ||
"github.com/gorilla/handlers" | ||
) | ||
|
||
func Example_server() { | ||
dbConns, err := config.GetInt("PG_SERVER_POOL_SIZE") | ||
if err != nil { | ||
log.Printf("Error getting database pool size: %s. Defaulting to 10", err) | ||
dbConns = 10 | ||
} | ||
|
||
if err = setup.DB(db.DefaultConnection, dbConns); err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
metrics.Namespace = "rickover.server" | ||
metrics.Start("web") | ||
|
||
go setup.MeasureActiveQueries(5 * time.Second) | ||
|
||
// Change this user to a private value | ||
server.AddUser("test", "hymanrickover") | ||
s := server.Get(server.DefaultAuthorizer) | ||
|
||
log.Println("Listening on port 9090\n") | ||
log.Fatal(http.ListenAndServe(":9090", handlers.LoggingHandler(os.Stdout, s))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// Package rickover contains logic for a scheduler and job queue backed by | ||
// Postgres. | ||
package rickover |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// Helpers for building various types of error responses. | ||
|
||
package server | ||
|
||
import ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters