Example #1
0
func (c *client) handleRemoveJob(msgID, payload []byte) (err error) {
	var job driver.Job
	var e error
	var conn = c.conn
	var sched = c.sched
	defer sched.jobLocker.Unlock()
	sched.jobLocker.Lock()
	job, e = driver.NewJob(payload)
	if e != nil {
		err = conn.Send([]byte(e.Error()))
		return
	}
	job, e = sched.driver.GetOne(job.Func, job.Name)
	if e == nil && job.ID > 0 {
		if _, ok := sched.procQueue[job.ID]; ok {
			delete(sched.procQueue, job.ID)
		}
		sched.driver.Delete(job.ID)
		sched.decrStatJob(job)
		if job.IsProc() {
			sched.decrStatProc(job)
			sched.removeRevertPQ(job)
		}
		sched.notifyJobTimer()
	}

	if e != nil {
		err = conn.Send([]byte(e.Error()))
	} else {
		err = c.handleCommand(msgID, protocol.SUCCESS)
	}
	return
}
Example #2
0
func extraJob(payload []byte) (job driver.Job, jobHandle []byte, err error) {
	parts := bytes.SplitN(payload, protocol.NullChar, 4)
	if len(parts) != 4 {
		err = errors.New("Invalid payload " + string(payload))
		return
	}
	job, err = driver.NewJob(parts[3])
	jobHandle = parts[2]
	return
}
Example #3
0
// Value returns the current job.
func (iter *Iterator) Value() (job driver.Job) {
	data := iter.iter.Value()
	if iter.Func == nil {
		job, _ = driver.NewJob(data)
		return
	}
	jobID, _ := strconv.ParseInt(string(data), 10, 64)
	job, _ = iter.l.get(jobID)
	return
}
Example #4
0
func (l Driver) get(jobID int64) (job driver.Job, err error) {
	var data []byte
	var key = PREJOB + strconv.FormatInt(jobID, 10)
	if val, hit := l.cache.Get(key); hit {
		return val.(driver.Job), nil
	}
	data, err = l.db.Get([]byte(key), nil)
	if err != nil {
		return
	}
	job, err = driver.NewJob(data)
	if err == nil {
		l.cache.Add(key, job)
	}
	return
}
Example #5
0
func (r RedisDriver) get(jobId int64) (job driver.Job, err error) {
	var data []byte
	var conn = r.pool.Get()
	defer conn.Close()
	var key = REDIS_PREFIX + strconv.FormatInt(jobId, 10)
	if val, hit := r.cache.Get(key); hit {
		return val.(driver.Job), nil
	}
	data, err = redis.Bytes(conn.Do("GET", key))
	if err != nil {
		return
	}
	job, err = driver.NewJob(data)
	if err == nil {
		r.cache.Add(key, job)
	}
	return
}
Example #6
0
func (c *client) handleSubmitJob(msgID []byte, payload []byte) (err error) {
	var job driver.Job
	var e error
	var conn = c.conn
	var sched = c.sched
	defer sched.jobLocker.Unlock()
	sched.jobLocker.Lock()
	job, e = driver.NewJob(payload)
	if e != nil {
		err = conn.Send([]byte(e.Error()))
		return
	}
	isNew := true
	changed := false
	job.SetReady()
	oldJob, e := sched.driver.GetOne(job.Func, job.Name)
	if e == nil && oldJob.ID > 0 {
		job.ID = oldJob.ID
		if oldJob.IsProc() {
			sched.decrStatProc(oldJob)
			sched.removeRevertPQ(job)
			changed = true
		}
		isNew = false
	}
	e = sched.driver.Save(&job)
	if e != nil {
		err = conn.Send([]byte(e.Error()))
		return
	}

	if isNew {
		sched.incrStatJob(job)
	}
	if isNew || changed {
		sched.pushJobPQ(job)
	}
	sched.notifyJobTimer()
	err = c.handleCommand(msgID, protocol.SUCCESS)
	return
}
Example #7
0
func (client *Client) HandleSubmitJob(msgId int64, payload []byte) (err error) {
	var job driver.Job
	var e error
	var conn = client.conn
	var sched = client.sched
	defer sched.JobLocker.Unlock()
	sched.JobLocker.Lock()
	job, e = driver.NewJob(payload)
	if e != nil {
		err = conn.Send([]byte(e.Error()))
		return
	}
	is_new := true
	changed := false
	job.Status = driver.JOB_STATUS_READY
	oldJob, e := sched.driver.GetOne(job.Func, job.Name)
	if e == nil && oldJob.Id > 0 {
		job.Id = oldJob.Id
		if oldJob.Status == driver.JOB_STATUS_PROC {
			sched.DecrStatProc(oldJob)
			sched.removeRevertPQ(job)
			changed = true
		}
		is_new = false
	}
	e = sched.driver.Save(&job)
	if e != nil {
		err = conn.Send([]byte(e.Error()))
		return
	}

	if is_new {
		sched.IncrStatJob(job)
	}
	if is_new || changed {
		sched.pushJobPQ(job)
	}
	sched.NotifyJobTimer()
	err = client.HandleCommand(msgId, protocol.SUCCESS)
	return
}
Example #8
0
// GetOne get a job with func and name.
func (l Driver) GetOne(Func, name string) (job driver.Job, err error) {
	defer l.RWLocker.Unlock()
	l.RWLocker.Lock()
	var data []byte
	var key = PREFUNC + Func + ":" + name
	data, err = l.db.Get([]byte(key), nil)
	if err != nil {
		return
	}
	key = PREJOB + string(data)
	if val, hit := l.cache.Get(key); hit {
		return val.(driver.Job), nil
	}
	data, err = l.db.Get([]byte(key), nil)
	if err != nil {
		return
	}
	job, err = driver.NewJob(data)
	if err == nil {
		l.cache.Add(key, job)
	}
	return
}