Skip to content

Instantly share code, notes, and snippets.

@nl5887
Created January 15, 2014 10:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nl5887/8433808 to your computer and use it in GitHub Desktop.
Save nl5887/8433808 to your computer and use it in GitHub Desktop.
Google App Engine boilerplate: Running background services.
package service
import (
_ "encoding/base64"
_ "fmt"
_ "net/http"
"appengine"
"appengine/urlfetch"
"appengine/runtime"
"time"
)
import "database/sql"
import _ "github.com/go-sql-driver/mysql"
import "github.com/nu7hatch/gouuid"
func init() {
http.HandleFunc("/_ah/warmup", handleWarmup)
http.HandleFunc("/_ah/start", handleStart)
http.HandleFunc("/_ah/stop", handleStop)
}
func handleWarmup(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
c.Infof("Warming up.")
}
func handleStart(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
c.Infof("pre run")
error := runtime.RunInBackground(c, runLoop)
c.Infof("post run")
c.Infof("error %s", error)
}
func runLoop(c appengine.Context) {
for {
c.Infof("Running")
time.Sleep(time.Millisecond * 2000)
}
return
}
func handleStop(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
c.Infof("Stopping.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment