Beispiel #1
0
func main() {
	mq, err := dispatch.NewRedis("redis:6379")
	if err != nil {
		log.Fatal(err)
	}

	stop := make(chan bool, 2)
	duration := time.Second * 1
	recv, err := mq.Subscribe("build_queue_update", stop, duration)
	if err != nil {
		log.Fatal(err)
	}

	defer func() {
		stop <- true
	}()

	sig := make(chan os.Signal, 1)
	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)

	go func() {
		for {
			select {
			case payload, ok := <-recv:
				if !ok {
					recv = nil
					break
				}
				go func() {
					msg := payload.(*redis.Message)
					dispatchMsg := new(dispatch.Message)
					s, _ := strconv.Unquote(msg.Payload)
					json.Unmarshal([]byte(s), &dispatchMsg)

					fmt.Println(dispatchMsg)

					if c, ok := conn[dispatchMsg.Repo]; ok {
						c.WriteMessage(websocket.TextMessage, []byte(s))
					}
				}()
			case <-sig:
				log.Println("OS Signal received, exiting...")
				recv = nil
			}

			if recv == nil {
				os.Exit(0)
				break
			}
		}
	}()

	http.HandleFunc("/", handle)
	log.Fatal(http.ListenAndServe(":8888", nil))
}
Beispiel #2
0
func main() {
	mq, err := dispatch.NewRedis("redis:6379")
	if err != nil {
		log.Fatal(err)
	}

	stop := make(chan bool, 2)
	duration := time.Second * 1
	recv, err := mq.Subscribe("github_webhook_push", stop, duration)
	if err != nil {
		log.Fatal(err)
	}

	defer func() {
		stop <- true
	}()

	mq.Publish(
		"github_webhook_push",
		github.Push{
			RequestID:  "f28a10b9",
			Branch:     "master",
			OwnerName:  "aneshas",
			OwnerEmail: "*****@*****.**",
			CloneURL:   "https://github.com/aneshas/guinea-pig",
			FullName:   "aneshas/guinea-pig",
		})

	sig := make(chan os.Signal, 1)
	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)

	for {
		select {
		case payload, ok := <-recv:
			if !ok {
				recv = nil
			}
			msg := payload.(*redis.Message)
			push := new(github.Push)
			err := json.Unmarshal([]byte(msg.Payload), &push)
			if err != nil {
				log.Println(err)
				break
			}

			// Instantiate golang stack
			goStack := stack.NewGolang("latest")

			// Create new contaner from stack
			cnt := docker.New(goStack, push)

			// Run the container
			cnt.WG.Add(2)
			go cnt.Run()
			go cnt.Observe(mq)

			log.Printf("Waiting for %s queue to finish", push.RequestID)
			// cnt.WG.Wait()

			log.Println(push)
		case <-sig:
			log.Println("OS Signal received, exiting...")
			recv = nil
		}

		if recv == nil {
			break
		}
	}

}