Last active
          August 29, 2015 14:04 
        
      - 
      
- 
        Save rgarcia/e4da14e3204a91ef526b to your computer and use it in GitHub Desktop. 
    writes
  
        
  
    
      This file contains hidden or 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 main | |
| import ( | |
| "bytes" | |
| "fmt" | |
| "io" | |
| ) | |
| // Job is used by gearman clients after a job is submitted | |
| type Job interface { | |
| Data() io.Reader | |
| } | |
| type job struct { | |
| data io.ReadWriter | |
| } | |
| func (j job) Data() io.Reader { return j.data } | |
| type Client struct { | |
| } | |
| func (c Client) Submit(databuffer io.ReadWriter) Job { | |
| if databuffer == nil { | |
| databuffer = &bytes.Buffer{} | |
| } | |
| return job{data: databuffer} | |
| } | |
| type CaresAboutIndividualDataEvents struct { | |
| bytes.Buffer | |
| } | |
| func (caide CaresAboutIndividualDataEvents) Write(p []byte) (int, error) { | |
| fmt.Printf("got data event: '%s'\n", string(p)) | |
| return caide.Buffer.Write(p) | |
| } | |
| func main() { | |
| c := Client{} | |
| // option 1: write data events to a buffer, look at the buffer | |
| // as a whole when job completes | |
| // j := c.Submit(nil) | |
| // j.Wait() | |
| // fmt.Println("got:") | |
| // io.Copy(os.Stdout, j.Data()) | |
| // option 2: give the user the ability to capture data writes | |
| j := c.Submit(&CaresAboutIndividualDataEvents{}) | |
| // hack to simulate data events | |
| realj := j.(job) | |
| realj.data.Write([]byte("1")) | |
| realj.data.Write([]byte("2")) | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment