فهرست منبع

Merge pull request #42 from qor5/action-worker-doc

add action worker doc
xuxinx 1 سال پیش
والد
کامیت
44dcbd5974

+ 6 - 0
docs/appendix/all-demo-examples.html

@@ -533,6 +533,12 @@
 <a href='https://github.com/qor5/docs/tree/main/docsrc/examples/example_basics/worker.go' target='_blank'>Source</a>
 </li>
 
+<li>
+<a href='/samples/action_worker/example-resources' target='_blank'>Action Worker</a>
+ | 
+<a href='https://github.com/qor5/docs/tree/main/docsrc/examples/example_basics/action_worker.go' target='_blank'>Source</a>
+</li>
+
 <li>
 <a href='/samples/composite-component-sample1' target='_blank'>Composite New Component With Go</a>
  | 

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 4 - 0
docs/basics/worker.html


تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 0 - 0
docs/search_indexes.json


+ 10 - 0
docsrc/content/basics/worker.go

@@ -35,4 +35,14 @@ Once registered with QOR Admin, Worker will provide a Workers section in the nav
 		path.Join(example_basics.WorkerExamplePath, "/workers"),
 		"example_basics/worker.go",
 	),
+	Markdown(`
+## Action Worker
+Action Worker is used to visualize the progress of long-running actions.
+    `),
+	ch.Code(generated.ActionWorkerExample).Language("go"),
+	utils.Demo(
+		"Action Worker",
+		path.Join(example_basics.ActionWorkerExamplePath, "/example-resources"),
+		"example_basics/action_worker.go",
+	),
 ).Slug("basics/worker").Title("Worker")

+ 162 - 0
docsrc/examples/example_basics/action_worker.go

