`
sillycat
  • 浏览: 2551254 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit

 
阅读更多
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running with Plugins

My serverless.yml will be changed as follow:
service: orderinfo

provider:
  name: tencent
  region: ap-shanghai
  runtime: Nodejs8.9
  credentials: ~/.credentials.ini
  stage: prod
  memorySize: 128
  timeout: 60

plugins:
  - serverless-tencent-scf

package:
  excludeDevDependencies: false
  exclude:
    - .gitignore
    - .git/**
    - .serverless
    - .env
    - app.js
    - .DS_Stone
 
functions:
  order-manager:
    handler: handler.orderManager
    description: Task Manager to Fetch Tasks
    memorySize: 128
    timeout: 60
    events:
      # 以下是各触发器配置示例
      # - timer:
      #     name: test
      #     cron: "*/5 * * * *"
      - apigw:
          name: order-manager_apigw
          parameters:
            stageName: release
            serviceId: service-mxgirfqn
            httpMethod: GET
  order-runner:
    handler: handler.ordeRunner
    description: Task Runner to Calculate
    events:
      - apigw:
          name: order-runner_apigw
          parameters:
            stageName: release
            serviceId: service-4qn5efd1
            httpMethod: GET
 

My handler.js will be as follow:
'use strict';
const {OrderService} = require('./src/service');
const {DB} = require('async-mysql-wrapper');
const _ = require('lodash');

exports.orderManager = async (event, context, callback) => {
    console.log("orderManager, event:" + JSON.stringify(event));
    DB.init({
        dbConnectionConfig: {
            host: process.env.MYSQL_DB_ADDRESS,
            user: process.env.MYSQL_DB_USER,
            database: process.env.MYSQL_DB_NAME,
            password: process.env.MYSQL_DB_PASSWORD,
            connectionLimit: process.env.MYSQL_DB_POOL_SIZE || 2,
            waitForConnections: true,
            dateStrings: true,
        },
        logLevel: 'debug'
    });
    await OrderService.groupLoadOrderInfo();
    return "success";
};

exports.ordeRunner = async (event, context, callback) => {
    const msg = _.get(event, 'message') || _.get(JSON.parse(_.get(event, 'body')), 'message');
    console.log("ordeRunner, message:" + msg);
    DB.init({
        dbConnectionConfig: {
            host: process.env.MYSQL_DB_ADDRESS,
            user: process.env.MYSQL_DB_USER,
            database: process.env.MYSQL_DB_NAME,
            password: process.env.MYSQL_DB_PASSWORD,
            connectionLimit: process.env.MYSQL_DB_POOL_SIZE || 2,
            waitForConnections: true,
            dateStrings: true,
        },
        logLevel: 'debug'
    });
    await OrderService.calculateAllOrderInfo(msg);
    return "success";
};


References:
https://github.com/tencentyun/serverless-tencent-scf/blob/master/example/serverless.yml



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics