Пример #1
0
// in loop
func (client *Client) inLoop() {
	defer common.DisablePanic()
	for {
		rel, err := client.read()
		if err != nil {
			if err == common.ErrConnection {
				client.Close()
			}
			client.err(err)
			break
		}
		job, err := decodeJob(rel)
		if err != nil {
			client.err(err)
			continue
			//break
		}
		switch job.DataType {
		case common.ERROR:
			_, err := common.GetError(job.Data)
			client.err(err)
		case common.WORK_DATA, common.WORK_WARNING, common.WORK_STATUS,
			common.WORK_COMPLETE, common.WORK_FAIL, common.WORK_EXCEPTION:
			client.handleJob(job)
		case common.ECHO_RES:
			client.handleEcho(job)
		case common.JOB_CREATED:
			client.handleCreated(job)
		case common.STATUS_RES:
			client.handleStatus(job)
		default:
			break
		}
	}
}
Пример #2
0
// Main loop
func (worker *Worker) Work() {
	defer func() {
		worker.running = false
		for _, v := range worker.agents {
			v.Close()
		}
	}()
	for funcname, f := range worker.funcs {
		worker.addFunc(funcname, f.timeout)
	}
	worker.running = true
	for _, v := range worker.agents {
		go v.Work()
	}
	ok := true
	for ok {
		var job *Job
		if job, ok = <-worker.in; ok {
			go func() {
				defer job.Close()
				switch job.DataType {
				case common.ERROR:
					_, err := common.GetError(job.Data)
					worker.err(err)
				case common.JOB_ASSIGN, common.JOB_ASSIGN_UNIQ:
					if err := worker.exec(job); err != nil {
						worker.err(err)
					}
				default:
					worker.handleJob(job)
				}
			}()
		}
	}
}
Пример #3
0
func (worker *Worker) dealJob(job *Job) {
	defer func() {
		job.Close()
		if worker.running && worker.limit != nil {
			<-worker.limit
		}
	}()
	switch job.DataType {
	case common.ERROR:
		_, err := common.GetError(job.Data)
		worker.err(err)
	case common.JOB_ASSIGN, common.JOB_ASSIGN_UNIQ:
		if err := worker.exec(job); err != nil {
			worker.err(err)
		}
	default:
		worker.handleJob(job)
	}
}