Code Monkey home page Code Monkey logo

go-supervisor's Introduction

Go Supervisor API Toolkit

API toolkit for Supervisor written in Go!

Event Listener

Listener implements a Supervisor event listener. The following code illustrates basic usage of the listener:

func main() {
	done := make(chan bool)
	events := make(chan supervisor.Event)
	evl := supervisor.NewListener(os.Stdin, os.Stdout)

	go func() {
		for event := range events {
			fmt.Fprintf(os.Stderr, "Got event: %s\n", event)
		}
		done <- true
	}()

	evl.Run(events)
	close(events)
	<-done
}

RPC Client

Client implements an HTML RPC client to communicate with Supervisor. The following code demonstrates using the client to start a service:

func main() {
	client, err := supervisor.NewClient("http://localhost:9001/RPC2")
	if err != nil {
		fmt.Printf("Error: %s\n", err)
		os.Exit(1)
	}

	if result, err = client.StartProcess("nginx", true); err != nil {
		fmt.Printf("Error: %s\n", err)
		os.Exit(1)
	} else if !result {
		fmt.Printf("Failed to start process.\n")
	}

	fmt.Printf("Process started.\n")
}

Stateful Monitor

Monitor implements a stateful monitoring system. It maintains the current state of all processes and emits events when processes are added, removed, or change state. It will also emit events when the Supervisor instance changes state. It will also do a full state refresh on any TICK event it receives. For full functionality it requires the PROCESS_STATE, SUPERVISOR_STATE_CHANGE, and a TICK event. If the TICK event is removed then no events will be emitted for process removal.

As an example:

func main() {
	url := "http://localhost:9001/RPC2"
	events := make(chan interface{})
	mon, err := supervisor.NewMonitor(url, os.Stdin, os.Stdout, events)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %s\n", err)
		os.Exit(1)
	}

	done := make(chan bool)
	go func() {
		for event := range events {
			switch event.(type) {
			case supervisor.ProcessAddEvent:
				process := (event.(supervisor.ProcessAddEvent)).Process
				fmt.Fprintf(os.Stderr, "Process %s added\n", process.Name)
			case supervisor.ProcessRemoveEvent:
				process := (event.(supervisor.ProcessRemoveEvent)).Process
				fmt.Fprintf(os.Stderr, "Process %s added\n", process.Name)
			case supervisor.ProcessStateEvent:
				process := (event.(supervisor.ProcessStateEvent)).Process
				from := (event.(supervisor.ProcessStateEvent)).FromState
				fmt.Fprintf(os.Stderr, "Process %s state change %s => %s\n", process.Name, from, process.State)
			case supervisor.SupervisorStateEvent:
				supervisor := (event.(supervisor.SupervisorStateEvent)).Supervisor
				from := (event.(supervisor.SupervisorStateEvent)).FromState
				fmt.Fprintf(os.Stderr, "Supervisor \"%s\" state change %s => %s\n", supervisor.Name, from, supervisor.State)
			default:
				fmt.Fprintf(os.Stderr, "Unchecked Event: %+v\n", event)
			}
		}
		done <- true
	}()

	mon.Refresh()
	mon.Run()

	close(events)
	mon.Close()
	<- done
}

License

This software project is licensed under the BSD-derived license and is copyright (c) 2013 Ryan Bourgeois. A copy of the license is included in the LICENSE file. If it is missing a copy can be found on the project page.

go-supervisor's People

Contributors

rynbrd avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.