1 | exports.handler = async function (event, context, callback) {
|
2 | return new Promise((resolve, reject) => {
|
3 | const cluster = require('cluster')
|
4 | if (cluster.isMaster) {
|
5 | // 创建子进程模拟线上请求的隔离方便global变量的使用保持与FC环境的一致性
|
6 | const worker = cluster.fork()
|
7 | worker.on('message', (msg) => {
|
8 | // console.log(msg,typeof msg)
|
9 | resolve(msg)
|
10 | })
|
11 | worker.on('error', error => {
|
12 | // console.log(error)
|
13 | })
|
14 | worker.on('exit', function (worker, code, signal) {
|
15 | // console.log('Worker %d died with code/signal %s. Restarting worker...', worker.process.pid, signal || code);
|
16 | // cluster.fork();
|
17 | })
|
18 | } else {
|
19 | console.log(event, context, callback)
|
20 | // callback()
|
21 | process.on('unhandledRejection', (err) => {
|
22 | // console.log('unhandledRejection', err)
|
23 | // process.send('aaaa')
|
24 | process.exit()
|
25 | })
|
26 | process.on('uncaughtException', (err) => {
|
27 | // console.log('uncaughtException', err)
|
28 | // process.send('aaaa')
|
29 | process.exit()
|
30 | })
|
31 | // throw new Error('asxxxxx')
|
32 | process.send({a: 'aaaa', b: 'bbbb'})
|
33 | process.exit()
|
34 | }
|
35 | })
|
36 | }
|
37 |
|
38 | exports.handler(1, 2, () => {
|
39 | // console.log('xxxxxxxx')
|
40 | }).then(data => {
|
41 | console.log(data)
|
42 | })
|