func reset() { db.DropTable(&database.Translation{}) database.New(db) Admin := admin.New(&qor.Config{DB: db}) Worker = worker.New() Admin.AddResource(Worker) I18n = i18n.New(database.New(db)) I18n.SaveTranslation(&i18n.Translation{Key: "qor_admin.title", Value: "title", Locale: "en-US"}) I18n.SaveTranslation(&i18n.Translation{Key: "qor_admin.subtitle", Value: "subtitle", Locale: "en-US"}) I18n.SaveTranslation(&i18n.Translation{Key: "qor_admin.description", Value: "description", Locale: "en-US"}) I18n.SaveTranslation(&i18n.Translation{Key: "header.title", Value: "Header Title", Locale: "en-US"}) exchange_actions.RegisterExchangeJobs(I18n, Worker) }
func getWorker() *worker.Worker { Worker := worker.New() type sendNewsletterArgument struct { Subject string Content string `sql:"size:65532"` SendPassword string } Worker.RegisterJob(worker.Job{ Name: "send_newsletter", Handler: func(argument interface{}, qorJob worker.QorJobInterface) error { qorJob.AddLog("Started sending newsletters...") qorJob.AddLog(fmt.Sprintf("Argument: %+v", argument.(*sendNewsletterArgument))) for i := 1; i <= 100; i++ { time.Sleep(100 * time.Millisecond) qorJob.AddLog(fmt.Sprintf("Sending newsletter %v...", i)) qorJob.SetProgress(uint(i)) } qorJob.AddLog("Finished send newsletters") return nil }, Resource: Admin.NewResource(&sendNewsletterArgument{}), }) type importProductArgument struct { File media_library.FileSystem } Worker.RegisterJob(worker.Job{ Name: "import_products", Handler: func(arg interface{}, qorJob worker.QorJobInterface) error { argument := arg.(*importProductArgument) context := &qor.Context{DB: db.DB} var errorCount uint if err := ProductExchange.Import( csv.New(path.Join("public", argument.File.URL())), context, func(progress exchange.Progress) error { var cells = []worker.TableCell{ {Value: fmt.Sprint(progress.Current)}, } var hasError bool for _, cell := range progress.Cells { var tableCell = worker.TableCell{ Value: fmt.Sprint(cell.Value), } if cell.Error != nil { hasError = true errorCount++ tableCell.Error = cell.Error.Error() } cells = append(cells, tableCell) } if hasError { if errorCount == 1 { var headerCells = []worker.TableCell{ {Value: "Line No."}, } for _, cell := range progress.Cells { headerCells = append(headerCells, worker.TableCell{ Value: cell.Header, }) } qorJob.AddTableRow(headerCells...) } qorJob.AddTableRow(cells...) } qorJob.SetProgress(uint(float32(progress.Current) / float32(progress.Total) * 100)) qorJob.AddLog(fmt.Sprintf("%d/%d Importing product %v", progress.Current, progress.Total, progress.Value.(*models.Product).Code)) return nil }, ); err != nil { qorJob.AddLog(err.Error()) } return nil }, Resource: Admin.NewResource(&importProductArgument{}), }) Worker.RegisterJob(worker.Job{ Name: "export_products", Handler: func(arg interface{}, qorJob worker.QorJobInterface) error { qorJob.AddLog("Exporting products...") context := &qor.Context{DB: db.DB} fileName := fmt.Sprintf("/downloads/products.%v.csv", time.Now().UnixNano()) if err := ProductExchange.Export( csv.New(path.Join("public", fileName)), context, func(progress exchange.Progress) error { qorJob.AddLog(fmt.Sprintf("%v/%v Exporting product %v", progress.Current, progress.Total, progress.Value.(*models.Product).Code)) return nil }, ); err != nil { qorJob.AddLog(err.Error()) } qorJob.SetProgressText(fmt.Sprintf("<a href='%v'>Download exported products</a>", fileName)) return nil }, }) return Worker }