Task

Tasks work very similarly to cron jobs. Tasks are used to run a job outside the normal request/response cycle. These can be adhoc or scheduled to run regularly. Examples include: Reporting memory and goroutine status, periodically triggering GC or cleaning up log files at fixed intervals.

Creating a new Task

To initialize a task implement :

  1. tk1 := task.NewTask("tk1", "0 12 * * * *", func(ctx context.Context) error {
  2. fmt.Println("tk1")
  3. return nil
  4. })

The NewTask signature:

  1. NewTask(tname string, spec string, f TaskFunc) *Task
  • tname: Task name
  • spec: Task format. See below for details.
  • f: The function which will be run as the task.

To implement this task, add it to the global task list and start it.

  1. task.AddTask("tk1", tk1)
  2. task.StartTask()
  3. defer task.StopTask()

Testing the TaskFunc

Use the code below to test if the TaskFunc is working correctly.

  1. err := tk.Run()
  2. if err != nil {
  3. t.Fatal(err)
  4. }

spec in detail

spec specifies when the new Task will be run. Its format is the same as that of traditional crontab:

  1. // The first 6 parts are:
  2. // second: 0-59
  3. // minute: 0-59
  4. // hour: 1-23
  5. // day: 1-31
  6. // month: 1-12
  7. // weekdays: 0-6(0 is Sunday)
  8. // Some special sign:
  9. // *: any time
  10. // ,: separator. E.g.: 2,4 in the third part means run at 2 and 4 o'clock
  11. //   -: range. E.g.: 1-5 in the third part means run between 1 and 5 o'clock
  12. // /n : run once every n time. E.g.: */1 in the third part means run once every an hour. Same as 1-23/1
  13. /////////////////////////////////////////////////////////
  14. // 0/30 * * * * * run every 30 seconds
  15. // 0 43 21 * * * run at 21:43
  16. // 0 15 05 * * * run at 05:15
  17. // 0 0 17 * * * run at 17:00
  18. // 0 0 17 * * 1 run at 17:00 of every Monday
  19. // 0 0,10 17 * * 0,2,3 run at 17:00 and 17:10 of every Sunday, Tuesday and Wednesday
  20. // 0 0-10 17 1 * * run once every minute from 17:00 to 7:10 on 1st day of every month
  21. // 0 0 0 1,15 * 1 run at 0:00 on 1st and 15th of each month and every Monday
  22. // 0 42 4 1 * * run at 4:42 on 1st of every month
  23. // 0 0 21 * * 1-6 run at 21:00 from Monday to Saturday
  24. // 0 0,10,20,30,40,50 * * * * run every 10 minutes
  25. // 0 */10 * * * * run every 10 minutes
  26. // 0 * 1 * * * run every one minute from 1:00 to 1:59
  27. // 0 0 1 * * * run at 1:00
  28. // 0 0 */1 * * * run at :00 of every hour
  29. // 0 0 * * * * run at :00 of every hour
  30. // 0 2 8-20/3 * * * run at 8:02, 11:02, 14:02, 17:02 and 20:02
  31. // 0 30 5 1,15 * * run at 5:30 of 1st and 15th of every month

Debug module (Already moved to utils module)

We always use print for debugging. But the default output is not good enough for debugging. Beego provides this debug module

  • Display() print result to console
  • GetDisplayString() return the string

It print key/value pairs. The following code:

  1. Display("v1", 1, "v2", 2, "v3", 3)

will output:

  1. 2013/12/16 23:48:41 [Debug] at TestPrint() [/Users/astaxie/github/beego/task/debug_test.go:13]
  2. [Variables]
  3. v1 = 1
  4. v2 = 2
  5. v3 = 3

For pointer type:

  1. type mytype struct {
  2. next *mytype
  3. prev *mytype
  4. }
  5. var v1 = new(mytype)
  6. var v2 = new(mytype)
  7. v1.prev = nil
  8. v1.next = v2
  9. v2.prev = v1
  10. v2.next = nil
  11. Display("v1", v1, "v2", v2)

The output result

  1. 2013/12/16 23:48:41 [Debug] at TestPrintPoint() [/Users/astaxie/github/beego/task/debug_test.go:26]
  2. [Variables]
  3. v1 = &task.mytype{
  4. next: &task.mytype{
  5. next: nil,
  6. prev: 0x210335420,
  7. },
  8. prev: nil,
  9. }
  10. v2 = &task.mytype{
  11. next: nil,
  12. prev: &task.mytype{
  13. next: 0x210335430,
  14. prev: nil,
  15. },
  16. }