Skip to content
gojuukaze edited this page Aug 27, 2022 · 2 revisions

get client

import "github.com/gojuukaze/YTask/v3"

ser := ytask.Server.NewServer(
		ytask.Config.Broker(&broker),
		ytask.Config.Backend(&backend),
		...
)

client = ser.GetClient()

send msg

// group1 : group name
// add : worker name
// 12,33 ... : func args
// return :
//   - taskId : taskId
//   - err : error
taskId,err:=client.Send("group1","add",12,33)

// set retry count
taskId,err=client.SetTaskCtl(client.RetryCount, 5).Send("group1","add",12,33)

// set delay time 
taskId,err=client.SetTaskCtl(client.RunAfter, 2*time.Second).Send("group1","add",12,33)

get result

The result can be obtained through GetResult() and GetResult2()

  • GetResult() : Return only when the task is over (task failure, completion is the end)
  • GetResult2() : If there is a record in the backend, it will return (generally, there will be a record when the task starts executing), which is usually used to obtain the progress of the workflow
// taskId :
// 3*time.Second : timeout
// 300*time.Millisecond : sleep time
result, _ := client.GetResult(taskId, 3*time.Second, 300*time.Millisecond)

// get worker func return
if result.IsSuccess(){
    // get worker func return
    a,err:=result.GetInt64(0)
    b,err:=result.GetBool(1)
    
    // or
    var a int
    var b bool
    err:=result.Get(0, &a)
    err:=result.Get(1, &b)

    // or
    var a int
    var b bool
    err:=result.Gets(&a, &b)
}

Warning!!!
Although YTask provides the ability to get results, don't rely on transitions.
If the backend error causes the result to not be saved, YTask will not retry again. Because the task state and result are saved by the same goroutine that runs the task, continuous retry will cause the worker to be occupied.
If you need task results in particular, it is recommended that you save them yourself in the task function.