Skip to content

Commit

Permalink
Merge pull request #124 from zsuroy/feature-timer
Browse files Browse the repository at this point in the history
feat: 支持定时重启服务
  • Loading branch information
xianyunleo authored Oct 30, 2024
2 parents 9180629 + d7dd8bb commit 42a1e63
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 3 deletions.
133 changes: 133 additions & 0 deletions src/renderer/components/Settings/Timer.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<template>
<a-card size="small" :title="t('Timer')" class="settings-card">
<div class="settings-card-row flex-vertical-center">
<a-tooltip>
<template #title>{{ t('TimerServerList') }}</template>
<span>{{ mt('Server', 'ws', 'List') }}</span>
</a-tooltip>

<a-select
v-model:value="store.settings.AutoTimerServerList"
:options="TimerServerOptions"
mode="multiple"
style="flex: 1"
placeholder="请选择"
@change="AutoTimerServerChange"
></a-select>
</div>

<div class="interval-card-row flex-vertical-center">
<a-tooltip>
<template #title>{{ t('RestartIntervalText') }}</template>
<span>{{ t('RestartIntervalText') }}:</span>
</a-tooltip>

<a-select v-model:value="store.settings.AutoTimerInterval" :options="intervalOptions" placeholder="选择重启间隔" style="flex: 1" @change="changeAutoTimerInterval"></a-select>
</div>

<div class="settings-card-row flex-vertical-center">
<a-switch v-model:checked="store.settings.AutoTimerRestartServer" class="settings-switch" @change="changeAutoTimerRestartServer" />
<span>{{ t('ServerAutoRestartText') }}</span>
</div>
</a-card>
</template>

<script setup>
import { setInterval } from 'timers'
import { computed, watch, inject, onMounted } from 'vue'
import { useMainStore } from '@/renderer/store'
import { storeToRefs } from 'pinia'
import { mt, t } from '@/renderer/utils/i18n'
import { createAsyncComponent } from '@/renderer/utils/utils'
import Settings from '@/main/Settings'
function formatUnitLabel(seconds) {
const timeUnits = [
{ unit: 604800, label: t('Weeks') },
{ unit: 86400, label: t('Days') },
{ unit: 3600, label: t('Hours') },
{ unit: 60, label: t('Minutes') },
{ unit: 1, label: t('Seconds') }
]
for (let { unit, label } of timeUnits) {
if (seconds >= unit) {
return `${seconds / unit} ${label}`
}
}
}
const intervalOptions = computed(() => {
const intervals = [600, 1800, 3600, 7200, 10800, 43200, 86400, 172800, 259200, 604800]
return intervals.map((value) => ({
value,
label: formatUnitLabel(value)
}))
})
const ACard = createAsyncComponent(import('ant-design-vue'), 'Card')
const store = useMainStore()
const { serverList } = storeToRefs(store)
const { serverReactive } = inject('GlobalProvide')
const TimerServerOptions = computed(() => {
const options = serverList.value.map((item) => {
const name = item.Name
const obj = { value: name, label: item.ServerName ? item.ServerName : name }
if (name === 'Nginx') {
obj.disabled = true
}
return obj
})
options.unshift({ label: t('Website') + ' PHP-FPM', value: 'PHP-FPM' })
return options
})
onMounted(async () => {
if (serverList?.value?.length > 0 && Settings.get('AutoTimerRestartServer') && Settings.get('AutoTimerServerList')) {
setRestartTimer()
}
})
const restartServer = async () => {
const initServerStatus = async (item) => {
const processList = Settings.get('AutoTimerServerList')
if (processList.length > 0 && processList.includes(item.Name)) serverReactive.restartFn(item.Name)
}
const promiseArray = serverList.value.map((item) => initServerStatus(item))
await Promise.all(promiseArray)
}
// 定时器管理
let restartTimer = null
const setRestartTimer = () => {
if (restartTimer) {
clearInterval(restartTimer)
}
if (store.settings.AutoTimerRestartServer && store.settings.AutoTimerInterval) {
restartTimer = setInterval(() => {
restartServer()
}, store.settings.AutoTimerInterval * 1000)
}
}
// 重设定重启间隔时自动更新定时器
watch(() => store.settings.AutoTimerInterval, setRestartTimer)
watch(() => store.settings.AutoTimerRestartServer, setRestartTimer)
const AutoTimerServerChange = () => {
store.setSettings('AutoTimerServerList')
}
const changeAutoTimerInterval = () => {
store.setSettings('AutoTimerInterval')
}
const changeAutoTimerRestartServer = () => {
store.setSettings('AutoTimerRestartServer')
setRestartTimer()
}
</script>
<style scoped></style>
2 changes: 2 additions & 0 deletions src/renderer/views/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<div class='content-container'>
<App />
<Server />
<Timer />
<EnvVar />
<Other />
<UserPwd v-if='!isWindows' />
Expand All @@ -11,6 +12,7 @@
<script setup>
import App from '@/renderer/components/Settings/App.vue'
import Server from '@/renderer/components/Settings/Server.vue'
import Timer from '@/renderer/components/Settings/Timer.vue'
import Other from '@/renderer/components/Settings/Other.vue'
import EnvVar from '@/renderer/components/Settings/EnvVar.vue'
import UserPwd from '@/renderer/components/Settings/UserPwd.vue'
Expand Down
11 changes: 10 additions & 1 deletion src/shared/i18n/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,14 @@ export default {
'file does not exist': 'file does not exist',
'Error opening file!': 'Error opening file!',
'does not exist!': 'does not exist!',
'Please reset the text editor': 'Please reset the text editor'
'Please reset the text editor': 'Please reset the text editor',
Timer: 'Timer',
TimerServerList: 'Timed Service List',
RestartIntervalText: 'Timed Cycle',
ServerAutoRestartText: 'Scheduled Restart Service',
Hours: 'Hours',
Minutes: 'Minutes',
Seconds: 'Seconds',
Days: 'Days',
Weeks: 'Weeks'
}
11 changes: 10 additions & 1 deletion src/shared/i18n/fr.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,14 @@ export default {
'file does not exist': 'fichier ne existe pas',
'Error opening file!': "Erreur lors de l'ouverture du fichier !",
'does not exist!': "n'existe pas!",
'Please reset the text editor': 'Veuillez réinitialiser votre éditeur de texte'
'Please reset the text editor': 'Veuillez réinitialiser votre éditeur de texte',
Timer: 'Minuterie',
TimerServerList: 'Liste de services temporisés',
RestartIntervalText: 'Cycle horaire',
ServerAutoRestartText: 'Service de redémarrage planifié',
Hours: 'Heures',
Minutes: 'Minutes',
Seconds: 'Secondes',
Days: 'Jours',
Weeks: 'Semaines'
}
11 changes: 10 additions & 1 deletion src/shared/i18n/zh.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,14 @@ export default {
'file does not exist': '文件不存在',
'Error opening file!': '打开文件出错!',
'does not exist!': '不存在!',
'Please reset the text editor': '请重新设置文本编辑器'
'Please reset the text editor': '请重新设置文本编辑器',
Timer: '定时器',
TimerServerList: '服务列表',
RestartIntervalText: '重启周期',
ServerAutoRestartText: '定时重启服务',
Hours: '小时',
Minutes: '分钟',
Seconds: '秒',
Days: '天',
Weeks: '周'
}

0 comments on commit 42a1e63

Please sign in to comment.