本文整理汇总了Golang中github.com/Lupino/periodic/driver.Job类的典型用法代码示例。如果您正苦于以下问题:Golang Job类的具体用法?Golang Job怎么用?Golang Job使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Job类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: handleRemoveJob
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
}
示例2: SubmitJob
func (sched *Sched) SubmitJob(grabItem GrabItem, job driver.Job) bool {
defer sched.JobLocker.Unlock()
sched.JobLocker.Lock()
if job.Name == "" {
sched.driver.Delete(job.Id)
return true
}
if _, ok := sched.procQueue[job.Id]; ok {
return true
}
if !grabItem.w.alive {
return false
}
if err := grabItem.w.HandleDo(grabItem.msgId, job); err != nil {
grabItem.w.alive = false
return false
}
now := time.Now()
current := int64(now.Unix())
job.Status = driver.JOB_STATUS_PROC
job.RunAt = current
sched.driver.Save(&job)
sched.IncrStatProc(job)
sched.pushRevertPQ(job)
sched.NotifyRevertTimer()
sched.procQueue[job.Id] = job
sched.grabQueue.Remove(grabItem)
return true
}
示例3: pushJobPQ
func (sched *Sched) pushJobPQ(job driver.Job) bool {
defer sched.PQLocker.Unlock()
sched.PQLocker.Lock()
if job.IsReady() {
item := &queue.Item{
Value: job.ID,
Priority: job.SchedAt,
}
if sched.cacheItem != nil && item.Priority < sched.cacheItem.Priority {
if job.ID == sched.cacheItem.Value {
return true
}
job, _ = sched.driver.Get(sched.cacheItem.Value)
sched.cacheItem = item
if job.ID <= 0 || !job.IsReady() {
return false
}
}
pq, ok := sched.jobPQ[job.Func]
if !ok {
pq1 := make(queue.PriorityQueue, 0)
pq = &pq1
sched.jobPQ[job.Func] = pq
heap.Init(pq)
}
heap.Push(pq, item)
return true
}
return false
}
示例4: SubmitJob
func SubmitJob(entryPoint string, job driver.Job) {
parts := strings.SplitN(entryPoint, "://", 2)
c, err := net.Dial(parts[0], parts[1])
if err != nil {
log.Fatal(err)
}
conn := protocol.NewClientConn(c)
defer conn.Close()
err = conn.Send(protocol.TYPE_CLIENT.Bytes())
if err != nil {
log.Fatal(err)
}
var msgId = []byte("100")
buf := bytes.NewBuffer(nil)
buf.Write(msgId)
buf.Write(protocol.NULL_CHAR)
buf.WriteByte(byte(protocol.SUBMIT_JOB))
buf.Write(protocol.NULL_CHAR)
buf.Write(job.Bytes())
err = conn.Send(buf.Bytes())
if err != nil {
log.Fatal(err)
}
payload, err := conn.Receive()
if err != nil {
log.Fatal(err)
}
_, cmd, _ := protocol.ParseCommand(payload)
fmt.Printf("%s\n", cmd.String())
}
示例5: submitJob
func (sched *Sched) submitJob(item grabItem, job driver.Job) bool {
defer sched.jobLocker.Unlock()
sched.jobLocker.Lock()
if job.Name == "" {
sched.driver.Delete(job.ID)
return true
}
if _, ok := sched.procQueue[job.ID]; ok {
return true
}
if !item.w.alive {
return false
}
if err := item.w.handleJobAssign(item.msgID, job); err != nil {
item.w.alive = false
return false
}
now := time.Now()
current := int64(now.Unix())
job.SetProc()
job.RunAt = current
sched.driver.Save(&job)
sched.incrStatProc(job)
sched.pushRevertPQ(job)
sched.notifyRevertTimer()
sched.procQueue[job.ID] = job
sched.grabQueue.remove(item)
return true
}
示例6: handleRemoveJob
func (c *httpClient) handleRemoveJob(req *http.Request) {
var job driver.Job
var e error
var sched = c.sched
defer sched.jobLocker.Unlock()
sched.jobLocker.Lock()
url := req.URL.String()
funcName := url[1:]
if funcName == "" {
funcName = req.FormValue("func")
}
name := req.FormValue("name")
job, e = sched.driver.GetOne(funcName, 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 {
c.sendErrResponse(e)
} else {
c.sendResponse("200 OK", []byte("{\"msg\": \""+protocol.SUCCESS.String()+"\"}"))
}
}
示例7: removeRevertPQ
func (sched *Sched) removeRevertPQ(job driver.Job) {
defer sched.PQLocker.Unlock()
sched.PQLocker.Lock()
if job.IsProc() && job.Timeout > 0 {
for _, item := range sched.revertPQ {
if item.Value == job.ID {
heap.Remove(&sched.revertPQ, item.Index)
break
}
}
}
}
示例8: HandleDo
func (worker *Worker) HandleDo(msgId int64, job driver.Job) (err error) {
defer worker.locker.Unlock()
worker.locker.Lock()
worker.jobQueue[job.Id] = job
buf := bytes.NewBuffer(nil)
buf.WriteString(strconv.FormatInt(msgId, 10))
buf.Write(protocol.NULL_CHAR)
buf.WriteString(strconv.FormatInt(job.Id, 10))
buf.Write(protocol.NULL_CHAR)
buf.Write(job.Bytes())
err = worker.conn.Send(buf.Bytes())
return
}
示例9: handleJobAssign
func (w *worker) handleJobAssign(msgId []byte, job driver.Job) (err error) {
defer w.locker.Unlock()
w.locker.Lock()
w.jobQueue[job.Id] = job
buf := bytes.NewBuffer(nil)
buf.Write(msgId)
buf.Write(protocol.NULL_CHAR)
buf.Write(protocol.JOB_ASSIGN.Bytes())
buf.Write(protocol.NULL_CHAR)
buf.WriteString(strconv.FormatInt(job.Id, 10))
buf.Write(protocol.NULL_CHAR)
buf.Write(job.Bytes())
err = w.conn.Send(buf.Bytes())
return
}
示例10: pushRevertPQ
func (sched *Sched) pushRevertPQ(job driver.Job) {
defer sched.PQLocker.Unlock()
sched.PQLocker.Lock()
if job.IsProc() && job.Timeout > 0 {
runAt := job.RunAt
if runAt == 0 {
runAt = job.SchedAt
}
item := &queue.Item{
Value: job.ID,
Priority: runAt + job.Timeout,
}
heap.Push(&sched.revertPQ, item)
}
}
示例11: handleSubmitJob
func (c *httpClient) handleSubmitJob(req *http.Request) {
var job driver.Job
var e error
var sched = c.sched
defer sched.jobLocker.Unlock()
sched.jobLocker.Lock()
url := req.URL.String()
funcName := url[1:]
if funcName == "" {
funcName = req.FormValue("func")
}
job.Name = req.FormValue("name")
job.Func = funcName
job.Args = req.FormValue("args")
job.Timeout, _ = strconv.ParseInt(req.FormValue("timeout"), 10, 64)
job.SchedAt, _ = strconv.ParseInt(req.FormValue("sched_at"), 10, 64)
if job.Name == "" || job.Func == "" {
c.sendErrResponse(errors.New("job name or func is required"))
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 job.IsProc() {
sched.decrStatProc(oldJob)
sched.removeRevertPQ(job)
changed = true
}
isNew = false
}
e = sched.driver.Save(&job)
if e != nil {
c.sendErrResponse(e)
return
}
if isNew {
sched.incrStatJob(job)
}
if isNew || changed {
sched.pushJobPQ(job)
}
sched.notifyJobTimer()
c.sendResponse("200 OK", []byte("{\"msg\": \""+protocol.SUCCESS.String()+"\"}"))
return
}
示例12: Save
func (r RedisDriver) Save(job *driver.Job) (err error) {
defer r.RWLocker.Unlock()
r.RWLocker.Lock()
var key string
var prefix = REDIS_PREFIX + job.Func + ":"
var conn = r.pool.Get()
defer conn.Close()
if job.Id > 0 {
old, e := r.get(job.Id)
key = REDIS_PREFIX + strconv.FormatInt(job.Id, 10)
if e != nil || old.Id < 1 {
err = errors.New(fmt.Sprintf("Update Job %d fail, the old job is not exists.", job.Id))
return
}
r.cache.Remove(key)
if old.Name != job.Name {
if _, e := conn.Do("ZERM", prefix+"name", old.Name); e != nil {
log.Printf("Error: ZREM %s %s failed\n", prefix+"name", old.Name)
}
}
} else {
job.Id, err = redis.Int64(conn.Do("INCRBY", REDIS_PREFIX+"sequence", 1))
if err != nil {
return
}
}
idx, _ := redis.Int64(conn.Do("ZSCORE", prefix+"name", job.Name))
if idx > 0 && idx != job.Id {
err = errors.New("Duplicate Job name: " + job.Name)
return
}
key = REDIS_PREFIX + strconv.FormatInt(job.Id, 10)
_, err = conn.Do("SET", key, job.Bytes())
if err == nil {
if _, e := conn.Do("ZADD", prefix+"name", job.Id, job.Name); e != nil {
log.Printf("Error: ZADD %s %d %s fail\n", prefix+"name", job.Id, job.Name)
}
if _, e := conn.Do("ZADD", REDIS_PREFIX+"ID", job.Id, strconv.FormatInt(job.Id, 10)); e != nil {
log.Printf("Error: ZADD %s %d %d fail\n", REDIS_PREFIX+"ID", job.Id, job.Id)
}
}
return
}
示例13: handleSubmitJob
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
}
示例14: HandleSubmitJob
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
}
示例15: Save
func (l LevelDBDriver) Save(job *driver.Job) (err error) {
defer l.RWLocker.Unlock()
l.RWLocker.Lock()
batch := new(leveldb.Batch)
var isNew = true
if job.Id > 0 {
isNew = false
} else {
last_id, e := l.db.Get([]byte(PRE_SEQUENCE+"JOB"), nil)
if e != nil || last_id == nil {
job.Id = 1
} else {
id, _ := strconv.ParseInt(string(last_id), 10, 64)
job.Id = id + 1
}
}
var strId = strconv.FormatInt(job.Id, 10)
if isNew {
batch.Put([]byte(PRE_SEQUENCE+"JOB"), []byte(strId))
batch.Put([]byte(PRE_JOB_FUNC+job.Func+":"+job.Name), []byte(strId))
} else {
old, e := l.get(job.Id)
if e != nil || old.Id == 0 {
err = errors.New(fmt.Sprintf("Update Job %d fail, the old job is not exists.", job.Id))
return
}
l.cache.Remove(PRE_JOB + strId)
if old.Name != job.Name {
batch.Delete([]byte(PRE_JOB_FUNC + job.Func + ":" + old.Name))
batch.Put([]byte(PRE_JOB_FUNC+job.Func+":"+job.Name), []byte(strId))
}
}
batch.Put([]byte(PRE_JOB+strId), job.Bytes())
err = l.db.Write(batch, nil)
return
}