this repo has no description smallweb.run
smallweb
4
fork

Configure Feed

Select the types of activity you want to include in your feed.

move cron jobs to global config

pomdtr e78889b5 a42bdc25

+59 -53
+2 -3
app/app.go
··· 20 20 ) 21 21 22 22 type AppConfig struct { 23 - Entrypoint string `json:"entrypoint,omitempty"` 24 - Root string `json:"root,omitempty"` 25 - Crons []CronJob `json:"crons,omitempty"` 23 + Entrypoint string `json:"entrypoint,omitempty"` 24 + Root string `json:"root,omitempty"` 26 25 } 27 26 28 27 type CronJob struct {
+19 -50
cmd/crons.go
··· 7 7 "io" 8 8 "log" 9 9 "os" 10 - "path/filepath" 11 10 "strings" 12 11 "time" 13 12 ··· 21 20 ) 22 21 23 22 type CronItem struct { 24 - App string `json:"app"` 25 - app.CronJob 23 + App string `json:"app"` 24 + Args []string `json:"args"` 25 + Schedule string `json:"schedule"` 26 26 } 27 27 28 28 func NewCmdCrons() *cobra.Command { ··· 45 45 }, 46 46 Short: "List cron jobs", 47 47 RunE: func(cmd *cobra.Command, args []string) error { 48 - var appName string 49 - if len(args) > 0 { 50 - appName = args[0] 51 - } else if !flags.all { 52 - cwd, err := os.Getwd() 53 - if err != nil { 54 - return fmt.Errorf("failed to get current directory: %w", err) 55 - } 56 - 57 - if filepath.Dir(cwd) != k.String("dir") { 58 - return fmt.Errorf("no app specified and not in an app directory") 59 - } 60 - 61 - appName = filepath.Base(cwd) 62 - } 63 - 64 - apps, err := app.ListApps(k.String("dir")) 65 - if err != nil { 66 - return fmt.Errorf("failed to list apps: %w", err) 67 - } 68 - 69 - crons := make([]CronItem, 0) 70 - for _, name := range apps { 71 - if appName != "" && name != appName { 48 + var crons []CronItem 49 + for _, appname := range k.MapKeys("apps") { 50 + if len(args) > 0 && appname != args[0] { 72 51 continue 73 52 } 74 53 75 - app, err := app.LoadApp(name, k.String("dir"), k.String("domain"), k.Bool(fmt.Sprintf("apps.%s.admin", name))) 76 - if err != nil { 77 - return fmt.Errorf("failed to load app: %w", err) 78 - } 79 - 80 - for _, job := range app.Config.Crons { 54 + for _, job := range k.Slices(fmt.Sprintf("apps.%s.crons", appname)) { 81 55 crons = append(crons, CronItem{ 82 - App: name, 83 - CronJob: job, 56 + App: appname, 57 + Args: job.Strings("args"), 58 + Schedule: job.String("schedule"), 84 59 }) 85 60 } 86 61 } ··· 143 118 c := cron.New(cron.WithParser(parser)) 144 119 _, _ = c.AddFunc("* * * * *", func() { 145 120 rounded := time.Now().Truncate(time.Minute) 146 - apps, err := app.ListApps(k.String("dir")) 147 - if err != nil { 148 - fmt.Println(err) 149 - } 150 - 151 - for _, name := range apps { 152 - a, err := app.LoadApp(name, k.String("dir"), k.String("domain"), k.Bool(fmt.Sprintf("apps.%s.admin", name))) 153 - if err != nil { 154 - fmt.Println(err) 155 - continue 156 - } 157 - 158 - for _, job := range a.Config.Crons { 159 - sched, err := parser.Parse(job.Schedule) 121 + for _, appname := range k.MapKeys("apps") { 122 + for _, job := range k.Slices(fmt.Sprintf("apps.%s.crons", appname)) { 123 + sched, err := parser.Parse(job.String("schedule")) 160 124 if err != nil { 161 125 fmt.Println(err) 162 126 continue ··· 166 130 continue 167 131 } 168 132 133 + a, err := app.LoadApp(appname, k.String("rootDir"), k.String("domain"), k.Bool(fmt.Sprintf("apps.%s.admin", appname))) 134 + if err != nil { 135 + fmt.Println(err) 136 + continue 137 + } 169 138 wk := worker.NewWorker(a) 170 139 171 - command, err := wk.Command(context.Background(), job.Args...) 140 + command, err := wk.Command(context.Background(), job.Strings("args")...) 172 141 if err != nil { 173 142 fmt.Println(err) 174 143 continue
+38
schemas/config.schema.json
··· 33 33 "type": "string" 34 34 } 35 35 }, 36 + "authorizedGroups": { 37 + "description": "Authorized groups", 38 + "type": "array", 39 + "items": { 40 + "type": "string" 41 + } 42 + }, 36 43 "authorizedKeys": { 37 44 "description": "Authorized SSH keys", 38 45 "type": "array", ··· 55 62 "description": "Give the app admin privileges", 56 63 "type": "boolean" 57 64 }, 65 + "crons": { 66 + "description": "Cron jobs", 67 + "type": "array", 68 + "items": { 69 + "type": "object", 70 + "required": [ 71 + "schedule", 72 + "args" 73 + ], 74 + "properties": { 75 + "schedule": { 76 + "description": "Cron schedule", 77 + "type": "string" 78 + }, 79 + "args": { 80 + "description": "Cron arguments", 81 + "type": "array", 82 + "items": { 83 + "type": "string" 84 + } 85 + } 86 + } 87 + } 88 + }, 58 89 "additionalDomains": { 59 90 "description": "Additional app domains", 60 91 "type": "array", ··· 64 95 }, 65 96 "authorizedEmails": { 66 97 "description": "Authorized email addresses", 98 + "type": "array", 99 + "items": { 100 + "type": "string" 101 + } 102 + }, 103 + "authorizedGroups": { 104 + "description": "Authorized groups", 67 105 "type": "array", 68 106 "items": { 69 107 "type": "string"