A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

73 lines
1.6 KiB

  1. package writefreely
  2. import (
  3. "github.com/writeas/web-core/log"
  4. "time"
  5. )
  6. type PostJob struct {
  7. ID int64
  8. PostID string
  9. Action string
  10. Delay int64
  11. }
  12. func addJob(app *App, p *PublicPost, action string, delay int64) error {
  13. j := &PostJob{
  14. PostID: p.ID,
  15. Action: action,
  16. Delay: delay,
  17. }
  18. return app.db.InsertJob(j)
  19. }
  20. func startPublishJobsQueue(app *App) {
  21. t := time.NewTicker(62 * time.Second)
  22. for {
  23. log.Info("[jobs] Done.")
  24. <-t.C
  25. log.Info("[jobs] Fetching email publish jobs...")
  26. jobs, err := app.db.GetJobsToRun("email")
  27. if err != nil {
  28. log.Error("[jobs] %s - Skipping.", err)
  29. continue
  30. }
  31. log.Info("[jobs] Running %d email publish jobs...", len(jobs))
  32. err = runJobs(app, jobs, true)
  33. if err != nil {
  34. log.Error("[jobs] Failed: %s", err)
  35. }
  36. }
  37. }
  38. func runJobs(app *App, jobs []*PostJob, reqColl bool) error {
  39. for _, j := range jobs {
  40. p, err := app.db.GetPost(j.PostID, 0)
  41. if err != nil {
  42. log.Info("[job #%d] Unable to get post: %s", j.ID, err)
  43. continue
  44. }
  45. if !p.CollectionID.Valid && reqColl {
  46. log.Info("[job #%d] Post %s not part of a collection", j.ID, p.ID)
  47. app.db.DeleteJob(j.ID)
  48. continue
  49. }
  50. coll, err := app.db.GetCollectionByID(p.CollectionID.Int64)
  51. if err != nil {
  52. log.Info("[job #%d] Unable to get collection: %s", j.ID, err)
  53. continue
  54. }
  55. coll.hostName = app.cfg.App.Host
  56. coll.ForPublic()
  57. p.Collection = &CollectionObj{Collection: *coll}
  58. err = emailPost(app, p, p.Collection.ID)
  59. if err != nil {
  60. log.Error("[job #%d] Failed to email post %s", j.ID, p.ID)
  61. continue
  62. }
  63. log.Info("[job #%d] Success for post %s.", j.ID, p.ID)
  64. app.db.DeleteJob(j.ID)
  65. }
  66. return nil
  67. }