// ConnectBucket is the default function used by BucketDataSource // to connect to a Couchbase cluster to retrieve Bucket information. // It is exposed for testability and to allow applications to // override or wrap via BucketDataSourceOptions. func ConnectBucket(serverURL, poolName, bucketName string, auth couchbase.AuthHandler) (Bucket, error) { var bucket *couchbase.Bucket var err error if auth != nil { client, err := couchbase.ConnectWithAuth(serverURL, auth) if err != nil { return nil, err } pool, err := client.GetPool(poolName) if err != nil { return nil, err } bucket, err = pool.GetBucket(bucketName) } else { bucket, err = couchbase.GetBucket(serverURL, poolName, bucketName) } if err != nil { return nil, err } if bucket == nil { return nil, fmt.Errorf("unknown bucket,"+ " serverURL: %s, bucketName: %s", serverURL, bucketName) } return &bucketWrapper{b: bucket}, nil }
// Creates a Bucket that talks to a real live Couchbase server. func GetCouchbaseBucket(spec BucketSpec, callback sgbucket.BucketNotifyFn) (bucket Bucket, err error) { client, err := couchbase.ConnectWithAuth(spec.Server, spec.Auth) if err != nil { return } poolName := spec.PoolName if poolName == "" { poolName = "default" } pool, err := client.GetPool(poolName) if err != nil { return } cbbucket, err := pool.GetBucket(spec.BucketName) spec.MaxNumRetries = 10 spec.InitialRetrySleepTimeMS = 5 if err == nil { // Start bucket updater - see SG issue 1011 cbbucket.RunBucketUpdater(func(bucket string, err error) { Warn("Bucket Updater for bucket %s returned error: %v", bucket, err) if callback != nil { callback(bucket, err) } }) bucket = CouchbaseBucket{cbbucket, spec} } return }
func jobPurger(couchbaseAddress string) { client, _ := couchbase.Connect(couchbaseAddress) pool, _ := client.GetPool("default") params := map[string]interface{}{ "reduce": false, "include_docs": true, "stale": "false", } bucket := "server" mc, err := memcached.Connect("tcp", "127.0.0.1:11210") _, err = mc.Auth(bucket, "") if err != nil { log.Fatal(err) } // query bucket b, err := pool.GetBucket(bucket) if err != nil { log.Fatalf("Error reading bucket: %v", err) } vr, err := b.View(bucket, "data_by_build", params) if err != nil { log.Println(err) } for _, row := range vr.Rows { vals := row.Value.([]interface{}) jobUrl := vals[5].(string) resp, _ := http.Head(jobUrl) if resp == nil { continue } if resp.StatusCode == 404 { // make sure 404 is not because jenkins is down parsedUrl, _ := url.Parse(jobUrl) resp, _ = http.Head(parsedUrl.Scheme + "://" + parsedUrl.Host) if resp != nil && resp.StatusCode != 200 { log.Println("Jenkins down! skipping: " + jobUrl) continue } log.Println("Purging: " + jobUrl) id := row.ID // TODO: using dirty workaround until keys are in correct vbuckets _, err = mc.Get(0, id) if err == nil { _, err = mc.Del(0, id) if err != nil { log.Fatal(err) } } } } }
// GetBucket is a convenience function for getting a named bucket from // a URL func GetBucket(endpoint, poolname, bucketname string) (*Bucket, error) { var err error client, err := Connect(endpoint) if err != nil { return nil, err } pool, err := client.GetPool(poolname) if err != nil { return nil, err } return pool.GetBucket(bucketname) }
// Creates a Bucket that talks to a real live Couchbase server. func GetCouchbaseBucket(spec BucketSpec) (bucket Bucket, err error) { client, err := couchbase.ConnectWithAuth(spec.Server, spec.Auth) if err != nil { return } poolName := spec.PoolName if poolName == "" { poolName = "default" } pool, err := client.GetPool(poolName) if err != nil { return } cbbucket, err := pool.GetBucket(spec.BucketName) if err == nil { bucket = CouchbaseBucket{cbbucket, spec} } return }
// Creates a Bucket that talks to a real live Couchbase server. func GetCouchbaseBucket(spec BucketSpec) (bucket Bucket, err error) { client, err := couchbase.ConnectWithAuth(spec.Server, spec.Auth) if err != nil { return } poolName := spec.PoolName if poolName == "" { poolName = "default" } pool, err := client.GetPool(poolName) if err != nil { return } cbbucket, err := pool.GetBucket(spec.BucketName) if err == nil { // Start bucket updater - see SG issue 1011 cbbucket.RunBucketUpdater(func(bucket string, err error) { Warn("Bucket Updater for bucket %s returned error: %v", bucket, err) }) bucket = CouchbaseBucket{cbbucket, spec} } return }
func jobPurger(couchbaseAddress string) { defer func() { if r := recover(); r != nil { fmt.Printf("Recovered from panic: %v", r) } }() client, _ := couchbase.Connect(couchbaseAddress) pool, _ := client.GetPool("default") params := map[string]interface{}{ "reduce": false, } bucket := "server" mc, err := memcached.Connect("tcp", "172.23.121.132:11210") _, err = mc.Auth(bucket, "") if err != nil { log.Fatal(err) } // query bucket b, err := pool.GetBucket(bucket) if err != nil { log.Fatalf("Error reading bucket: %v", err) } vr, err := b.View("server", "jobs_by_build", params) if err != nil { log.Println(err) } for _, row := range vr.Rows { vals := row.Value.([]interface{}) if len(vals) < 7 { continue } jobUrl := vals[3].(string) //log.Println("Processing: " +jobUrl) resp, _ := http.Get(jobUrl) if resp.StatusCode == 200 { resp.Body.Close() continue } if resp.StatusCode == 404 { // make sure 404 is not because jenkins is down parsedUrl, _ := url.Parse(jobUrl) resp, _ = http.Get(parsedUrl.Scheme + "://" + parsedUrl.Host) if resp != nil && resp.StatusCode != 200 { log.Println("Jenkins down! skipping: " + jobUrl) continue } resp.Body.Close() log.Println("Purging: " + jobUrl) id := row.ID // TODO: using dirty workaround until keys are in correct vbuckets _, err = mc.Get(0, id) if err == nil { _, err = mc.Del(0, id) if err != nil { log.Fatal(err) } } } } mc.Close() }