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.

BackgroundJob.vue 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <!--
  2. - @copyright 2022 Carl Schwan <carl@carlschwan.eu>
  3. -
  4. - @author Carl Schwan <carl@carlschwan.eu>
  5. -
  6. - @license GNU AGPL version 3 or any later version
  7. -
  8. - This program is free software: you can redistribute it and/or modify
  9. - it under the terms of the GNU Affero General Public License as
  10. - published by the Free Software Foundation, either version 3 of the
  11. - License, or (at your option) any later version.
  12. -
  13. - This program is distributed in the hope that it will be useful,
  14. - but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. - GNU Affero General Public License for more details.
  17. -
  18. - You should have received a copy of the GNU Affero General Public License
  19. - along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. -
  21. -->
  22. <template>
  23. <SettingsSection :title="t('settings', 'Background jobs')"
  24. :description="t('settings', `For the server to work properly, it's important to configure background jobs correctly. Cron is the recommended setting. Please see the documentation for more information.`)"
  25. :doc-url="backgroundJobsDocUrl">
  26. <template v-if="lastCron !== 0">
  27. <span v-if="oldExecution" class="error">
  28. {{ t('settings', 'Last job execution ran {time}. Something seems wrong.', {time: relativeTime}) }}
  29. </span>
  30. <span v-else-if="longExecutionNotCron" class="warning">
  31. {{ t('settings', "Some jobs haven’t been executed since {maxAgeRelativeTime}. Please consider increasing the execution frequency.", {maxAgeRelativeTime}) }}
  32. </span>
  33. <span v-else-if="longExecutionCron" class="warning">
  34. {{ t('settings', "Some jobs haven’t been executed since {maxAgeRelativeTime}. Please consider switching to system cron.", {maxAgeRelativeTime}) }}
  35. </span>
  36. <span v-else>
  37. {{ t('settings', 'Last job ran {relativeTime}.', {relativeTime}) }}
  38. </span>
  39. </template>
  40. <span v-else class="error">
  41. {{ t('settings', 'Background job didn’t run yet!') }}
  42. </span>
  43. <CheckboxRadioSwitch type="radio"
  44. :checked.sync="backgroundJobsMode"
  45. name="backgroundJobsMode"
  46. value="ajax"
  47. class="ajaxSwitch"
  48. @update:checked="onBackgroundJobModeChanged">
  49. {{ t('settings', 'AJAX') }}
  50. </CheckboxRadioSwitch>
  51. <em>{{ t('settings', 'Execute one task with each page loaded. Use case: Single user instance.') }}</em>
  52. <CheckboxRadioSwitch type="radio"
  53. :checked.sync="backgroundJobsMode"
  54. name="backgroundJobsMode"
  55. value="webcron"
  56. @update:checked="onBackgroundJobModeChanged">
  57. {{ t('settings', 'Webcron') }}
  58. </CheckboxRadioSwitch>
  59. <em>{{ t('settings', 'cron.php is registered at a webcron service to call cron.php every 5 minutes over HTTP. Use case: Very small instance (1–5 users depending on the usage).') }}</em>
  60. <CheckboxRadioSwitch v-if="cliBasedCronPossible"
  61. type="radio"
  62. :checked.sync="backgroundJobsMode"
  63. value="cron"
  64. name="backgroundJobsMode"
  65. @update:checked="onBackgroundJobModeChanged">
  66. {{ t('settings', 'Cron (Recommended)') }}
  67. </CheckboxRadioSwitch>
  68. <em v-if="cliBasedCronPossible">{{ cronLabel }}</em>
  69. <em v-else>
  70. {{ t('settings', 'To run this you need the PHP POSIX extension. See {linkstart}PHP documentation{linkend} for more details.', {
  71. linkstart: '<a href="https://www.php.net/manual/en/book.posix.php">',
  72. linkend: '</a>',
  73. }) }}
  74. </em>
  75. </SettingsSection>
  76. </template>
  77. <script>
  78. import { loadState } from '@nextcloud/initial-state'
  79. import { showError } from '@nextcloud/dialogs'
  80. import CheckboxRadioSwitch from '@nextcloud/vue/dist/Components/CheckboxRadioSwitch'
  81. import SettingsSection from '@nextcloud/vue/dist/Components/SettingsSection'
  82. import moment from '@nextcloud/moment'
  83. import axios from '@nextcloud/axios'
  84. import { generateOcsUrl } from '@nextcloud/router'
  85. import confirmPassword from '@nextcloud/password-confirmation'
  86. const lastCron = loadState('settings', 'lastCron')
  87. const cronMaxAge = loadState('settings', 'cronMaxAge', '')
  88. const backgroundJobsMode = loadState('settings', 'backgroundJobsMode', 'cron')
  89. const cliBasedCronPossible = loadState('settings', 'cliBasedCronPossible', true)
  90. const cliBasedCronUser = loadState('settings', 'cliBasedCronUser', 'www-data')
  91. const backgroundJobsDocUrl = loadState('settings', 'backgroundJobsDocUrl')
  92. export default {
  93. name: 'BackgroundJob',
  94. components: {
  95. CheckboxRadioSwitch,
  96. SettingsSection,
  97. },
  98. data() {
  99. return {
  100. lastCron,
  101. cronMaxAge,
  102. backgroundJobsMode,
  103. cliBasedCronPossible,
  104. cliBasedCronUser,
  105. backgroundJobsDocUrl,
  106. relativeTime: moment(lastCron * 1000).fromNow(),
  107. maxAgeRelativeTime: moment(cronMaxAge * 1000).fromNow(),
  108. }
  109. },
  110. computed: {
  111. cronLabel() {
  112. let desc = t('settings', 'Use system cron service to call the cron.php file every 5 minutes. Recommended for all instances.')
  113. if (this.cliBasedCronPossible) {
  114. desc += ' ' + t('settings', 'The cron.php needs to be executed by the system user "{user}".', { user: this.cliBasedCronUser })
  115. }
  116. return desc
  117. },
  118. oldExecution() {
  119. return Date.now() / 1000 - this.lastCron > 600
  120. },
  121. longExecutionNotCron() {
  122. return Date.now() / 1000 - this.cronMaxAge > 12 * 3600 && this.backgroundJobsMode !== 'cron'
  123. },
  124. longExecutionCron() {
  125. return Date.now() / 1000 - this.cronMaxAge > 12 * 3600 && this.backgroundJobsMode === 'cron'
  126. },
  127. },
  128. methods: {
  129. async onBackgroundJobModeChanged(backgroundJobsMode) {
  130. const url = generateOcsUrl('/apps/provisioning_api/api/v1/config/apps/{appId}/{key}', {
  131. appId: 'core',
  132. key: 'backgroundjobs_mode',
  133. })
  134. await confirmPassword()
  135. try {
  136. const { data } = await axios.post(url, {
  137. value: backgroundJobsMode,
  138. })
  139. this.handleResponse({
  140. status: data.ocs?.meta?.status,
  141. })
  142. } catch (e) {
  143. this.handleResponse({
  144. errorMessage: t('settings', 'Unable to update background job mode'),
  145. error: e,
  146. })
  147. }
  148. },
  149. async handleResponse({ status, errorMessage, error }) {
  150. if (status === 'ok') {
  151. await this.deleteError()
  152. } else {
  153. showError(errorMessage)
  154. console.error(errorMessage, error)
  155. }
  156. },
  157. async deleteError() {
  158. // clear cron errors on background job mode change
  159. const url = generateOcsUrl('/apps/provisioning_api/api/v1/config/apps/{appId}/{key}', {
  160. appId: 'core',
  161. key: 'cronErrors',
  162. })
  163. await confirmPassword()
  164. try {
  165. await axios.delete(url)
  166. } catch (error) {
  167. console.error(error)
  168. }
  169. },
  170. },
  171. }
  172. </script>
  173. <style lang="scss" scoped>
  174. .error {
  175. margin-top: 8px;
  176. padding: 5px;
  177. border-radius: var(--border-radius);
  178. color: var(--color-primary-text);
  179. background-color: var(--color-error);
  180. width: initial;
  181. }
  182. .warning {
  183. margin-top: 8px;
  184. padding: 5px;
  185. border-radius: var(--border-radius);
  186. color: var(--color-primary-text);
  187. background-color: var(--color-warning);
  188. width: initial;
  189. }
  190. .ajaxSwitch {
  191. margin-top: 1rem;
  192. }
  193. </style>