Example #1
0
// Wait for existing requests to complete and shutdown service
func (s *Service) shutdown() {
	if s.shuttingDown {
		return
	}

	s.shuttingDown = true

	s.doneGroup.Add(1)
	s.rpcListener.Close()

	s.doneChan <- true

	s.activeRequests.Wait()

	err := skynet.GetServiceManager().Remove(*s.ServiceInfo)
	if err != nil {
		log.Println(log.ERROR, "Failed to remove service: "+err.Error())
	}

	skynet.GetServiceManager().Shutdown()

	s.Delegate.Stopped(s) // Call user defined callback

	s.doneGroup.Done()
}
Example #2
0
func StopDaemon(criteria *skynet.Criteria) {
	hosts, err := skynet.GetServiceManager().ListHosts(criteria)

	if err != nil {
		log.Fatal(err)
	}

	var wait sync.WaitGroup

	for _, host := range hosts {
		wait.Add(1)
		go func(host string) {
			d := daemon.GetDaemonForHost(host)

			in := daemon.StopRequest{}
			out, err := d.Stop(in)

			if err != nil {
				fmt.Println("Returned Error: " + err.Error())
				wait.Done()
				return
			}

			if out.Ok {
				fmt.Printf("Daemon stopped on host: %v\n", host)
			} else {
				fmt.Printf("Failed to stop daemon on host: %v\n", host)
			}

			wait.Done()
		}(host)
	}

	wait.Wait()
}
Example #3
0
func Stop(criteria *skynet.Criteria) {
	instances, err := skynet.GetServiceManager().ListInstances(criteria)

	if err != nil {
		log.Fatal(err)
	}

	var wait sync.WaitGroup

	for _, instance := range filterDaemon(instances) {
		wait.Add(1)
		go func(instance skynet.ServiceInfo) {
			fmt.Println("Stopping: " + instance.UUID)
			d := daemon.GetDaemonForService(&instance)

			in := daemon.StopSubServiceRequest{
				UUID: instance.UUID,
			}
			out, err := d.StopSubService(in)

			if err != nil {
				fmt.Println("Returned Error: " + err.Error())
				wait.Done()
				return
			}

			stopTemplate.Execute(os.Stdout, out)
			wait.Done()
		}(instance)
	}

	wait.Wait()
}
Example #4
0
func SetLogLevel(criteria *skynet.Criteria, level string) {
	instances, err := skynet.GetServiceManager().ListInstances(criteria)

	if err != nil {
		log.Fatal(err)
	}

	var wait sync.WaitGroup

	for _, instance := range filterDaemon(instances) {
		wait.Add(1)
		go func(instance skynet.ServiceInfo) {
			fmt.Println("Setting LogLevel to " + level + " for: " + instance.UUID)
			d := daemon.GetDaemonForService(&instance)

			in := daemon.SubServiceLogLevelRequest{
				UUID:  instance.UUID,
				Level: level,
			}
			out, err := d.SubServiceLogLevel(in)

			if err != nil {
				fmt.Println("Returned Error: " + err.Error())
				wait.Done()
				return
			}

			logLevelTemplate.Execute(os.Stdout, out)
			wait.Done()
		}(instance)
	}

	wait.Wait()
}
Example #5
0
func getInstances(c *skynet.Criteria) []skynet.ServiceInfo {
	instances, err := skynet.GetServiceManager().ListInstances(c)

	if err != nil {
		log.Fatal(err)
	}

	return instances
}
Example #6
0
func getHosts(c *skynet.Criteria) []string {
	hosts, err := skynet.GetServiceManager().ListHosts(c)

	if err != nil {
		log.Fatal(err)
	}

	return hosts
}
Example #7
0
func getServices(c *skynet.Criteria) []string {
	services, err := skynet.GetServiceManager().ListServices(c)

	if err != nil {
		log.Fatal(err)
	}

	return services
}
Example #8
0
func getVersions(c *skynet.Criteria) []string {
	versions, err := skynet.GetServiceManager().ListVersions(c)

	if err != nil {
		log.Fatal(err)
	}

	return versions
}
Example #9
0
func addServiceClient(sc ServiceClientProvider) {
	serviceClients = append(serviceClients, sc)

	instances := skynet.GetServiceManager().Watch(sc, instanceWatcher)

	for _, i := range instances {
		pool.AddInstance(i)
		sc.Notify(skynet.InstanceNotification{Type: skynet.InstanceAdded, Service: i})
	}
}
Example #10
0
func (s *Service) unregister() {
	// this version must be run from the mux() goroutine
	if !s.Registered {
		return
	}

	err := skynet.GetServiceManager().Unregister(s.UUID)
	if err != nil {
		log.Println(log.ERROR, "Failed to unregister service: "+err.Error())
	}

	s.Registered = false
	log.Printf(log.INFO, "%+v\n", ServiceUnregistered{s.ServiceInfo})
	s.Delegate.Unregistered(s) // Call user defined callback
}
Example #11
0
// Starts your skynet service, including binding to ports. Optionally register for requests at the same time. Returns a sync.WaitGroup that will block until all requests have finished
func (s *Service) Start() (done *sync.WaitGroup) {
	bindWait := &sync.WaitGroup{}

	bindWait.Add(1)
	go s.listen(s.ServiceAddr, bindWait)

	// Watch signals for shutdown
	c := make(chan os.Signal, 1)
	go watchSignals(c, s)

	s.doneChan = make(chan bool, 1)

	// We must block here, we don't want to register, until we've actually bound to an ip:port
	bindWait.Wait()

	s.doneGroup = &sync.WaitGroup{}
	s.doneGroup.Add(1)

	go func() {
		s.mux()
		s.doneGroup.Done()
	}()
	done = s.doneGroup

	if r, err := config.Bool(s.Name, s.Version, "service.register"); err == nil {
		s.Registered = r
	}

	err := skynet.GetServiceManager().Add(*s.ServiceInfo)
	if err != nil {
		log.Println(log.ERROR, "Failed to add service: "+err.Error())
	}

	if s.Registered {
		s.Register()
	}

	go s.Delegate.Started(s) // Call user defined callback

	if s.ServiceInfo.Registered {
		go s.Delegate.Registered(s) // Call user defined callback
	}

	return
}
Example #12
0
func Start(criteria *skynet.Criteria, args []string) {
	if len(args) < 1 {
		fmt.Println("Please provide a service name 'sky start binaryName'")
		return
	}

	hosts, err := skynet.GetServiceManager().ListHosts(criteria)

	if err != nil {
		log.Fatal(err)
	}

	var wait sync.WaitGroup

	for _, host := range hosts {
		wait.Add(1)
		go func(host string) {
			fmt.Println("Starting on host: " + host)
			d := daemon.GetDaemonForHost(host)

			in := daemon.StartSubServiceRequest{
				BinaryName: args[0],
				Args:       shellquote.Join(args[1:]...),
				// TODO: maybe an optional flag to change this?
				Registered: true,
			}
			out, err := d.StartSubService(in)

			if err != nil {
				fmt.Println("Returned Error: " + err.Error())
				wait.Done()
				return
			}

			startTemplate.Execute(os.Stdout, out)
			wait.Done()
		}(host)
	}

	wait.Wait()
}
Example #13
0
func (s *SkynetDaemon) cleanupHost(daemonUUID string) (err error) {
	sm := skynet.GetServiceManager()
	c := skynet.Criteria{}

	c.AddHost(s.Service.ServiceInfo.ServiceAddr.IPAddress)

	var instances []skynet.ServiceInfo
	instances, err = sm.ListInstances(&c)

	if err != nil {
		return
	}

	for _, i := range instances {
		if i.UUID != daemonUUID {
			sm.Remove(i)
		}
	}

	return
}
Example #14
0
func SetDaemonLogLevel(criteria *skynet.Criteria, level string) {
	hosts, err := skynet.GetServiceManager().ListHosts(criteria)

	if err != nil {
		log.Fatal(err)
	}

	var wait sync.WaitGroup

	for _, host := range hosts {
		wait.Add(1)
		go func(host string) {
			d := daemon.GetDaemonForHost(host)

			in := daemon.LogLevelRequest{
				Level: level,
			}
			out, err := d.LogLevel(in)

			if err != nil {
				fmt.Println("Returned Error: " + err.Error())
				wait.Done()
				return
			}

			if out.Ok {
				fmt.Printf("Set daemon log level to %v on host: %v\n", level, host)
			} else {
				fmt.Printf("Failed to set daemon log level to %v on host: %v\n", level, host)
			}

			wait.Done()
		}(host)
	}

	wait.Wait()
}