// WaitService waits for all of a service's instances to satisfy a particular
// state.
7933
func WaitService(cancel <-chan struct{}, conn client.Connection, poolID, serviceID string, checkCount func(count int) bool, checkState func(s *State, exists bool) bool) error {
	basepth := ""
	if poolID != "" {
		basepth = path.Join("/pools", poolID)
	}
	pth := path.Join(basepth, "/services", serviceID)

	logger := plog.WithFields(log.Fields{
		"serviceid": serviceID,
		"zkpath":    pth,
	})

	done := make(chan struct{})
	defer func() { close(done) }()
	for {

		// clean any bad service states
		if err := CleanServiceStates(conn, poolID, serviceID); err != nil {
			return err
		}

		// get the list of states
		ch, ev, err := conn.ChildrenW(pth, done)
		if err != nil {

			logger.WithError(err).Debug("Could not watch states for service")

			// TODO: wrap error?
			return err
		}

		// make sure the count satisfies the requirements
		if checkCount(len(ch)) {
			ok := true

			for _, stateID := range ch {
				st8log := logger.WithField("stateid", stateID)

				// parse the state id and set up the request
				hostID, _, instanceID, err := ParseStateID(stateID)
				if err != nil {

					// This should never happen, but handle it
					st8log.WithError(err).Error("Invalid state id while monitoring service")
					return err
				}

				req := StateRequest{
					PoolID:     poolID,
					HostID:     hostID,
					ServiceID:  serviceID,
					InstanceID: instanceID,
				}

				// wait for the state to satisfy the requirements
				if _, err := MonitorState(cancel, conn, req, checkState); err != nil {
					st8log.WithError(err).Debug("Stopped monitoring state")

					// return if cancel was triggered
					select {
					case <-cancel:
						return nil
					default:
					}

					ok = false
					break
				}
			}

			// if all of the requirements are satisfied, return
			if ok {
				return nil
			}
		}

		// otherwise, wait for the number of states to change
		select {
		case <-ev:
		case <-cancel:
			return nil
		}
		close(done)
		done = make(chan struct{})
	}
}
1224325
func (s Service) WaitForStatus(wait time.Duration, delay time.Duration) (*ServiceStatus, error) {
	s.log.Info("Waiting for %s to be loaded...", s.label)
	return waitForStatus(wait, delay, s.LoadStatus)
}
1188244
func startService() error {
	// Connect to the windows service manager.
	serviceManager, err := mgr.Connect()
	if err != nil {
		return err
	}
	defer serviceManager.Disconnect()

	service, err := serviceManager.OpenService(svcName)
	if err != nil {
		return fmt.Errorf("could not access service: %v", err)
	}
	defer service.Close()

	err = service.Start(os.Args)
	if err != nil {
		return fmt.Errorf("could not start service: %v", err)
	}

	return nil
}
175181
func (ec *ServiceManager) BindService(service RunnableInterface) {
	ec.services = append(ec.services, service)
}
1061204
func (s *DestinationInfo) SetService(v string) *DestinationInfo {
	s.Service = &v
	return s
}
731314
func (client *Client) WaitForInstance(instanceId string, status InstanceStatus, timeout int) error {
	if timeout <= 0 {
		timeout = InstanceDefaultTimeout
	}
	for {
		args := DescribeDBInstanceAttributeArgs{
			DBInstanceId: instanceId,
		}

		resp, err := client.DescribeDBInstanceAttribute(&args)
		if err != nil {
			return err
		}

		if timeout <= 0 {
			return common.GetClientErrorFromString("Timeout")
		}

		timeout = timeout - DefaultWaitForInterval
		time.Sleep(DefaultWaitForInterval * time.Second)

		if len(resp.Items.DBInstanceAttribute) < 1 {
			continue
		}
		instance := resp.Items.DBInstanceAttribute[0]
		if instance.DBInstanceStatus == status {
			break
		}

	}
	return nil
}
167910
func (_m *FacadeInterface) WaitService(ctx datastore.Context, dstate service.DesiredState, timeout time.Duration, recursive bool, serviceIDs ...string) error {
	ret := _m.Called(ctx, dstate, timeout, recursive, serviceIDs)

	var r0 error
	if rf, ok := ret.Get(0).(func(datastore.Context, service.DesiredState, time.Duration, bool, ...string) error); ok {
		r0 = rf(ctx, dstate, timeout, recursive, serviceIDs...)
	} else {
		r0 = ret.Error(0)
	}

	return r0
}
1285742
func InitService(serviceName string) error {
	h := &handler{
		tosvc:   make(chan bool),
		fromsvc: make(chan error),
	}

	service = h
	var err error
	go func() {
		err = svc.Run(serviceName, h)
		h.fromsvc <- err
	}()

	// Wait for the first signal from the service handler.
	err = <-h.fromsvc
	if err != nil {
		return err
	}
	klog.Infof("Running %s as a Windows service!", serviceName)
	return nil
}
1078427
func StartService() error {
	svc, err := discoverService(ServiceName)
	if err != nil {
		return errors.Trace(err)
	}
	return svc.Start()
}
1296395
func WaitAndMaybeOpenService(api libmachine.API, namespace string, service string, urlTemplate *template.Template, urlMode bool, https bool,
	wait int, interval int) error {
	if err := util.RetryAfter(wait, func() error { return CheckService(namespace, service) }, time.Duration(interval)*time.Second); err != nil {
		return errors.Wrapf(err, "Could not find finalized endpoint being pointed to by %s", service)
	}

	urls, err := GetServiceURLsForService(api, namespace, service, urlTemplate)
	if err != nil {
		return errors.Wrap(err, "Check that minikube is running and that you have specified the correct namespace")
	}
	for _, bareURLString := range urls {
		urlString, isHTTPSchemedURL := OptionallyHTTPSFormattedURLString(bareURLString, https)

		if urlMode || !isHTTPSchemedURL {
			console.OutLn(urlString)
		} else {
			console.OutStyle("celebrate", "Opening kubernetes service %s/%s in default browser...", namespace, service)
			browser.OpenURL(urlString)
		}
	}
	return nil
}
719199
func (s *BatchServiceStateManager) WaitScheduled(tenantID string, serviceIDs ...string) {
	s.lock.RLock()
	var wg sync.WaitGroup
	for _, sid := range serviceIDs {
		// find the service in the queues
		if svc, ok := s.findService(tenantID, sid); ok {
			wg.Add(1)
			go func(cs *CancellableService) {
				<-cs.C
				wg.Done()
			}(svc)
		} else {
			plog.WithFields(logrus.Fields{
				"tenantid":  tenantID,
				"serviceid": sid,
			}).Debug("Not waiting for service, could not find it")
		}
	}
	s.lock.RUnlock()
	wg.Wait()
}
87473
func (c *AutoScaling) WaitUntilGroupInService(input *DescribeAutoScalingGroupsInput) error {
	return c.WaitUntilGroupInServiceWithContext(aws.BackgroundContext(), input)
}
619116
func (s *ReindexService) WaitForCompletion(waitForCompletion bool) *ReindexService {
	s.waitForCompletion = &waitForCompletion
	return s
}
491036
func (a *api) StartService(config SchedulerConfig) (int, error) {
	client, err := a.connectDAO()
	if err != nil {
		return 0, err
	}

	var affected int
	err = client.StartService(dao.ScheduleServiceRequest{config.ServiceIDs, config.AutoLaunch, config.Synchronous}, &affected)
	return affected, err
}
1120980
func (device *Device) FindService(serviceType string) []*Service {
	var services []*Service
	device.VisitServices(func(s *Service) {
		if s.ServiceType == serviceType {
			services = append(services, s)
		}
	})
	return services
}
181510
func getService(c context.Context) *compute.Service {
	return c.Value(&key).(*compute.Service)
}
512388
func (c *Client) WaitService(serviceIDs []string, state service.DesiredState, timeout time.Duration, recursive bool) error {
	waitSvcRequest := &WaitServiceRequest{
		ServiceIDs: serviceIDs,
		State:      state,
		Timeout:    timeout,
		Recursive:  recursive,
	}
	throwaway := ""
	err := c.call("WaitService", waitSvcRequest, &throwaway)
	return err
}