src/controllers/serviceController.ts   A
last analyzed

Complexity

Total Complexity 30
Complexity/F 7.5

Size

Lines of Code 173
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 134
dl 0
loc 173
rs 10
c 0
b 0
f 0
wmc 30
mnd 26
bc 26
fnc 4
bpm 6.5
cpm 7.5
noi 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
B ServiceController.controlService 0 31 6
B ServiceController.executeRestart 0 30 8
B ServiceController.executeStart 0 30 8
B ServiceController.executeStop 0 31 8
1
import Dnsmasq from '../services/dnsmasq'
2
import Elasticsearch from '../services/elasticsearch'
3
import Mailhog from '../services/mailhog'
4
import Mariadb from '../services/mariadb'
5
import Mysql from '../services/mysql'
6
import Mysql57 from '../services/mysql57'
7
import Mysql80 from '../services/mysql80'
8
import Nginx from '../services/nginx'
9
import PhpFpm from '../services/phpFpm'
10
import PhpFpm72 from '../services/phpFpm72'
11
import PhpFpm73 from '../services/phpFpm73'
12
import PhpFpm74 from '../services/phpFpm74'
13
import PhpFpm80 from '../services/phpFpm80'
14
import Redis from '../services/redis'
15
import Service from '../services/service'
16
import {error, info, success, warning} from '../utils/console'
17
import {getLinkedDatabase} from '../utils/database'
18
import {getLinkedPhpVersion} from '../utils/phpFpm'
19
20
class ServiceController {
21
    allServices: Service[] = [
22
        new Dnsmasq(),
23
        new Elasticsearch(),
24
        new Mailhog(),
25
        new Nginx(),
26
        new Mariadb(),
27
        new Mysql80(),
28
        new Mysql57(),
29
        new PhpFpm80(),
30
        new PhpFpm74(),
31
        new PhpFpm73(),
32
        new PhpFpm72(),
33
        new Redis()
34
    ]
35
36
    executeStart = async (serviceName: string | undefined): Promise<boolean> => {
37
        if (!serviceName) {
38
            for (const service of this.allServices) {
39
                try {
40
                    await this.controlService(service, 'start')
41
                } catch (e) {
42
                    error(`Failed to start ${service.service}: ${e.message}`)
43
                }
44
            }
45
            success('Successfully started all Jale services.')
46
            return true
47
        }
48
49
        for (const service of this.allServices) {
50
            if (service.service.includes(serviceName)) {
51
                try {
52
                    if (!(await this.controlService(service, 'start'))) {
53
                        continue
54
                    }
55
                    success(`Successfully started ${serviceName}.`)
56
                    return true
57
                } catch (e) {
58
                    error(`Failed to start ${service.service}: ${e.message}`)
59
                    return false
60
                }
61
            }
62
        }
63
64
        warning(`Invalid service: ${serviceName}`)
65
        return false
66
    }
67
68
    executeStop = async (serviceName: string | undefined): Promise<boolean> => {
69
        if (!serviceName) {
70
            for (const service of this.allServices) {
71
                try {
72
                    await this.controlService(service, 'stop')
73
                } catch (e) {
74
                    error(`Failed to stop ${service.service}: ${e.message}`)
75
                }
76
            }
77
78
            success('Successfully stopped all Jale services.')
79
            return true
80
        }
81
82
        for (const service of this.allServices) {
83
            if (service.service.includes(serviceName)) {
84
                try {
85
                    if (!(await this.controlService(service, 'stop'))) {
86
                        continue
87
                    }
88
                    success(`Successfully stopped ${serviceName}`)
89
                    return true
90
                } catch (e) {
91
                    error(`Failed to stop ${service.service}: ${e.message}`)
92
                    return false
93
                }
94
            }
95
        }
96
97
        error(`Invalid service: ${serviceName}`)
98
        return false
99
    }
100
101
    executeRestart = async (serviceName: string | undefined): Promise<boolean> => {
102
        if (!serviceName) {
103
            for (const service of this.allServices) {
104
                try {
105
                    await this.controlService(service, 'restart')
106
                } catch (e) {
107
                    error(`Failed to restarted ${service.service}: ${e.message}`)
108
                }
109
            }
110
            success('Successfully restarted all Jale services.')
111
            return true
112
        }
113
114
        for (const service of this.allServices) {
115
            if (service.service.includes(serviceName)) {
116
                try {
117
                    if (!(await this.controlService(service, 'restart'))) {
118
                        continue
119
                    }
120
                    success(`Successfully restarted ${serviceName}.`)
121
                    return true
122
                } catch (e) {
123
                    error(`Failed to restarted ${service.service}: ${e.message}`)
124
                    return false
125
                }
126
            }
127
        }
128
129
        error(`Invalid service: ${serviceName}`)
130
        return false
131
    }
132
133
    /**
134
     * Convenience method to start, stop or restart a service. It also checks if you are restarting PHP or MySQL.
135
     * @param service
136
     * @param action
137
     */
138
    controlService = async (service: Service, action: 'start' | 'stop' | 'restart'): Promise<boolean> => {
139
        if (service instanceof Mysql) {
140
            const linkedDatabase = await getLinkedDatabase()
141
            if (linkedDatabase.service !== service.service) {
142
                return false
143
            }
144
        }
145
146
        if (service instanceof PhpFpm) {
147
            const linkedPhpVersion = await getLinkedPhpVersion()
148
            if (linkedPhpVersion.service !== service.service) {
149
                return false
150
            }
151
        }
152
153
        switch (action) {
154
        case 'start':
155
            info(`Starting ${service.service}...`)
156
            await service.start()
157
            break
158
        case 'stop':
159
            info(`Stopping ${service.service}...`)
160
            await service.stop()
161
            break
162
        case 'restart':
163
            info(`Restarting ${service.service}...`)
164
            await service.restart()
165
            break
166
        }
167
168
        return true
169
    }
170
171
}
172
173
export default ServiceController