scheduler.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package main
  2. import (
  3. "net/http"
  4. module "imuslab.com/arozos/mod/modules"
  5. prout "imuslab.com/arozos/mod/prouter"
  6. "imuslab.com/arozos/mod/time/nightly"
  7. "imuslab.com/arozos/mod/time/scheduler"
  8. "imuslab.com/arozos/mod/utils"
  9. )
  10. /*
  11. Nightly.go
  12. author: tobychui
  13. This is a handle for putting everything that is required to run everynight.
  14. Default: Run once every day 3am in the morning.
  15. */
  16. var (
  17. nightlyManager *nightly.TaskManager
  18. systemScheduler *scheduler.Scheduler
  19. )
  20. func NightlyTasksInit() {
  21. /*
  22. Nighty Task Manager
  23. The tasks that should be done once per night. Internal function only.
  24. */
  25. nightlyManager = nightly.NewNightlyTaskManager(*nightlyTaskRunTime)
  26. }
  27. func SchedulerInit() {
  28. /*
  29. System Scheudler
  30. The internal scheudler for arozos
  31. */
  32. //Create an user router and its module
  33. router := prout.NewModuleRouter(prout.RouterOption{
  34. ModuleName: "Tasks Scheduler",
  35. AdminOnly: false,
  36. UserHandler: userHandler,
  37. DeniedHandler: func(w http.ResponseWriter, r *http.Request) {
  38. utils.SendErrorResponse(w, "Permission Denied")
  39. },
  40. })
  41. //Register the module
  42. moduleHandler.RegisterModule(module.ModuleInfo{
  43. Name: "Tasks Scheduler",
  44. Group: "System Tools",
  45. IconPath: "SystemAO/arsm/img/scheduler.png",
  46. Version: "1.2",
  47. StartDir: "SystemAO/arsm/scheduler.html",
  48. SupportFW: true,
  49. InitFWSize: []int{1080, 580},
  50. LaunchFWDir: "SystemAO/arsm/scheduler.html",
  51. SupportEmb: false,
  52. })
  53. //Startup the ArOZ Emulated Crontab Service
  54. newScheduler, err := scheduler.NewScheduler(&scheduler.ScheudlerOption{
  55. UserHandler: userHandler,
  56. Gateway: AGIGateway,
  57. Logger: systemWideLogger,
  58. CronFile: "system/cron.json",
  59. })
  60. if err != nil {
  61. systemWideLogger.PrintAndLog("Cron", "ArOZ Emulated Cron Startup Failed. Stopping all scheduled tasks.", err)
  62. }
  63. systemScheduler = newScheduler
  64. //Register Endpoints
  65. http.HandleFunc("/system/arsm/aecron/list", func(w http.ResponseWriter, r *http.Request) {
  66. if authAgent.CheckAuth(r) {
  67. //User logged in
  68. newScheduler.HandleListJobs(w, r)
  69. } else {
  70. //User not logged in
  71. errorHandlePermissionDenied(w, r)
  72. }
  73. })
  74. router.HandleFunc("/system/arsm/aecron/add", systemScheduler.HandleAddJob)
  75. router.HandleFunc("/system/arsm/aecron/remove", systemScheduler.HandleJobRemoval)
  76. //router.HandleFunc("/system/arsm/aecron/listlog", systemScheduler.HandleShowLog)
  77. //Register settings
  78. registerSetting(settingModule{
  79. Name: "Tasks Scheduler",
  80. Desc: "System Tasks and Excution Scheduler",
  81. IconPath: "SystemAO/arsm/img/small_icon.png",
  82. Group: "Cluster",
  83. StartDir: "SystemAO/arsm/aecron.html",
  84. RequireAdmin: false,
  85. })
  86. }