Created
January 15, 2014 10:10
-
-
Save nl5887/8433808 to your computer and use it in GitHub Desktop.
Google App Engine boilerplate: Running background services.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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