// Start a factory service mock with factory service URI and a func to create service state func (th *ServiceHost) StartFactoryMock(factoryLink string, createStatef func() interface{}) MockFactoryService { u := uri.Extend(th.URI(), "/"+factoryLink) factoryService, s := NewMockFactoryService(th, factoryLink, createStatef) if err := th.StartServiceSync(u.Path, s); err != nil { th.t.Fatalf("Error starting factory service: %s\n", err) } return factoryService }
func main() { services := []struct { uri string svc host.Service }{ // Examples { "/core/examples", host.NewFactoryServiceContext(&host.ExampleFactoryService{}), }, // Examples { "/core/ping", host.NewPingService(), }, } var err error flag.Parse() glog.Infof("Started with %s", os.Args[1:]) h := host.NewServiceHost() err = h.Initialize(bindAddress.String()) if err != nil { glog.Fatalf("Error initializing: %s\n", err) } ctx := operation.SetAuthorizationToken(context.Background(), authToken) _, err = host.GetServiceHostManagementState(ctx) if err != nil { glog.Fatalf("Error getting ServiceHostState: %s\n", err) } var ops []*operation.Operation for _, s := range services { op := operation.NewPost(ctx, uri.Extend(uri.Empty(), s.uri), nil) ops = append(ops, op) h.StartService(op, s.svc) } _, err = operation.Join(ops) if err != nil { glog.Fatalf("Error starting services: %s", err) } start(h) }
// GetServiceHostManagementState returns the local node's NodeState func GetServiceHostManagementState(ctx context.Context) (*common.ServiceHostState, error) { u := uri.Extend(uri.Local(), common.Management) p := operation.NewGet(ctx, u) client.Send(p) if err := p.Wait(); err != nil { return nil, err } var state common.ServiceHostState err := p.DecodeBody(&state) if err != nil { return nil, err } return &state, nil }
// Start a service mock with given serivce state and selfLink func (th *ServiceHost) StartMockWithSelfLink(data interface{}, selfLink string) uri.URI { u := uri.Extend(th.URI(), selfLink) s := NewMockService(data) if err := th.StartServiceSync(u.Path, s); err != nil { th.t.Fatalf("Error starting service: %s\n", err) } ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) defer cancel() select { case <-s.StageBarrier(host.StageAvailable): case <-ctx.Done(): th.t.Fatalf("timeout starting service") } return u }
// handlePost calls out to the factory service implementation's POST handler, // if it exists, and waits for completion. If this runs and completes without // error, the returned body is passed to the start operation for the service // created by this factory. func (f *FactoryServiceContext) handlePost(ctx context.Context, op *operation.Operation) { var err error var sd *common.ServiceDocument if h, ok := f.h.(PostHandler); ok { // Run the factory service's POST handler and wait for completion. err = op.CreateChild(ctx).Go(ctx, h.HandlePost).Wait() if err != nil { op.Fail(err) return } } doc := f.h.CreateDocument() err = op.DecodeBody(doc) if err != nil && err != io.EOF { op.Fail(err) return } sd = doc.GetServiceDocument() sd.SelfLink = path.Join(f.SelfLink(), uuid.New()) op.SetBody(doc) buf, err := op.EncodeBodyAsBuffer() if err != nil { op.Fail(err) return } // Start child service at service document's selflink startOp := op.NewPost(ctx, uri.Extend(uri.Local(), sd.SelfLink), buf) f.Host().StartService(startOp, f.h.CreateService()) err = startOp.Wait() if err != nil { op.Fail(err) return } op.Complete() }
// StartServiceSync starts the specified service, synchronously. func (h *ServiceHost) StartServiceSync(path string, s Service) error { ctx := context.Background() startOp := operation.NewPost(ctx, uri.Extend(h.selfURI, path), nil) h.StartService(startOp, s) return startOp.Wait() }