RichardKnop / machinery

Machinery is an asynchronous task queue/job queue based on distributed message passing.
Mozilla Public License 2.0
7.52k stars 914 forks source link

Q: How to regist Periodic Tasks? #818

Closed xiaoz194 closed 1 week ago

xiaoz194 commented 2 weeks ago

I have some normal tasks,and one peridic task, how to regist peridic task? could u please support a completely code example? thanks!

my code like this, my normal task is ok, but periodic task is failure, error msg is : task "checkAllJobs" not regist.

{
        // .....

    // regist tasks
    tasksMap := initAsyncTaskMap()
    err = server.RegisterTasks(tasksMap) // ok
    // regist periodic task 
    signature := &tasks.Signature{
        Name: "checkAllJobs",
    }
    if !server.IsTaskRegistered("checkAllJobs") {
        logutil.G(ctx).Infof("checkAllJobs task not regist")
        err = server.RegisterPeriodicTask("1 * * * ?", "checkAllJobs", signature) // failed err: task not register
    }
    return server, err
}

func initAsyncTaskMap() map[string]interface{} {
    tasksMap := map[string]interface{}{
        "produce":      task.Produce,
        "checkAllJobs": task.CheckAllJobs,  // this is periodic tasks 
    }
    return tasksMap
}