本文整理匯總了Golang中github.com/flynn/flynn/pkg/postgres.Wait函數的典型用法代碼示例。如果您正苦於以下問題:Golang Wait函數的具體用法?Golang Wait怎麽用?Golang Wait使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Wait函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: runServer
func runServer(_ *docopt.Args) error {
addr := ":" + os.Getenv("PORT")
db := postgres.Wait(nil, nil)
if err := dbMigrations.Migrate(db); err != nil {
return fmt.Errorf("error running DB migrations: %s", err)
}
mux := http.NewServeMux()
repo, err := data.NewFileRepoFromEnv(db)
if err != nil {
return err
}
hb, err := discoverd.AddServiceAndRegister("blobstore", addr)
if err != nil {
return err
}
shutdown.BeforeExit(func() { hb.Close() })
log.Println("Blobstore serving files on " + addr)
mux.Handle("/", handler(repo))
mux.Handle(status.Path, status.Handler(func() status.Status {
if err := db.Exec("SELECT 1"); err != nil {
return status.Unhealthy
}
return status.Healthy
}))
h := httphelper.ContextInjector("blobstore", httphelper.NewRequestLogger(mux))
return http.ListenAndServe(addr, h)
}
示例2: main
func main() {
defer shutdown.Exit()
db := postgres.Wait(&postgres.Conf{
Service: serviceName,
User: "flynn",
Password: os.Getenv("PGPASSWORD"),
Database: "postgres",
}, nil)
api := &pgAPI{db}
router := httprouter.New()
router.POST("/databases", httphelper.WrapHandler(api.createDatabase))
router.DELETE("/databases", httphelper.WrapHandler(api.dropDatabase))
router.GET("/ping", httphelper.WrapHandler(api.ping))
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
addr := ":" + port
hb, err := discoverd.AddServiceAndRegister(serviceName+"-api", addr)
if err != nil {
shutdown.Fatal(err)
}
shutdown.BeforeExit(func() { hb.Close() })
handler := httphelper.ContextInjector(serviceName+"-api", httphelper.NewRequestLogger(router))
shutdown.Fatal(http.ListenAndServe(addr, handler))
}
示例3: main
func main() {
log := logger.New("fn", "main")
log.Info("creating controller client")
client, err := controller.NewClient("", os.Getenv("AUTH_KEY"))
if err != nil {
log.Error("error creating controller client", "err", err)
shutdown.Fatal(err)
}
log.Info("connecting to postgres")
db := postgres.Wait(nil, schema.PrepareStatements)
shutdown.BeforeExit(func() { db.Close() })
go func() {
status.AddHandler(func() status.Status {
_, err := db.ConnPool.Exec("ping")
if err != nil {
return status.Unhealthy
}
return status.Healthy
})
addr := ":" + os.Getenv("PORT")
hb, err := discoverd.AddServiceAndRegister("controller-worker", addr)
if err != nil {
shutdown.Fatal(err)
}
shutdown.BeforeExit(func() { hb.Close() })
shutdown.Fatal(http.ListenAndServe(addr, nil))
}()
workers := que.NewWorkerPool(
que.NewClient(db.ConnPool),
que.WorkMap{
"deployment": deployment.JobHandler(db, client, logger),
"app_deletion": app_deletion.JobHandler(db, client, logger),
"domain_migration": domain_migration.JobHandler(db, client, logger),
"release_cleanup": release_cleanup.JobHandler(db, client, logger),
"app_garbage_collection": app_garbage_collection.JobHandler(db, client, logger),
},
workerCount,
)
workers.Interval = 5 * time.Second
log.Info("starting workers", "count", workerCount, "interval", workers.Interval)
workers.Start()
shutdown.BeforeExit(func() { workers.Shutdown() })
select {} // block and keep running
}
示例4: runMigrate
func runMigrate(args *docopt.Args) error {
deleteFiles := args.Bool["--delete"]
concurrency, err := strconv.Atoi(args.String["--concurrency"])
if err != nil {
return err
}
if concurrency < 1 {
concurrency = 4
}
prefix := args.String["--prefix"]
db := postgres.Wait(nil, nil)
repo, err := data.NewFileRepoFromEnv(db)
if err != nil {
return nil
}
files, err := repo.ListFilesExcludingDefaultBackend(prefix)
if err != nil {
return nil
}
var wg sync.WaitGroup
tokens := make(chan struct{}, concurrency)
var errorCount int64
dest := repo.DefaultBackend().Name()
for i, f := range files {
log.Printf("[%d/%d] Moving %s (%s, %d bytes) from %s to %s", i+1, len(files), f.FileInfo.Name, f.ID, f.Size, f.Backend.Name(), dest)
tokens <- struct{}{}
wg.Add(1)
go func(f data.BackendFile) {
if err := moveFile(db, repo, f, deleteFiles); err != nil {
log.Printf("Error moving %s (%s): %s", f.FileInfo.Name, f.ID, err)
atomic.AddInt64(&errorCount, 1)
}
<-tokens
wg.Done()
}(f)
}
wg.Wait()
db.Close()
if errorCount > 0 {
return fmt.Errorf("Finished with %d errors", errorCount)
}
log.Printf("Finished with no errors.")
return nil
}
示例5: main
func main() {
log := logger.New("fn", "main")
log.Info("creating controller client")
client, err := controller.NewClient("", os.Getenv("AUTH_KEY"))
if err != nil {
log.Error("error creating controller client", "err", err)
shutdown.Fatal()
}
log.Info("connecting to postgres")
db := postgres.Wait("", "")
log.Info("creating postgres connection pool")
pgxpool, err := pgx.NewConnPool(pgx.ConnPoolConfig{
ConnConfig: pgx.ConnConfig{
Host: os.Getenv("PGHOST"),
User: os.Getenv("PGUSER"),
Password: os.Getenv("PGPASSWORD"),
Database: os.Getenv("PGDATABASE"),
},
AfterConnect: que.PrepareStatements,
MaxConnections: workerCount,
})
if err != nil {
log.Error("error creating postgres connection pool", "err", err)
shutdown.Fatal()
}
shutdown.BeforeExit(func() { pgxpool.Close() })
workers := que.NewWorkerPool(
que.NewClient(pgxpool),
que.WorkMap{
"deployment": deployment.JobHandler(db, client, logger),
"app_deletion": app_deletion.JobHandler(db, client, logger),
},
workerCount,
)
workers.Interval = 5 * time.Second
log.Info("starting workers", "count", workerCount, "interval", workers.Interval)
workers.Start()
shutdown.BeforeExit(func() { workers.Shutdown() })
select {} // block and keep running
}
示例6: migrate
func migrate() error {
db := postgres.Wait(nil, nil)
slugbuilder, err := getSlugbuilderArtifact(db)
if err != nil {
log.Printf("error getting slugbuilder artifact: %s", err)
return err
}
artifacts, err := getActiveSlugArtifacts(db)
if err != nil {
log.Printf("error getting active slug artifacts: %s", err)
return err
}
log.Printf("converting %d active slugs to Flynn images", len(artifacts))
for i, artifact := range artifacts {
log.Printf("converting slug %s (%d/%d)", artifact.ID, i+1, len(artifacts))
newID, err := convert(slugbuilder, artifact.URI)
if err != nil {
if err == ErrNotFound {
log.Printf("skipping slug %s (%d/%d): slug no longer exists", artifact.ID, i+1, len(artifacts))
continue
}
log.Printf("error converting slug %s (%d/%d): %s", artifact.ID, i+1, len(artifacts), err)
return err
}
tx, err := db.Begin()
if err != nil {
return err
}
if err := tx.Exec(`UPDATE release_artifacts SET artifact_id = $1 WHERE artifact_id = $2`, newID, artifact.ID); err != nil {
tx.Rollback()
return err
}
if err := tx.Exec(`UPDATE artifacts SET deleted_at = now() WHERE artifact_id = $1`, artifact.ID); err != nil {
tx.Rollback()
return err
}
if err := tx.Commit(); err != nil {
return err
}
}
return nil
}
示例7: main
func main() {
deleteFiles := flag.Bool("delete", false, "enable deletion of files from source backend")
concurrency := flag.Int("concurrency", 4, "number of parallel file moves to run at a time")
prefix := flag.String("prefix", "", "only migrate files with a name that starts with this prefix")
flag.Parse()
db := postgres.Wait(nil, nil)
repo, err := data.NewFileRepoFromEnv(db)
if err != nil {
log.Fatal(err)
}
files, err := repo.ListFilesExcludingDefaultBackend(*prefix)
if err != nil {
log.Fatal(err)
}
var wg sync.WaitGroup
tokens := make(chan struct{}, *concurrency)
var errorCount int64
dest := repo.DefaultBackend().Name()
for i, f := range files {
log.Printf("[%d/%d] Moving %s (%s, %d bytes) from %s to %s", i+1, len(files), f.FileInfo.Name, f.ID, f.Size, f.Backend.Name(), dest)
tokens <- struct{}{}
wg.Add(1)
go func(f data.BackendFile) {
if err := moveFile(db, repo, f, *deleteFiles); err != nil {
log.Printf("Error moving %s (%s): %s", f.FileInfo.Name, f.ID, err)
atomic.AddInt64(&errorCount, 1)
}
<-tokens
wg.Done()
}(f)
}
wg.Wait()
db.Close()
if errorCount > 0 {
log.Printf("Finished with %d errors", errorCount)
os.Exit(1)
} else {
log.Printf("Finished with no errors.")
}
}
示例8: runCleanup
func runCleanup(args *docopt.Args) error {
concurrency, err := strconv.Atoi(args.String["--concurrency"])
if err != nil {
return err
}
if concurrency < 1 {
concurrency = 4
}
db := postgres.Wait(nil, nil)
repo, err := data.NewFileRepoFromEnv(db)
if err != nil {
return err
}
files, err := repo.ListDeletedFilesForCleanup()
if err != nil {
return err
}
var wg sync.WaitGroup
tokens := make(chan struct{}, concurrency)
for i, f := range files {
if f.Backend == nil {
log.Printf("[%d/%d] Skipping %s (%s) because backend is not configured", i+1, len(files), f.FileInfo.Name, f.ID)
continue
}
tokens <- struct{}{}
wg.Add(1)
go func(f data.BackendFile) {
if err := f.Backend.Delete(nil, f.FileInfo); err == nil {
log.Printf("[%d/%d] Successfully deleted %s (%s) from backend %s", i+1, len(files), f.FileInfo.Name, f.ID, f.Backend.Name())
}
<-tokens
wg.Done()
}(f)
}
wg.Wait()
db.Close()
log.Printf("Done.")
return nil
}
示例9: main
func main() {
defer shutdown.Exit()
flag.Parse()
addr := os.Getenv("PORT")
if addr == "" {
addr = *listenPort
}
addr = ":" + addr
var fs Filesystem
var storageDesc string
if *storageDir != "" {
fs = NewOSFilesystem(*storageDir)
storageDesc = *storageDir
} else {
var err error
db := postgres.Wait(nil, nil)
fs, err = NewPostgresFilesystem(db)
if err != nil {
shutdown.Fatal(err)
}
storageDesc = "Postgres"
}
if *serviceDiscovery {
hb, err := discoverd.AddServiceAndRegister("blobstore", addr)
if err != nil {
shutdown.Fatal(err)
}
shutdown.BeforeExit(func() { hb.Close() })
}
log.Println("Blobstore serving files on " + addr + " from " + storageDesc)
mux := http.NewServeMux()
mux.Handle("/", handler(fs))
mux.Handle(status.Path, status.Handler(fs.Status))
h := httphelper.ContextInjector("blobstore", httphelper.NewRequestLogger(mux))
shutdown.Fatal(http.ListenAndServe(addr, h))
}
示例10: main
func main() {
concurrency := flag.Int("concurrency", 4, "number of parallel file deletions to run at a time")
flag.Parse()
db := postgres.Wait(nil, nil)
repo, err := data.NewFileRepoFromEnv(db)
if err != nil {
log.Fatal(err)
}
files, err := repo.ListDeletedFilesForCleanup()
if err != nil {
log.Fatal(err)
}
var wg sync.WaitGroup
tokens := make(chan struct{}, *concurrency)
for i, f := range files {
if f.Backend == nil {
log.Printf("[%d/%d] Skipping %s (%s) because backend is not configured", i+1, len(files), f.FileInfo.Name, f.ID)
continue
}
tokens <- struct{}{}
wg.Add(1)
go func(f data.BackendFile) {
if err := f.Backend.Delete(nil, f.FileInfo); err == nil {
log.Printf("[%d/%d] Successfully deleted %s (%s) from backend %s", i+1, len(files), f.FileInfo.Name, f.ID, f.Backend.Name())
}
<-tokens
wg.Done()
}(f)
}
wg.Wait()
db.Close()
log.Printf("Done.")
}
示例11: migrate
func migrate() error {
db := postgres.Wait(nil, nil)
artifacts, err := getActiveImageArtifacts(db)
if err != nil {
log.Printf("error getting active Docker image artifacts: %s", err)
return err
}
log.Printf("converting %d active Docker images to Flynn images", len(artifacts))
for i, artifact := range artifacts {
log.Printf("converting Docker image %s (%d/%d)", artifact.ID, i+1, len(artifacts))
newID, err := convert(artifact.URI)
if err != nil {
log.Printf("error converting Docker image %s (%d/%d): %s", artifact.ID, i+1, len(artifacts), err)
return err
}
tx, err := db.Begin()
if err != nil {
return err
}
if err := tx.Exec(`UPDATE release_artifacts SET artifact_id = $1 WHERE artifact_id = $2`, newID, artifact.ID); err != nil {
tx.Rollback()
return err
}
if err := tx.Exec(`UPDATE artifacts SET deleted_at = now() WHERE artifact_id = $1`, artifact.ID); err != nil {
tx.Rollback()
return err
}
if err := tx.Commit(); err != nil {
return err
}
}
return nil
}
示例12: main
func main() {
defer shutdown.Exit()
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
addr := ":" + port
if seed := os.Getenv("NAME_SEED"); seed != "" {
s, err := hex.DecodeString(seed)
if err != nil {
log.Fatalln("error decoding NAME_SEED:", err)
}
name.SetSeed(s)
}
db := postgres.Wait("", "")
if err := migrateDB(db.DB); err != nil {
shutdown.Fatal(err)
}
pgxcfg, err := pgx.ParseURI(fmt.Sprintf("http://%s:%[email protected]%s/%s", os.Getenv("PGUSER"), os.Getenv("PGPASSWORD"), db.Addr(), os.Getenv("PGDATABASE")))
if err != nil {
log.Fatal(err)
}
pgxcfg.Dial = dialer.Retry.Dial
pgxpool, err := pgx.NewConnPool(pgx.ConnPoolConfig{
ConnConfig: pgxcfg,
AfterConnect: que.PrepareStatements,
})
if err != nil {
log.Fatal(err)
}
shutdown.BeforeExit(func() { pgxpool.Close() })
lc, err := logaggc.New("")
if err != nil {
shutdown.Fatal(err)
}
rc := routerc.New()
hb, err := discoverd.DefaultClient.AddServiceAndRegisterInstance("flynn-controller", &discoverd.Instance{
Addr: addr,
Proto: "http",
Meta: map[string]string{
"AUTH_KEY": os.Getenv("AUTH_KEY"),
},
})
if err != nil {
shutdown.Fatal(err)
}
shutdown.BeforeExit(func() {
hb.Close()
})
handler := appHandler(handlerConfig{
db: db,
cc: clusterClientWrapper{cluster.NewClient()},
lc: lc,
rc: rc,
pgxpool: pgxpool,
keys: strings.Split(os.Getenv("AUTH_KEY"), ","),
})
shutdown.Fatal(http.ListenAndServe(addr, handler))
}
示例13: main
func main() {
log := logger.New("fn", "main")
log.Info("creating controller client")
client, err := controller.NewClient("", os.Getenv("AUTH_KEY"))
if err != nil {
log.Error("error creating controller client", "err", err)
shutdown.Fatal()
}
log.Info("connecting to postgres")
db := postgres.Wait("", "")
log.Info("creating postgres connection pool")
pgxpool, err := pgx.NewConnPool(pgx.ConnPoolConfig{
ConnConfig: pgx.ConnConfig{
Host: os.Getenv("PGHOST"),
User: os.Getenv("PGUSER"),
Password: os.Getenv("PGPASSWORD"),
Database: os.Getenv("PGDATABASE"),
},
AfterConnect: que.PrepareStatements,
MaxConnections: workerCount,
})
if err != nil {
log.Error("error creating postgres connection pool", "err", err)
shutdown.Fatal()
}
shutdown.BeforeExit(func() { pgxpool.Close() })
go func() {
status.AddHandler(func() status.Status {
_, err := pgxpool.Exec("SELECT 1")
if err != nil {
return status.Unhealthy
}
return status.Healthy
})
addr := ":" + os.Getenv("PORT")
hb, err := discoverd.AddServiceAndRegister("flynn-controller-worker", addr)
if err != nil {
shutdown.Fatal(err)
}
shutdown.BeforeExit(func() { hb.Close() })
shutdown.Fatal(http.ListenAndServe(addr, nil))
}()
workers := que.NewWorkerPool(
que.NewClient(pgxpool),
que.WorkMap{
"deployment": deployment.JobHandler(db, client, logger),
"app_deletion": app_deletion.JobHandler(db, client, logger),
},
workerCount,
)
workers.Interval = 5 * time.Second
log.Info("starting workers", "count", workerCount, "interval", workers.Interval)
workers.Start()
shutdown.BeforeExit(func() { workers.Shutdown() })
select {} // block and keep running
}
示例14:
query := r.flynn("pg", "psql", "--", "-c", "SELECT * FROM foos")
t.Assert(query, SuccessfulOutputContains, "foobar")
t.Assert(r.flynn("resource", "remove", "postgres", id), Succeeds)
}
var sireniaPostgres = sireniaDatabase{
appName: "postgres",
serviceKey: "FLYNN_POSTGRES",
hostKey: "PGHOST",
initDb: func(t *c.C, r *ct.Release, d *sireniaDeploy) {
db := postgres.Wait(&postgres.Conf{
Discoverd: discoverd.NewClientWithURL(fmt.Sprintf("http://%s:1111", routerIP)),
Service: d.name,
User: "flynn",
Password: r.Env["PGPASSWORD"],
Database: "postgres",
}, nil)
dbname := "deploy-test"
t.Assert(db.Exec(fmt.Sprintf(`CREATE DATABASE "%s" WITH OWNER = "flynn"`, dbname)), c.IsNil)
db.Close()
db = postgres.Wait(&postgres.Conf{
Discoverd: discoverd.NewClientWithURL(fmt.Sprintf("http://%s:1111", routerIP)),
Service: d.name,
User: "flynn",
Password: r.Env["PGPASSWORD"],
Database: dbname,
}, nil)
defer db.Close()
t.Assert(db.Exec(`CREATE TABLE deploy_test ( data text)`), c.IsNil)
示例15: main
func main() {
defer shutdown.Exit()
var cookieKey *[32]byte
if key := os.Getenv("COOKIE_KEY"); key != "" {
res, err := base64.StdEncoding.DecodeString(key)
if err != nil {
shutdown.Fatalf("error decoding COOKIE_KEY: %s", err)
}
if len(res) != 32 {
shutdown.Fatalf("decoded %d bytes from COOKIE_KEY, expected 32", len(res))
}
var k [32]byte
copy(k[:], res)
cookieKey = &k
}
if cookieKey == nil {
shutdown.Fatal("Missing random 32 byte base64-encoded COOKIE_KEY")
}
httpPort := flag.String("http-port", "8080", "http listen port")
httpsPort := flag.String("https-port", "4433", "https listen port")
tcpIP := flag.String("tcp-ip", os.Getenv("LISTEN_IP"), "tcp router listen ip")
tcpRangeStart := flag.Int("tcp-range-start", 3000, "tcp port range start")
tcpRangeEnd := flag.Int("tcp-range-end", 3500, "tcp port range end")
certFile := flag.String("tls-cert", "", "TLS (SSL) cert file in pem format")
keyFile := flag.String("tls-key", "", "TLS (SSL) key file in pem format")
apiPort := flag.String("api-port", "", "api listen port")
flag.Parse()
if *apiPort == "" {
*apiPort = os.Getenv("PORT")
if *apiPort == "" {
*apiPort = "5000"
}
}
keypair := tls.Certificate{}
var err error
if *certFile != "" {
if keypair, err = tls.LoadX509KeyPair(*certFile, *keyFile); err != nil {
shutdown.Fatal(err)
}
} else if tlsCert := os.Getenv("TLSCERT"); tlsCert != "" {
if tlsKey := os.Getenv("TLSKEY"); tlsKey != "" {
os.Setenv("TLSKEY", fmt.Sprintf("md5^(%s)", md5sum(tlsKey)))
if keypair, err = tls.X509KeyPair([]byte(tlsCert), []byte(tlsKey)); err != nil {
shutdown.Fatal(err)
}
}
}
log := logger.New("fn", "main")
log.Info("connecting to postgres")
db := postgres.Wait(nil, nil)
log.Info("running DB migrations")
if err := migrateDB(db); err != nil {
shutdown.Fatal(err)
}
db.Close()
log.Info("reconnecting to postgres with prepared queries")
db = postgres.Wait(nil, schema.PrepareStatements)
shutdown.BeforeExit(func() { db.Close() })
httpAddr := net.JoinHostPort(os.Getenv("LISTEN_IP"), *httpPort)
httpsAddr := net.JoinHostPort(os.Getenv("LISTEN_IP"), *httpsPort)
r := Router{
TCP: &TCPListener{
IP: *tcpIP,
startPort: *tcpRangeStart,
endPort: *tcpRangeEnd,
ds: NewPostgresDataStore("tcp", db.ConnPool),
discoverd: discoverd.DefaultClient,
},
HTTP: &HTTPListener{
Addr: httpAddr,
TLSAddr: httpsAddr,
cookieKey: cookieKey,
keypair: keypair,
ds: NewPostgresDataStore("http", db.ConnPool),
discoverd: discoverd.DefaultClient,
},
}
if err := r.Start(); err != nil {
shutdown.Fatal(err)
}
shutdown.BeforeExit(r.Close)
apiAddr := net.JoinHostPort(os.Getenv("LISTEN_IP"), *apiPort)
log.Info("starting API listener")
listener, err := listenFunc("tcp4", apiAddr)
if err != nil {
log.Error("error starting API listener", "err", err)
shutdown.Fatal(listenErr{apiAddr, err})
}
//.........這裏部分代碼省略.........