예제 #1
0
파일: task.go 프로젝트: GoToolz/Task
func run(messages []sqs.Message) {

	numMessages := len(messages)
	log.Debugln("worker: Received", numMessages, " tasks")

	var wg sync.WaitGroup
	wg.Add(numMessages)
	for i := range messages {
		go func(m *sqs.Message) {
			// launch goroutine
			defer func() {
				if err := recover(); err != nil {

					trace := make([]byte, 2048)
					runtime.Stack(trace, true)

					log.Criticalln("worker:", err, string(trace))
				}
				wg.Done()
			}()

			if err := handleMessage(m); err != nil {
				log.Errorln("worker:", err)
			}
		}(&messages[i])
	}

	wg.Wait()
}
예제 #2
0
파일: app.go 프로젝트: Wayt/ImgBot
func main() {

	app := happyngine.NewAPI()

	// Setup seed
	rand.Seed(time.Now().UnixNano())

	// Setup Origin
	if origin := env.Get("ALLOW_ORIGIN"); len(origin) > 0 {
		app.Headers["Access-Control-Allow-Origin"] = origin
	}

	if cacheControl := env.Get("CACHE_CONTROL"); len(cacheControl) > 0 {
		app.Headers["Cache-Control"] = cacheControl
	}

	// Register actions
	app.AddRoute("GET", "/:bucket/:file", newGetFileAction)

	// Setup custuom 404 handler
	app.Error404Handler = func(ctx *happyngine.Context, err interface{}) {

		ctx.Send(http.StatusNotFound, `not found 404`)
	}

	// Setup custuom panic handler
	app.PanicHandler = func(ctx *happyngine.Context, err interface{}) {

		ctx.Send(500, `{"error":"internal_error"}`)

		trace := make([]byte, 1024)
		runtime.Stack(trace, true)

		ctx.Criticalln(err, string(trace))
	}

	log.Debugln("Running...")
	if err := app.Run(":8080"); err != nil {
		log.Criticalln("app.Run:", err)
	}
}