|
| 1 | +<!-- |
| 2 | + - @copyright 2022 Carl Schwan <[email protected]> |
| 3 | + - |
| 4 | + - @author Carl Schwan <[email protected]> |
| 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 | + |
| 23 | +<template> |
| 24 | + <SettingsSection :title="t('settings', 'Background jobs')" |
| 25 | + :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.`)" |
| 26 | + :doc-url="backgroundJobsDocUrl"> |
| 27 | + |
| 28 | + <template v-if="lastCron !== 0"> |
| 29 | + <span v-if="oldExecution" class="error"> |
| 30 | + {{ t('settings', 'Last job execution ran {time}. Something seems wrong.', {time: relativeTime}) }} |
| 31 | + </span> |
| 32 | + |
| 33 | + <span v-else-if="longExecutionNotCron" class="warning"> |
| 34 | + {{ t('settings', "Some jobs haven’t been executed since {maxAgeRelativeTime}. Please consider increasing the execution frequency.", {maxAgeRelativeTime}) }} |
| 35 | + </span> |
| 36 | + |
| 37 | + <span class="warning" v-else-if="longExecutionCron"> |
| 38 | + {{ t('settings', "Some jobs haven’t been executed since {maxAgeRelativeTime}. Please consider switching to system cron.", {maxAgeRelativeTime}) }} |
| 39 | + </span> |
| 40 | + |
| 41 | + <span v-else> |
| 42 | + {{ t('settings', 'Last job ran {relativeTime}.', {relativeTime}) }} |
| 43 | + </span> |
| 44 | + </template> |
| 45 | + |
| 46 | + <span class="error" v-else> |
| 47 | + {{ t('settings', 'Background job didn’t run yet!') }} |
| 48 | + </span> |
| 49 | + |
| 50 | + <CheckboxRadioSwitch type="radio" |
| 51 | + :checked.sync="backgroundJobsMode" |
| 52 | + name="backgroundJobsMode" |
| 53 | + value="ajax" |
| 54 | + class="ajaxSwitch" |
| 55 | + @update:checked="onBackgroundJobModeChanged"> |
| 56 | + {{ t('settings', 'AJAX') }} |
| 57 | + </CheckboxRadioSwitch> |
| 58 | + <em>{{ t('settings', 'Execute one task with each page loaded. Use case: Single user instance.') }}</em> |
| 59 | + |
| 60 | + <CheckboxRadioSwitch type="radio" |
| 61 | + :checked.sync="backgroundJobsMode" |
| 62 | + name="backgroundJobsMode" |
| 63 | + value="webcron" |
| 64 | + @update:checked="onBackgroundJobModeChanged"> |
| 65 | + {{ t('settings', 'Webcron') }} |
| 66 | + </CheckboxRadioSwitch> |
| 67 | + <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> |
| 68 | + |
| 69 | + <CheckboxRadioSwitch type="radio" |
| 70 | + :checked.sync="backgroundJobsMode" |
| 71 | + value="cron" |
| 72 | + name="backgroundJobsMode" |
| 73 | + v-if="cliBasedCronPossible" |
| 74 | + @update:checked="onBackgroundJobModeChanged"> |
| 75 | + {{ t('settings', 'Cron (Recommended)') }} |
| 76 | + </CheckboxRadioSwitch> |
| 77 | + <em v-if="cliBasedCronPossible">{{ cronLabel }}</em> |
| 78 | + <em v-else> |
| 79 | + {{ t('settings', 'To run this you need the PHP POSIX extension. See {linkstart}PHP documentation{linkend} for more details.', { |
| 80 | + linkstart: '<a href="https://www.php.net/manual/en/book.posix.php">', |
| 81 | + linkend: '</a>', |
| 82 | + }) }} |
| 83 | + </em> |
| 84 | + </SettingsSection> |
| 85 | +</template> |
| 86 | + |
| 87 | +<script> |
| 88 | +import { loadState } from '@nextcloud/initial-state' |
| 89 | +import { showError } from '@nextcloud/dialogs' |
| 90 | +import CheckboxRadioSwitch from '@nextcloud/vue/dist/Components/CheckboxRadioSwitch' |
| 91 | +import SettingsSection from '@nextcloud/vue/dist/Components/SettingsSection' |
| 92 | +import moment from '@nextcloud/moment' |
| 93 | +import axios from '@nextcloud/axios' |
| 94 | +import { generateOcsUrl } from '@nextcloud/router' |
| 95 | +import confirmPassword from '@nextcloud/password-confirmation' |
| 96 | +
|
| 97 | +const lastCron = loadState('settings', 'lastCron') |
| 98 | +const cronMaxAge = loadState('settings', 'cronMaxAge', '') |
| 99 | +const backgroundJobsMode = loadState('settings', 'backgroundJobsMode', 'cron') |
| 100 | +const cliBasedCronPossible = loadState('settings', 'cliBasedCronPossible', true) |
| 101 | +const cliBasedCronUser = loadState('settings', 'cliBasedCronUser', 'www-data') |
| 102 | +const backgroundJobsDocUrl = loadState('settings', 'backgroundJobsDocUrl') |
| 103 | +
|
| 104 | +export default { |
| 105 | + name: 'BackgroundJob', |
| 106 | +
|
| 107 | + components: { |
| 108 | + CheckboxRadioSwitch, |
| 109 | + SettingsSection, |
| 110 | + }, |
| 111 | +
|
| 112 | + data() { |
| 113 | + return { |
| 114 | + lastCron, |
| 115 | + cronMaxAge, |
| 116 | + backgroundJobsMode, |
| 117 | + cliBasedCronPossible, |
| 118 | + cliBasedCronUser, |
| 119 | + backgroundJobsDocUrl, |
| 120 | + relativeTime: moment(lastCron * 1000).fromNow(), |
| 121 | + maxAgeRelativeTime: moment(cronMaxAge * 1000).fromNow(), |
| 122 | + } |
| 123 | + }, |
| 124 | + computed: { |
| 125 | + cronLabel() { |
| 126 | + let desc = t('settings', 'Use system cron service to call the cron.php file every 5 minutes. Recommended for all instances.') |
| 127 | + if (this.cliBasedCronPossible) { |
| 128 | + desc += ' ' + t('settings', 'The cron.php needs to be executed by the system user "{user}".', { user: this.cliBasedCronUser }) |
| 129 | + } |
| 130 | + return desc |
| 131 | + }, |
| 132 | + oldExecution() { |
| 133 | + return Date.now() / 1000 - this.lastCron > 600 |
| 134 | + }, |
| 135 | + longExecutionNotCron() { |
| 136 | + return Date.now() / 1000 - this.cronMaxAge > 12 * 3600 && this.backgroundJobsMode !== 'cron' |
| 137 | + }, |
| 138 | + longExecutionCron() { |
| 139 | + return Date.now() / 1000 - this.cronMaxAge > 12 * 3600 && this.backgroundJobsMode === 'cron' |
| 140 | + } |
| 141 | + }, |
| 142 | + methods: { |
| 143 | + async onBackgroundJobModeChanged(backgroundJobsMode) { |
| 144 | + const url = generateOcsUrl('/apps/provisioning_api/api/v1/config/apps/{appId}/{key}', { |
| 145 | + appId: 'core', |
| 146 | + key: 'backgroundjobs_mode', |
| 147 | + }) |
| 148 | +
|
| 149 | + await confirmPassword() |
| 150 | +
|
| 151 | + try { |
| 152 | + const { data } = await axios.post(url, { |
| 153 | + value: backgroundJobsMode |
| 154 | + }) |
| 155 | + this.handleResponse({ |
| 156 | + status: data.ocs?.meta?.status |
| 157 | + }) |
| 158 | + } catch (e) { |
| 159 | + this.handleResponse({ |
| 160 | + errorMessage: t('settings', 'Unable to update background job mode'), |
| 161 | + error: e, |
| 162 | + }) |
| 163 | + } |
| 164 | + }, |
| 165 | + async handleResponse({ status, errorMessage, error }) { |
| 166 | + if (status === 'ok') { |
| 167 | + await this.deleteError() |
| 168 | + } else { |
| 169 | + showError(errorMessage) |
| 170 | + console.error(errorMessage, error) |
| 171 | + } |
| 172 | + }, |
| 173 | + async deleteError() { |
| 174 | + // clear cron errors on background job mode change |
| 175 | + const url = generateOcsUrl('/apps/provisioning_api/api/v1/config/apps/{appId}/{key}', { |
| 176 | + appId: 'core', |
| 177 | + key: 'cronErrors', |
| 178 | + }) |
| 179 | +
|
| 180 | + await confirmPassword() |
| 181 | +
|
| 182 | + try { |
| 183 | + await axios.delete(url) |
| 184 | + } catch (error) { |
| 185 | + console.error(error) |
| 186 | + } |
| 187 | + } |
| 188 | + }, |
| 189 | +} |
| 190 | +</script> |
| 191 | +
|
| 192 | +<style lang="scss" scoped> |
| 193 | +.error { |
| 194 | + margin-top: 8px; |
| 195 | + padding: 5px; |
| 196 | + border-radius: var(--border-radius); |
| 197 | + color: var(--color-primary-text); |
| 198 | + background-color: var(--color-error); |
| 199 | + width: initial; |
| 200 | +} |
| 201 | +.warning { |
| 202 | + margin-top: 8px; |
| 203 | + padding: 5px; |
| 204 | + border-radius: var(--border-radius); |
| 205 | + color: var(--color-primary-text); |
| 206 | + background-color: var(--color-warning); |
| 207 | + width: initial; |
| 208 | +} |
| 209 | +.ajaxSwitch { |
| 210 | + margin-top: 1rem; |
| 211 | +} |
| 212 | +</style> |
0 commit comments