juanjoDiaz / serverless-middleware

Serverless plugin to allow middleware handlers configured directly in serverless.yaml
MIT License
18 stars 11 forks source link

conflict with serverless-plugin-warmup plugin #33

Closed ii-igku closed 3 years ago

ii-igku commented 3 years ago

Hi, I've spotted that the serverless-plugin-warmup plugin stopped working after I've installed serverless-middleware and configured a global 'pre' function. the idea was to extract the warmup check into a global middleware 'pre' function like this:

// ./utils/warmup.js
exports.handler = async (event) => {
  if (event.source === 'serverless-plugin-warmup') {
    console.log(`Keeping lambda warm`);
    await new Promise(r => setTimeout(r, 25));
    throw new Error("WarmupOK"); // this is to stop the rest of the function from executing
  }
}

then I configured serverless.yml like this:

plugins:
  - serverless-offline
  - serverless-plugin-warmup
  - serverless-middleware

custom:
  warmup:
    officeHours:
      enabled: false
      events:
        - schedule: cron(0/3 3-18 ? * MON-FRI *)
  middleware:
    pre:
      - api/utils/warmup.handler

functions:
  callback:
    handler: api/http/callback.handler
    warmup:
      officeHours:
        enabled: false
    events:
      - http:
          path: callback
          method: get

after deploy I have the warmup lambda function created, but it looks like the warmup function was wrapped up by the middleware plugin and in the lambda runtime settings I have HandlerInfo set to .middleware/influencer-index-api-staging-warmup-plugin-officeHours.handler whereas normally it would be set to .warmup/officeHours/index.warmUp

therefore I now have the next error in the warmup lambda logs:

{
    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module 'api-staging-warmup-plugin-officeHours'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
    "stack": [
        "Runtime.ImportModuleError: Error: Cannot find module 'api-staging-warmup-plugin-officeHours'",
        "Require stack:",
        "- /var/runtime/UserFunction.js",
        "- /var/runtime/index.js",
        "    at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
        "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
        "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
        "    at Module._compile (internal/modules/cjs/loader.js:1072:14)",
        "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)",
        "    at Module.load (internal/modules/cjs/loader.js:937:32)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:778:12)",
        "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)",
        "    at internal/main/run_main_module.js:17:47"
    ]
}

is there anything I can do to make it work?

p.s. sorry for duplicating this issue here, just wasn't sure where this should be reported...

juanjoDiaz commented 3 years ago

Hi @ii-igku ,

Plugins are executed in order.

What you probably want is to first transpile your middleware, then add the warmup function (so it's not transpiled) and then start the offline server (so it includes the transpiled functions and the warmup function).

So:

plugins:
  - serverless-middleware
  - serverless-plugin-warmup
  - serverless-offline
ii-igku commented 3 years ago

hi @juanjoDiaz , thanks a lot, changing the order of plugins helped After I put serverless-middleware before serverless-plugin-warmup and deployed to aws the warmup function was not embedded in the middleware wrapper anymore. thanks for the help!