Example #1
0
func main() {
	// logging
	logrus.SetLevel(logrus.DebugLevel)

	// seed random
	rand.Seed(time.Now().Unix())

	//
	ctx, shutdown := context.WithCancel(context.Background())

	// dispatcher
	dispatch := dispatch.New()
	go dispatch.Run(ctx)

	// Asana
	team, err := strconv.Atoi(os.Getenv("ASANA_TEAM"))
	if err != nil {
		logrus.WithField("error", err).Fatal("could not convert ASANA_TEAM to int")
	}
	asana, err := asana.New(os.Getenv("ASANA_TOKEN"), team)
	if err != nil {
		logrus.WithField("error", err).Fatal("could not initialize Asana")
	}
	go asana.Handle(ctx, dispatch.Register("github:opened"))

	// Github
	github, err := github.New(os.Getenv("PORT"))
	if err != nil {
		logrus.WithField("error", err).Fatal("could not initialize Github")
	}
	go func() {
		err := github.Produce(dispatch.Send)
		if err != nil {
			logrus.WithField("error", err).Error("error starting producer")
		}
		logrus.Info("shutting down due to HTTP stopping")
		shutdown()
	}()

	// and finally sleep and catch events
	defer shutdown()
	go catch(shutdown)

	// give services time to finish and shut down
	<-ctx.Done()
	logrus.Info("waiting for grace period for services to shut down")
	time.Sleep(time.Second * 5)
}
Example #2
0
// New returns a new Asana Handler
func New(token string, teamID int) (*Handler, error) {
	handler := &Handler{
		Asana:  NewAsana(token),
		Team:   teamID,
		logger: logrus.WithField("service", "asana").WithField("team", teamID),
	}

	// get the organization from the team
	_, jsonResponse, errs := handler.Asana.Client().Get(fmt.Sprintf("https://app.asana.com/api/1.0/teams/%d", teamID)).End()

	if errs != nil {
		return nil, errs[0]
	}

	team := &teamResponse{}
	err := json.Unmarshal([]byte(jsonResponse), team)
	if err != nil {
		return nil, err
	}
	if team.Data.Organization.ID == 0 {
		return nil, fmt.Errorf("could not get organization: %s", jsonResponse)
	}
	handler.Organization = team.Data.Organization.ID

	return handler, nil
}
Example #3
0
// New returns a new producer
func New(listen string) (*Producer, error) {
	return &Producer{
		Port:   listen,
		logger: logrus.WithField("service", "github"),
	}, nil
}