示例#1
0
func Example() {
	myOplogURL := "http://oplog.mydomain.com"
	c := oplogc.Subscribe(myOplogURL, oplogc.Options{})

	ops, errs, done := c.Start()

	for {
		select {
		case op := <-ops:
			// Got the next operation
			switch op.Event {
			case "reset":
				// reset the data store
			case "live":
				// put the service back in production
			default:
				// Do something with the operation
				//url := fmt.Sprintf("http://api.domain.com/%s/%s", op.Data.Type, op.Data.ID)
				//data := MyAPIGetter(url)
				//MyDataSyncer(data)
			}

			// Ack the fact you handled the operation
			op.Done()
		case err := <-errs:
			switch err {
			case oplogc.ErrAccessDenied, oplogc.ErrWritingState:
				c.Stop()
				log.Fatal(err)
			case oplogc.ErrResumeFailed:
				log.Print("Resume failed, forcing full replication")
				c.SetLastID("0")
			default:
				log.Print(err)
			}
		case <-done:
			return
		}
	}
}
示例#2
0
文件: main.go 项目: saminou/oplogc
func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
		flag.PrintDefaults()
		fmt.Print("  <oplog url>\n")
	}
	flag.Parse()
	if flag.NArg() != 1 {
		flag.Usage()
		os.Exit(2)
	}
	url := flag.Arg(0)

	f := oplogc.Filter{
		Types:   strings.Split(*types, ","),
		Parents: strings.Split(*parents, ","),
	}
	c := oplogc.Subscribe(url, oplogc.Options{
		StateFile:        *stateFile,
		Password:         *password,
		AllowReplication: *allowReplication,
		Filter:           f,
	})

	ops, errs, done := c.Start()
	for {
		select {
		case op := <-ops:
			switch op.Event {
			case "reset":
				fmt.Print("** reset\n")
			case "live":
				fmt.Print("** live\n")
			default:
				if op.Data.Ref == "" {
					fmt.Printf("%s: %s #%s %s/%s (%s)\n",
						op.Data.Timestamp, op.Event, op.ID, op.Data.Type, op.Data.ID, strings.Join(op.Data.Parents, ", "))
				} else {
					fmt.Printf("%s: %s #%s %s (%s)\n",
						op.Data.Timestamp, op.Event, op.ID, op.Data.Ref, strings.Join(op.Data.Parents, ", "))
				}
			}
			op.Done()
		case err := <-errs:
			switch err {
			case oplogc.ErrAccessDenied, oplogc.ErrWritingState:
				c.Stop()
				log.Fatal(err)
			case oplogc.ErrResumeFailed:
				if *stateFile != "" {
					log.Print("Resume failed, forcing full replication")
					c.SetLastID("0")
				} else {
					log.Print(err)
				}
			default:
				log.Print(err)
			}
		case <-done:
			return
		}
	}
}