package console import ( "context" "fmt" "git.golem15.com/golem15/sm-sitemap-plugin/classes" "git.golem15.com/golem15/summercms/modules/backpack" "git.golem15.com/golem15/summercms/modules/bonfire" ) // Rebuild is sitemap:rebuild [--queue]: it rebuilds the sitemap files now // (classes.Rebuild), or with --queue dispatches the rebuild job for a // worker. The scheduler runs it every night at 03:00. func Rebuild(app *backpack.App) bonfire.Command { return bonfire.Command{ Name: "sitemap:rebuild", Description: "Rebuild the sitemap files from the content types and the additional addresses", Long: `Reads the published records of every content type and the additional addresses, writes them into the sitemap's address table and numbers the files: existing addresses keep their files, new ones are appended, and only a changed file size renumbers. It runs in this process and ends with "Rebuilt the sitemaps: N addresses in N files (N added, N changed, N removed)." --queue dispatches the rebuild job instead, for a worker to run. The scheduler runs this command every night at 03:00; record changes and a save of the sitemap settings queue a rebuild too.`, Flags: []bonfire.Flag{ {Name: "queue", Description: "Dispatch the rebuild job for a worker instead of rebuilding here", Bare: true}, }, Examples: []bonfire.Example{ {Command: "sitemap:rebuild", Description: "Rebuild the sitemap files now"}, {Command: "sitemap:rebuild --queue", Description: "Queue a rebuild for the worker"}, }, Run: func(ctx context.Context, in bonfire.Input, out bonfire.Output) error { _, done, err := lookupDB(ctx, app) if err != nil { return err } defer done() if flagOn(in, "queue") { id, err := classes.DispatchRebuild(ctx, app) if err != nil { return err } out.Success(fmt.Sprintf("Queued the sitemap rebuild (job %d).", id)) return nil } res, err := classes.Rebuild(ctx, app) if err != nil { return err } out.Success(fmt.Sprintf("Rebuilt the sitemaps: %d addresses in %d files (%d added, %d changed, %d removed).", res.Listed, res.Files, len(res.Added), len(res.Changed), len(res.Removed))) return nil }, } }