useAppMaintenanceCheck.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { useRouteQuery } from '@vueuse/router'
  3. import { onMounted, reactive, watch } from 'vue'
  4. import {
  5. useNotifications,
  6. NotificationTypes,
  7. } from '#shared/components/CommonNotifications/index.ts'
  8. import { useApplicationBuildChecksumQuery } from '#shared/graphql/queries/applicationBuildChecksum.api.ts'
  9. import { useAppMaintenanceSubscription } from '#shared/graphql/subscriptions/appMaintenance.api.ts'
  10. import type {
  11. ApplicationBuildChecksumQuery,
  12. ApplicationBuildChecksumQueryVariables,
  13. AppMaintenanceSubscription,
  14. AppMaintenanceSubscriptionVariables,
  15. } from '#shared/graphql/types.ts'
  16. import { EnumAppMaintenanceType } from '#shared/graphql/types.ts'
  17. import {
  18. QueryHandler,
  19. SubscriptionHandler,
  20. } from '#shared/server/apollo/handler/index.ts'
  21. import testFlags from '#shared/utils/testFlags.ts'
  22. let checksumQuery: QueryHandler<
  23. ApplicationBuildChecksumQuery,
  24. ApplicationBuildChecksumQueryVariables
  25. >
  26. let previousChecksum: string
  27. let appMaintenanceSubscription: SubscriptionHandler<
  28. AppMaintenanceSubscription,
  29. AppMaintenanceSubscriptionVariables
  30. >
  31. interface UseAppMaintenanceCheckOptions {
  32. onNeedRefresh?: () => void
  33. }
  34. const useAppMaintenanceCheck = (
  35. maintenanceOptions: UseAppMaintenanceCheckOptions = {},
  36. ) => {
  37. const notify = (message: string, callback?: () => void) => {
  38. useNotifications().notify({
  39. id: 'app-maintenance',
  40. message,
  41. type: NotificationTypes.Warn,
  42. persistent: true,
  43. callback,
  44. })
  45. }
  46. onMounted(() => {
  47. if (checksumQuery) return
  48. // Default poll interval: every minute.
  49. const defaultPollInterval = 60 * 1000
  50. const applicationRebuildCheckInterval = useRouteQuery(
  51. 'ApplicationRebuildCheckInterval',
  52. defaultPollInterval.toString(),
  53. )
  54. const options = reactive({
  55. pollInterval: parseInt(applicationRebuildCheckInterval.value, 10),
  56. })
  57. watch(applicationRebuildCheckInterval, () => {
  58. options.pollInterval = parseInt(applicationRebuildCheckInterval.value, 10)
  59. })
  60. checksumQuery = new QueryHandler(useApplicationBuildChecksumQuery(options))
  61. const notificationMessage = __(
  62. 'A newer version of the app is available. Please reload at your earliest.',
  63. )
  64. checksumQuery.watchOnResult((queryResult): void => {
  65. if (!queryResult?.applicationBuildChecksum.length) return
  66. if (!previousChecksum) {
  67. previousChecksum = queryResult?.applicationBuildChecksum
  68. testFlags.set('useApplicationBuildChecksumQuery.firstResult')
  69. }
  70. if (queryResult?.applicationBuildChecksum !== previousChecksum) {
  71. notify(notificationMessage, maintenanceOptions.onNeedRefresh)
  72. }
  73. })
  74. appMaintenanceSubscription = new SubscriptionHandler(
  75. useAppMaintenanceSubscription(),
  76. )
  77. appMaintenanceSubscription.onResult((result) => {
  78. const type = result.data?.appMaintenance?.type
  79. let message = notificationMessage
  80. if (!type) {
  81. testFlags.set('useAppMaintenanceSubscription.subscribed')
  82. return
  83. }
  84. switch (type) {
  85. case EnumAppMaintenanceType.ConfigChanged:
  86. message = __(
  87. 'The configuration of Zammad has changed. Please reload at your earliest.',
  88. )
  89. break
  90. case EnumAppMaintenanceType.RestartAuto:
  91. case EnumAppMaintenanceType.RestartManual:
  92. // TODO: this case cannot be handled right now. Legacy interface performs a connectivity check.
  93. break
  94. default:
  95. break
  96. }
  97. notify(message, () => window.location.reload())
  98. })
  99. })
  100. }
  101. export default useAppMaintenanceCheck