简体   繁体   中英

How to properly delay the loading of a Node/Express API using tasks.json in VS Code

I've got a microservices project which I debug in a single VS Code instance. I use Compounds in launch.json to launch/debug.

There's a "Metadata" service which all other services are dependent on, so it needs to be running before any others start up.

I had this solved and it has worked swimmingly for me, for the last 10 months, but it spontaneously broke, recently.

Here's what I've got.

launch.json:

Compound:

{
    "name": "API-only",
    "stopAll": true,
    "configurations": [
        "Metadata API",
        "Auth API"
    ]
}

...and the individual API configs in the compound:

{
    "type": "node",
    "request": "launch",
    "name": "Metadata API",
    "program": "${workspaceFolder}/metadata-api/bin/www",
    "envFile": "${workspaceFolder}/metadata-api/.env",
    "skipFiles": [
        "<node_internals>/**/*.js",
        "${workspaceRoot}/node_modules/**/*.js"
    ],
    "presentation": {
        "hidden": false,
        "group": "apis",
        "order": 1
    }
},
{
    "type": "node",
    "request": "launch",
    "name": "Auth API",
    "program": "${workspaceFolder}/auth-api/bin/www",
    "envFile": "${workspaceFolder}/auth-api/.env",
    "skipFiles": [
        "<node_internals>/**/*.js",
        "${workspaceRoot}/node_modules/**/*.js"
    ],
    "preLaunchTask": "Preload Delay",
    "presentation": {
        "hidden": true,
        "group": "",
        "order": 1
    }
}

You can see the "preLaunchTask" is set in Auth API but not Metadata API. Here's that:

tasks.json :

{

    "version": "2.0.0",
    "tasks": [
        {
            "label": "Preload Delay",
            "type": "shell",
            "command": "sleep 3",
            "windows": {
                "command": "ping 127.0.0.1 -n 3 > nul"
            },
            "group": "none",
            "presentation": {
                "reveal": "silent",
                "panel": "shared",
                "revealProblems": "onProblem"
            }
        }
    ]
}

I used to be able to watch Metadata load first, then Auth after 3 seconds, in the VSC Call Stack panel. Now, they both show up immediately and often, Auth fails because it's no longer respecting the delay to wait for Metadata to load first, so it can make calls to it and load, itself.

Sure enough, if I manually start Metadata first, or manually pause Auth to wait for Metadata to load, it works every time.

None of this configuration has changed in over 10 months now, so I suspect a recent VSC update had to have broken this? Is there another way? I'm not finding much to go on, out there.

I went through the VS Code documentation and found two problems with your tasks.json.

1-You need to create two different tasks separately for the above mentioned scenario like this eg

 { "version": "2.0.0", "tasks": [{ "label": "Client Build", "command": "gulp", "args": ["build"], "options": { "cwd": "${workspaceFolder}/client" } }, { "label": "Server Build", "command": "gulp", "args": ["build"], "options": { "cwd": "${workspaceFolder}/server" } }, { "label": "Build", "dependsOn": ["Client Build", "Server Build"] } ] }

2- You need to set "dependsOrder": "sequence" and configure dependencies like eg

 { "label": "One", "type": "shell", "command": "echo Hello ", "dependsOrder": "sequence", "dependsOn": ["Two", "Three"] }

Find out More about compound tasks? Good Luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM