From 05590bfb8caebf1bf072503d30d4956540f77888 Mon Sep 17 00:00:00 2001 From: mikespook Date: Tue, 25 Sep 2012 15:16:17 +0800 Subject: [PATCH] Info funcs added --HG-- branch : dev --- example/py/client.py | 12 ++++++++++++ example/worker.go | 2 ++ worker/func.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 worker/func.go diff --git a/example/py/client.py b/example/py/client.py index 1cb3777..94b6b07 100755 --- a/example/py/client.py +++ b/example/py/client.py @@ -32,6 +32,18 @@ def main(): except Exception as e: print type(e) + try: + completed_job_request = client.submit_job("SysInfo", "") + check_request_status(completed_job_request) + except Exception as e: + print type(e) + + try: + completed_job_request = client.submit_job("MemInfo", "") + check_request_status(completed_job_request) + except Exception as e: + print type(e) + if __name__ == '__main__': main() diff --git a/example/worker.go b/example/worker.go index 3975cac..0426727 100644 --- a/example/worker.go +++ b/example/worker.go @@ -52,6 +52,8 @@ func main() { w.AddFunc("ToUpper", ToUpper, worker.Immediately) w.AddFunc("ToUpperTimeOut5", ToUpperDelay10, 5) w.AddFunc("ToUpperTimeOut20", ToUpperDelay10, 20) + w.AddFunc("SysInfo", worker.SysInfo, worker.Immediately) + w.AddFunc("MemInfo", worker.MemInfo, worker.Immediately) go w.Work() sh := signal.NewHandler() sh.Bind(os.Interrupt, func() bool {return true}) diff --git a/worker/func.go b/worker/func.go new file mode 100644 index 0000000..374b7dd --- /dev/null +++ b/worker/func.go @@ -0,0 +1,31 @@ +package worker + +import ( + "runtime" + "encoding/json" +) + +type systemInfo struct { + GOOS, GOARCH, GOROOT, Version string + NumCPU, NumGoroutine int + NumCgoCall int64 +} + +func SysInfo(job *Job) ([]byte, error) { + return json.Marshal(&systemInfo{ + GOOS: runtime.GOOS, + GOARCH: runtime.GOARCH, + GOROOT: runtime.GOROOT(), + Version: runtime.Version(), + NumCPU: runtime.NumCPU(), + NumGoroutine: runtime.NumGoroutine(), + NumCgoCall: runtime.NumCgoCall(), + }) +} + +var memState runtime.MemStats + +func MemInfo(job *Job) ([]byte, error) { + runtime.ReadMemStats(&memState) + return json.Marshal(&memState) +}