README 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*!
  2. \ingroup JobQueue
  3. \page jobqueue_design Job queue design
  4. Notes on the Job queuing system architecture.
  5. \section intro Introduction
  6. The data model consist of the following main components:
  7. * The Job object represents a particular deferred task that happens in the
  8. background. All jobs subclass the Job object and put the main logic in the
  9. function called run().
  10. * The JobQueue object represents a particular queue of jobs of a certain type.
  11. For example there may be a queue for email jobs and a queue for CDN purge
  12. jobs.
  13. \section jobqueue Job queues
  14. Each job type has its own queue and is associated to a storage medium. One
  15. queue might save its jobs in redis while another one uses would use a database.
  16. Storage medium are defined in a queue class. Before using it, you must
  17. define in $wgJobTypeConf a mapping of the job type to a queue class.
  18. The factory class JobQueueGroup provides helper functions:
  19. - getting the queue for a given job
  20. - route new job insertions to the proper queue
  21. The following queue classes are available:
  22. * JobQueueDB (stores jobs in the `job` table in a database)
  23. * JobQueueRedis (stores jobs in a redis server)
  24. All queue classes support some basic operations (though some may be no-ops):
  25. * enqueueing a batch of jobs
  26. * dequeueing a single job
  27. * acknowledging a job is completed
  28. * checking if the queue is empty
  29. Some queue classes (like JobQueueDB) may dequeue jobs in random order while other
  30. queues might dequeue jobs in exact FIFO order. Callers should thus not assume jobs
  31. are executed in FIFO order.
  32. Also note that not all queue classes will have the same reliability guarantees.
  33. In-memory queues may lose data when restarted depending on snapshot and journal
  34. settings (including journal fsync() frequency). Some queue types may totally remove
  35. jobs when dequeued while leaving the ack() function as a no-op; if a job is
  36. dequeued by a job runner, which crashes before completion, the job will be
  37. lost. Some jobs, like purging CDN caches after a template change, may not
  38. require durable queues, whereas other jobs might be more important.
  39. \section aggregator Job queue aggregator
  40. The aggregators are used by nextJobDB.php, which is a script that will return a
  41. random ready queue (on any wiki in the farm) that can be used with runJobs.php.
  42. This can be used in conjunction with any scripts that handle wiki farm job queues.
  43. Note that $wgLocalDatabases defines what wikis are in the wiki farm.
  44. Since each job type has its own queue, and wiki-farms may have many wikis,
  45. there might be a large number of queues to keep track of. To avoid wasting
  46. large amounts of time polling empty queues, aggregators exists to keep track
  47. of which queues are ready.
  48. The following queue aggregator classes are available:
  49. * JobQueueAggregatorRedis (uses a redis server to track ready queues)
  50. Some aggregators cache data for a few minutes while others may be always up to date.
  51. This can be an important factor for jobs that need a low pickup time (or latency).
  52. \section jobs Jobs
  53. Callers should also try to make jobs maintain correctness when executed twice.
  54. This is useful for queues that actually implement ack(), since they may recycle
  55. dequeued but un-acknowledged jobs back into the queue to be attempted again. If
  56. a runner dequeues a job, runs it, but then crashes before calling ack(), the
  57. job may be returned to the queue and run a second time. Jobs like cache purging can
  58. happen several times without any correctness problems. However, a pathological case
  59. would be if a bug causes the problem to systematically keep repeating. For example,
  60. a job may always throw a DB error at the end of run(). This problem is trickier to
  61. solve and more obnoxious for things like email jobs, for example. For such jobs,
  62. it might be useful to use a queue that does not retry jobs.