action_worker.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package example_basics
  2. // @snippet_begin(ActionWorkerExample)
  3. import (
  4. "context"
  5. "fmt"
  6. "time"
  7. "github.com/qor5/admin/presets"
  8. "github.com/qor5/admin/worker"
  9. "github.com/qor5/ui/vuetify"
  10. "github.com/qor5/web"
  11. h "github.com/theplant/htmlgo"
  12. "gorm.io/gorm"
  13. )
  14. type ExampleResource struct {
  15. gorm.Model
  16. Name string
  17. }
  18. func MountActionWorker(b *presets.Builder) {
  19. mb := b.Model(&ExampleResource{})
  20. mb.Listing().ActionsAsMenu(true)
  21. wb := worker.New(DB)
  22. wb.Configure(b)
  23. defer wb.Listen()
  24. addActionJobs(mb, wb)
  25. }
  26. func addActionJobs(mb *presets.ModelBuilder, wb *worker.Builder) {
  27. lb := mb.Listing()
  28. noParametersJob := wb.ActionJob(
  29. "No parameters",
  30. mb,
  31. func(ctx context.Context, job worker.QorJobInterface) error {
  32. for i := 1; i <= 10; i++ {
  33. select {
  34. case <-ctx.Done():
  35. job.AddLog("job aborted")
  36. return nil
  37. default:
  38. job.SetProgress(uint(i * 10))
  39. time.Sleep(time.Second)
  40. }
  41. }
  42. job.SetProgressText(`<a href="https://qor5-test.s3.ap-northeast-1.amazonaws.com/system/media_libraries/37/file.@qor_preview.png">Please download this file</a>`)
  43. return nil
  44. },
  45. ).Description("This test demo is used to show that an no parameter job can be executed")
  46. parametersBoxJob := wb.ActionJob(
  47. "Parameter input box",
  48. mb,
  49. func(ctx context.Context, job worker.QorJobInterface) error {
  50. for i := 1; i <= 10; i++ {
  51. select {
  52. case <-ctx.Done():
  53. job.AddLog("job aborted")
  54. return nil
  55. default:
  56. job.SetProgress(uint(i * 10))
  57. time.Sleep(time.Second)
  58. }
  59. }
  60. job.SetProgressText(`<a href="https://qor5-test.s3.ap-northeast-1.amazonaws.com/system/media_libraries/37/file.@qor_preview.png">Please download this file</a>`)
  61. return nil
  62. },
  63. ).Description("This test demo is used to show that an input box when there are parameters").
  64. Params(&struct{ Name string }{})
  65. displayLogJob := wb.ActionJob(
  66. "Display log",
  67. mb,
  68. func(ctx context.Context, job worker.QorJobInterface) error {
  69. for i := 1; i <= 10; i++ {
  70. select {
  71. case <-ctx.Done():
  72. job.AddLog("job aborted")
  73. return nil
  74. default:
  75. job.SetProgress(uint(i * 10))
  76. job.AddLog(fmt.Sprintf("%v", i))
  77. time.Sleep(time.Second)
  78. }
  79. }
  80. job.SetProgressText(`<a href="https://qor5-test.s3.ap-northeast-1.amazonaws.com/system/media_libraries/37/file.@qor_preview.png">Please download this file</a>`)
  81. return nil
  82. },
  83. ).Description("This test demo is used to show the log section of this job").
  84. Params(&struct{ Name string }{}).
  85. DisplayLog(true).
  86. ProgressingInterval(4000)
  87. getArgsJob := wb.ActionJob(
  88. "Get Args",
  89. mb,
  90. func(ctx context.Context, job worker.QorJobInterface) error {
  91. jobInfo, err := job.GetJobInfo()
  92. if err != nil {
  93. return err
  94. }
  95. job.AddLog(fmt.Sprintf("Action Params Name is %#+v", jobInfo.Argument.(*struct{ Name string }).Name))
  96. job.AddLog(fmt.Sprintf("Origina Context AuthInfo is %#+v", jobInfo.Context["AuthInfo"]))
  97. job.AddLog(fmt.Sprintf("Origina Context URL is %#+v", jobInfo.Context["URL"]))
  98. for i := 1; i <= 10; i++ {
  99. select {
  100. case <-ctx.Done():
  101. return nil
  102. default:
  103. job.SetProgress(uint(i * 10))
  104. time.Sleep(time.Second)
  105. }
  106. }
  107. job.SetProgressText(`<a href="https://qor5-test.s3.ap-northeast-1.amazonaws.com/system/media_libraries/37/file.@qor_preview.png">Please download this file</a>`)
  108. return nil
  109. },
  110. ).Description("This test demo is used to show how to get the action's arguments and original page context").
  111. Params(&struct{ Name string }{}).
  112. DisplayLog(true).
  113. ContextHandler(func(ctx *web.EventContext) map[string]interface{} {
  114. auth, err := ctx.R.Cookie("auth")
  115. if err == nil {
  116. return map[string]interface{}{"AuthInfo": auth.Value}
  117. }
  118. return nil
  119. })
  120. lb.Action("Action Job - No parameters").
  121. ButtonCompFunc(
  122. func(ctx *web.EventContext) h.HTMLComponent {
  123. return vuetify.VBtn("Action Job - No parameters").Color("secondary").Depressed(true).Class("ml-2").
  124. Attr("@click", noParametersJob.URL())
  125. })
  126. lb.Action("Action Job - Parameter input box").
  127. ButtonCompFunc(
  128. func(ctx *web.EventContext) h.HTMLComponent {
  129. return vuetify.VBtn("Action Job - Parameter input box").Color("secondary").Depressed(true).Class("ml-2").
  130. Attr("@click", parametersBoxJob.URL())
  131. })
  132. lb.Action("Action Job - Display log").
  133. ButtonCompFunc(
  134. func(ctx *web.EventContext) h.HTMLComponent {
  135. return vuetify.VBtn("Action Job - Display log").Color("secondary").Depressed(true).Class("ml-2").
  136. Attr("@click", displayLogJob.URL())
  137. })
  138. lb.Action("Action Job - Get Args").
  139. ButtonCompFunc(
  140. func(ctx *web.EventContext) h.HTMLComponent {
  141. return vuetify.VBtn("Action Job - Get Args").Color("secondary").Depressed(true).Class("ml-2").
  142. Attr("@click", getArgsJob.URL())
  143. })
  144. }
  145. // @snippet_end