-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmaster.js
138 lines (106 loc) · 3.41 KB
/
master.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import uniq from 'lodash/uniq'
import { Hookable } from '../mixins'
import { consola } from '../utils'
import { getNuxt, getGenerator } from '../utils/nuxt'
import Watchdog from './watchdog'
export default class Master extends Hookable() {
constructor (options, { workerCount, workerConcurrency, failOnPageError, adjustLogLevel }) {
super()
this.options = options
this.watchdog = new Watchdog()
this.startTime = process.hrtime()
this.workerCount = parseInt(workerCount)
this.workerConcurrency = parseInt(workerConcurrency)
this.failOnPageError = failOnPageError
if (adjustLogLevel) {
consola.level = consola._defaultLevel + adjustLogLevel
this.options.__workerLogLevel = consola.level
}
this.routes = []
this.errors = []
}
async init () {
if (this.generator) {
return
}
const level = consola.level
const nuxt = await getNuxt(this.options)
consola.level = level // ignore whatever Nuxt thinks the level should be
this.generator = await getGenerator(nuxt)
this.workerCount = this.workerCount || parseInt(nuxt.options.generate.workers) || require('os').cpus().length
this.workerConcurrency = this.workerConcurrency || parseInt(nuxt.options.generate.workerConcurrency) || 500
}
async run ({ build, params } = {}) {
await this.init()
if (build) {
await this.build()
await this.callHook('built', params)
} else {
await this.initiate()
}
await this.getRoutes(params)
if (this.routes.length < 1) {
consola.warn('No routes so not starting workers')
return
}
let options = this.options
if (typeof this.options.generate.beforeWorkers === 'function') {
options = this.options.generate.beforeWorkers(this.options) || this.options
}
await this.startWorkers(options)
}
async initiate (build) {
if (!build) {
build = false
}
await this.generator.initiate({ build, init: build })
}
async build () {
await this.initiate(true)
}
async getRoutes (params) {
try {
const routes = await this.generator.initRoutes(params)
if (routes.length) {
// add routes to any existing routes
Array.prototype.push.apply(this.routes, routes)
this.routes = uniq(this.routes)
}
return true
} catch (e) {
return false
}
}
calculateBatchSize () {
// Even the load between workers
let workerConcurrency = this.workerConcurrency
if (this.routes.length < this.workerCount * this.workerConcurrency) {
workerConcurrency = Math.ceil(this.routes.length / this.workerCount)
}
return workerConcurrency
}
getBatchRoutes () {
const batchSize = this.calculateBatchSize()
const routes = this.routes.splice(0, batchSize)
return routes
}
async done (workerInfo) {
await this.generator.afterGenerate()
let duration = process.hrtime(this.startTime)
duration = Math.round((duration[0] * 1E9 + duration[1]) / 1E8) / 10
const info = {
duration,
errors: this.errors,
workerInfo: workerInfo || this.watchdog.workers
}
if (this.options.generate && typeof this.options.generate.done === 'function') {
await this.options.generate.done(info, this.generator.nuxt)
}
await this.callHook('done', info)
this.errors = []
}
startWorkers (options) {
consola.error('Should be implemented by a derived class')
return false
}
}