@@ -0,0 +1,162 @@
+package example_basics
+
+// @snippet_begin(ActionWorkerExample)
+import (
+	"context"
+	"fmt"
+	"time"
+
+	"github.com/qor5/admin/presets"
+	"github.com/qor5/admin/worker"
+	"github.com/qor5/ui/vuetify"
+	"github.com/qor5/web"
+	h "github.com/theplant/htmlgo"
+	"gorm.io/gorm"
+)
+
+type ExampleResource struct {
+	gorm.Model
+	Name string
+}
+
+func MountActionWorker(b *presets.Builder) {
+	mb := b.Model(&ExampleResource{})
+	mb.Listing().ActionsAsMenu(true)
+
+	wb := worker.New(DB)
+	wb.Configure(b)
+	defer wb.Listen()
+
+	addActionJobs(mb, wb)
+}
+
+func addActionJobs(mb *presets.ModelBuilder, wb *worker.Builder) {
+	lb := mb.Listing()
+
+	noParametersJob := wb.ActionJob(
+		"No parameters",
+		mb,
+		func(ctx context.Context, job worker.QorJobInterface) error {
+			for i := 1; i <= 10; i++ {
+				select {
+				case <-ctx.Done():
+					job.AddLog("job aborted")
+					return nil
+				default:
+					job.SetProgress(uint(i * 10))
+					time.Sleep(time.Second)
+				}
+			}
+			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>`)
+			return nil
+		},
+	).Description("This test demo is used to show that an no parameter job can be executed")
+
+	parametersBoxJob := wb.ActionJob(
+		"Parameter input box",
+		mb,
+		func(ctx context.Context, job worker.QorJobInterface) error {
+			for i := 1; i <= 10; i++ {
+				select {
+				case <-ctx.Done():
+					job.AddLog("job aborted")
+					return nil
+				default:
+					job.SetProgress(uint(i * 10))
+					time.Sleep(time.Second)
+				}
+			}
+			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>`)
+			return nil
+		},
+	).Description("This test demo is used to show that an input box when there are parameters").
+		Params(&struct{ Name string }{})
+
+	displayLogJob := wb.ActionJob(
+		"Display log",
+		mb,
+		func(ctx context.Context, job worker.QorJobInterface) error {
+			for i := 1; i <= 10; i++ {
+				select {
+				case <-ctx.Done():
+					job.AddLog("job aborted")
+					return nil
+				default:
+					job.SetProgress(uint(i * 10))
+					job.AddLog(fmt.Sprintf("%v", i))
+					time.Sleep(time.Second)
+				}
+			}
+			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>`)
+			return nil
+		},
+	).Description("This test demo is used to show the log section of this job").
+		Params(&struct{ Name string }{}).
+		DisplayLog(true).
+		ProgressingInterval(4000)
+
+	getArgsJob := wb.ActionJob(
+		"Get Args",
+		mb,
+		func(ctx context.Context, job worker.QorJobInterface) error {
+			jobInfo, err := job.GetJobInfo()
+			if err != nil {
+				return err
+			}
+
+			job.AddLog(fmt.Sprintf("Action Params Name is  %#+v", jobInfo.Argument.(*struct{ Name string }).Name))
+			job.AddLog(fmt.Sprintf("Origina Context AuthInfo is  %#+v", jobInfo.Context["AuthInfo"]))
+			job.AddLog(fmt.Sprintf("Origina Context URL is  %#+v", jobInfo.Context["URL"]))
+
+			for i := 1; i <= 10; i++ {
+				select {
+				case <-ctx.Done():
+					return nil
+				default:
+					job.SetProgress(uint(i * 10))
+					time.Sleep(time.Second)
+				}
+			}
+			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>`)
+			return nil
+		},
+	).Description("This test demo is used to show how to get the action's arguments and original page context").
+		Params(&struct{ Name string }{}).
+		DisplayLog(true).
+		ContextHandler(func(ctx *web.EventContext) map[string]interface{} {
+			auth, err := ctx.R.Cookie("auth")
+			if err == nil {
+				return map[string]interface{}{"AuthInfo": auth.Value}
+			}
+			return nil
+		})
+
+	lb.Action("Action Job - No parameters").
+		ButtonCompFunc(
+			func(ctx *web.EventContext) h.HTMLComponent {
+				return vuetify.VBtn("Action Job - No parameters").Color("secondary").Depressed(true).Class("ml-2").
+					Attr("@click", noParametersJob.URL())
+			})
+
+	lb.Action("Action Job - Parameter input box").
+		ButtonCompFunc(
+			func(ctx *web.EventContext) h.HTMLComponent {
+				return vuetify.VBtn("Action Job - Parameter input box").Color("secondary").Depressed(true).Class("ml-2").
+					Attr("@click", parametersBoxJob.URL())
+			})
+	lb.Action("Action Job - Display log").
+		ButtonCompFunc(
+			func(ctx *web.EventContext) h.HTMLComponent {
+				return vuetify.VBtn("Action Job - Display log").Color("secondary").Depressed(true).Class("ml-2").
+					Attr("@click", displayLogJob.URL())
+			})
+
+	lb.Action("Action Job - Get Args").
+		ButtonCompFunc(
+			func(ctx *web.EventContext) h.HTMLComponent {
+				return vuetify.VBtn("Action Job - Get Args").Color("secondary").Depressed(true).Class("ml-2").
+					Attr("@click", getArgsJob.URL())
+			})
+}
+
+// @snippet_end

+ 26 - 0
docsrc/examples/example_basics/action_worker_mock_que.go

@@ -0,0 +1,26 @@
+package example_basics
+
+import (
+	"github.com/qor5/admin/presets"
+	"github.com/qor5/admin/presets/gorm2op"
+	"github.com/qor5/admin/worker"
+)
+
+func ActionWorkerExampleMock(b *presets.Builder) {
+	if err := DB.AutoMigrate(&ExampleResource{}); err != nil {
+		panic(err)
+	}
+
+	b.URIPrefix(ActionWorkerExamplePath).
+		DataOperator(gorm2op.DataOperator(DB))
+
+	mb := b.Model(&ExampleResource{})
+	mb.Listing().ActionsAsMenu(true)
+
+	wb := worker.NewWithQueue(DB, Que)
+	wb.Configure(b)
+	addActionJobs(mb, wb)
+	wb.Listen()
+}
+
+const ActionWorkerExamplePath = "/samples/action_worker"

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 0 - 0
docsrc/generated/examples-generated.go


+ 7 - 0
docsrc/mux.go

@@ -725,5 +725,12 @@ func SamplesHandler(prefix string) http.Handler {
 		c23,
 	)
 
+	c24 := presets.New().AssetFunc(addGA)
+	example_basics.ActionWorkerExampleMock(c24)
+	mux.Handle(
+		example_basics.ActionWorkerExamplePath+"/",
+		c24,
+	)
+
 	return mux
 }

برخی فایل ها در این مقایسه diff نمایش داده نمی شوند زیرا تعداد فایل ها بسیار زیاد است