簡體   English   中英

我如何查看 azure 函數的變化?

[英]How can i watch for changes in my azure functions?

我正在使用用 NODE.JS 編寫的 azure 函數。我正在啟動本地服務器

func host start

一切正常。 但問題是當我更改我的 azure 函數中的某些內容時 - 例如 index.js 文件,當我添加一個簡單的控制台日志時


module.exports = async function (context, req) {
        console.log('request came');
        context.res = {
            body: { success: true, message: null, response: context.bindings.inputDocument }
        };
};

服務器未重新啟動,我需要停止服務器再次運行 func host start 以查看不好的更改。 在 nodejs 中,我們有 nodemon,在 azure 函數中有什么東西可以用來觀察變化而無需停止並在每次變化時啟動服務器嗎?

我試過的

我嘗試在我的 host.json 文件中添加

  "watchDirectories": [ "*" ] 

但沒有成功。

我也試過

在 package.json 文件的scripts中編輯開始屬性

"scripts": {
    "start": "start npm run watch & npm run start:host",
    "test": "echo \"No tests yet...\""
  },

反而

"scripts": {
    "start": "func start",
    "test": "echo \"No tests yet...\""
  },

但仍然沒有成功。

我正在使用 azure 函數 v2。

你在做什么是正確的,但你不需要手表,因為這是為了觀看共享代碼,例如你引用的另一個文件夾中的代碼。

我們做以下

"watch": "tsc --w",
"start:host": "func start --javascript",
"start": "npm-run-all --parallel start:host watch",

我們開始

npm start

npm-run-all是一個 package,用npm install npm-run-all --save-dev

如果它不起作用,則可能是您在local.settings.json中有一些額外的設置,例如WEBSITE_RUN_FROM_PACKAGE

如果您曾經運行過func azure functionapp fetch-app-settings那么它將獲取所有設置,包括您不需要的設置,讓WEBSITE_RUN_FROM_PACKAGE防止自動重啟。

服務器未重新啟動,我需要停止服務器再次運行 func host start 以查看不好的更改。 在 nodejs 中我們有 nodemon,在 azure 函數中是否有這方面的東西,我可以使用它來觀察變化而無需停止並在每次更改時啟動服務器?

對於這個問題,我認為答案是否定的。 The restart function is necessary, not only on azure, but also locally, every time the code is updated, the runtime will be restarted (it will download and load some dependency packages when restarting, and then start the entire azure function runtime).

就我而言,情況是“在watchDirectories下更改將使function主機停止而不是重新啟動”(無論我更改文件中的內容還是添加文件。)在azure上時,它是相似的。 function 內容更改后重啟。

您可以啟動兩個進程(基於@azure/functions 1.2.3):

  • 流程一: tsc -w (或npm run watch
  • 過程2: func start (或npm start

Typescript 編譯器將在監視模式下工作,自動編譯更改,azure func 工具也將在更改時重新加載它的函數。

尤里卡。 我以優雅的方式解決了它。

啟用調試模式熱重載的部分:-

  • .vscode/launch.json
{
   "name":"Attach to Node Functions",
   "type":"node",
   "request":"attach",
   "port":9229,
   "preLaunchTask":"func: host start",
   "restart":true
}
  • .vscode/task.json
[
   {
      "type":"func",
      "command":"host start",
      "problemMatcher":"$func-node-watch",
      "isBackground":true,
      "dependsOn":"npm build"
   },
   {
      "type":"shell",
      "label":"npm build",
      "command":"npm run watch",
      "dependsOn":"npm install",
      "problemMatcher":"$tsc-watch",
      "isBackground":true
   },
   {
      "type":"shell",
      "label":"npm install",
      "command":"npm install"
   },
   {
      "type":"shell",
      "label":"npm prune",
      "command":"npm prune --production",
      "dependsOn":"npm build",
      "problemMatcher":[
         
      ]
   }
]

這里的代碼參考: https://github.com/safwanmasarik/Azure-Function-Typescript-Hot-Reload

視頻證據: https://user-images.githubusercontent.com/35250295/201973704-b11eeb4a-8d41-4e0b-9f9b-385fdcf10846.mp4

Git 問題: https://github.com/Azure/azure-functions-core-tools/issues/1239#issuecomment-1315565942

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM