Running Azure Functions with VS Code instanlty fails with ECONNREFUSED - python

Yesterday I could run and debug my Azure Function project with VS Code. Today, when I'm hitting F5 ("Start Debugging"), a pop-up instantly appears with
connect ECONNREFUSED 127.0.0.1:9091
I believe it's a network issue, since VS Code doesn't even open a terminal displaying "Executing task [...]".
But within VS Code I can browse the extension marketplace, run "pip install" within the terminal.
Here is my configuration:
launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current file",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        },
        {
            "name": "Azure Functions",
            "type": "python",
            "request": "attach",
            "port": 9091,
            "preLaunchTask": "func: host start"
        }
    ]
}
tasks.json
{
"version": "2.0.0",
"tasks": [
{
"type": "func",
"command": "host start",
"problemMatcher": "$func-python-watch",
"isBackground": true,
"dependsOn": "pipInstall",
"options": {
"cwd": "${config:azureFunctions.projectSubpath}"
}
},
{
"label": "pipInstall",
"type": "shell",
"command": "${config:python.pythonPath} -m pip install -r requirements.txt",
"problemMatcher": [],
"options": {
"cwd": "${config:azureFunctions.projectSubpath}"
}
}
]
}
settings.json
{
"azureFunctions.deploySubpath": "azure-functions\\my_project",
"azureFunctions.projectSubpath": "azure-functions\\my_project",
"azureFunctions.scmDoBuildDuringDeployment": true,
"azureFunctions.pythonVenv": ".venv",
"azureFunctions.projectLanguage": "Python",
"azureFunctions.projectRuntime": "~3",
"debug.internalConsoleOptions": "neverOpen",
"python.pythonPath": "C:\\Users\\myself\\BigRepo\\azure-functions\\my_project\\.venv\\Scripts\\python.exe",
"powershell.codeFormatting.addWhitespaceAroundPipe": true
//"terminal.integrated.shell.windows": "&&"
}

This is so weird. I edited my launch.json back and forth, sometimes it was a valid one, sometimes it was not, and now when I'm saving my original launch.json, the configuration works.
This configuration works fine:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current file",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Azure Functions",
"type": "python",
"request": "attach",
"port": 9091,
"preLaunchTask": "func: host start"
}
]
}
I'm not sure I understand what is going on.

Re-install everything.
It works.

Related

Azure function timeTrigger is not present in portal

I have an azure function app running in Linux system. In it, I have create two functions, one is triggered by a blob trigger and the other with timetrigger.
Both functions are deployed with azure DevOps but when I go to portal, timetrigger function is not present.
To deploy the functions, I have the code in Git repository and it is copied to a .zip folder to build the artifact. Once artifact is built, it is deployed to function app with azure cli.
Code:
function.json
{
"schedule": "0 30 14 * * *",
"name": "myTimer",
"type": "timerTrigger",
"direction": "in",
"runOnStartup": false
}
host.json
{
"version": "2.0",
"logging": {
"fileLoggingMode": "always",
"applicationInsights": {
"samplingSettings": {
"isEnabled": true
}
}
},
"extensions": {
"queues": {
"maxPollingInterval": "00:00:02",
"visibilityTimeout" : "00:00:30",
"batchSize": 8,
"maxDequeueCount": 5,
"newBatchThreshold": 4,
"messageEncoding": "base64"
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[3.3.0, 4.0.0)"
},
"functionTimeout": "-1",
"retry": {
"strategy": "fixedDelay",
"maxRetryCount": 0,
"delayInterval": "00:00:05"
}
}
init.py
import datetime
import logging
import azure.functions as func
def main(mytimer: func.TimerRequest) -> None:
utc_timestamp = datetime.datetime.utcnow().replace(
tzinfo=datetime.timezone.utc).isoformat()
if mytimer.past_due:
logging.info('The timer is past due!')
logging.info('Python timer trigger function ran at %s', utc_timestamp)
SOLUTION
I had wrong configured function.json file. Correct content is:
{
"bindings": [
{
"schedule": "0 30 14 * * *",
"name": "myTimer",
"type": "timerTrigger",
"direction": "in",
"runOnStartup": false
}
],
"disabled": false,
"scriptFile": "__init__.py"
}
Thanks for confirming #vll1990,
Posting the solution as an answer This will be beneficial for other members for the similar issue so that they can find and fix their issue.
We have tried to create a python azure function using timer trigger and figured out as you that we need to have our function.json file in below format.
The function.json that you are using is:
{
"schedule": "0 30 14 * * *",
"name": "myTimer",
"type": "timerTrigger",
"direction": "in",
"runOnStartup": false
}
Instead of that We need to specify the bindings before adding the value in our .json file .
For example:-
{
"bindings": [
{
"schedule": "0 30 14 * * *",
"name": "myTimer",
"type": "timerTrigger",
"direction": "in",
"runOnStartup": false //
}
],
"disabled": false,
"scriptFile": "__init__.py"
}
And then after deploy we will be able to see the timer function in our function app on Portal itself.
For more information please refer this MICROSOFT DOCUMENT| Timer trigger for Azure Functions.

I'm trying to run a node.js app from python. But It is not working

I want to run a node.js app from python. To run the node.js app in a terminal I type "npm start".
This is my package.json
{
"name": "web-scraping",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js"
},
"author": "pupy frias",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"express-handlebars": "^5.3.3",
"nodemon": "^2.0.12",
"sqlite3": "^5.0.2"
}
}
I tried with os.system from python, but it didn't work.
So, tried with this
from subprocess import call
call(["node", "Offer Store\app.js"])
It worked, but don't find any files like views. When I run the app from any terminal (cmd, shell) it works, I don't know what is happening with this method. Please help me how to solve this issue. Thanks

VsCode different behavior between run-mode and debug-mode

when I run the following python code in run mode, the response equals 0, when I run it in debug mode, the response is 127 and the failure /bin/sh: ping: command not found occured.
cmd = '-c 1'
host = 192.168.1.1
response = subprocess.call(f'ping {cmd} {host}, shell=True')
my launch.json looks like that:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
When using response = os.system(f'ping {cmd} {host}'), the response in run mode is 0 and in debug mode the response is 32512 and the failure sh: ping: command not found occured.
Can someone help me?

Configure Vs code version 2.0.0 Build Task for python

I need help in configuring my Vs code to run scripts in python using Cntrl Shift B, I was working fine until Vs code upgraded to version 2.0.0 now it wants me to configure the Build. And I am clueless what Build is all about.
In the past it worked well when I only needed to configure the task runner. There are youtube videos for the task runner. I cant seem to lay my finger on what the Build is all about.
In VS Code go Tasks -> Configure Tasks
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"taskName": "Run File",
"command": "python ${file}",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal": "always",
"panel": "new",
"focus": true
}
},
{
"taskName": "nosetest",
"command": "nosetests -v",
"type": "shell",
"group": {
"kind": "test",
"isDefault": true
},
"presentation": {
"reveal": "always",
"panel": "new",
"focus": true
}
}
]
}
command: runs current python file
group: 'build'
presentation:
always shows the shell when run
uses a new shell
focuses the shell (i.e. keyboard is captured in shell window)
2nd Task is configured as the default test and just runs nosetest -v in the folder that's currently open in VS Code.
The "Run Build Task" (the one that's bound to Ctrl+Shift+B) is the one that's configured as the default build task, task 1 in this example (see the group entry).
EDIT:
Suggested by #RafaelZayas in the comments (this will use the Python interpreter that's specified in VS Code's settings rather than the system default; see his comment for more info):
"command": "${command:python.interpreterPath} ${file}"
...Don't have enough reputation to comment on the accepted answer...
At least in my environment (Ubuntu 18.04, w/ virtual env) if arguments are being passed in with "args", the file must be the first argument, as #VladBezden is doing, and not part of the command as #orangeInk is doing. Otherwise I get the message "No such file or directory".
Specifically, the answer #VladBezden has does work for me, where the following does not.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "${config:python.pythonPath} setup.py", // WRONG, move setup.py to args
"group": {
"kind": "build",
"isDefault": true
},
"args": [
"install"
],
"presentation": {
"echo": true,
"panel": "shared",
"focus": true
}
}
]
}
This took me a while to figure out, so I thought I would share.
Here is my configuration for the build (Ctrl+Shift+B)
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "python",
"group": {
"kind": "build",
"isDefault": true
},
"args": [
"setup.py",
"install"
],
"presentation": {
"echo": true,
"panel": "shared",
"focus": true
}
}
]
}

gce startup startup-script not firing

I have the following startup script variable defined in my python script:
default_startup_script = """
#! /bin/bash
cd ~/git/gcloud;
git config --global user.email "my.email#gmail.com";
git config --global user.name "my.name";
git stash;
git pull https://user:pw#bitbucket.org/url/my_repo.git;
"""
and the following config:
config = {
"name": "instance-bfb6559d-788f-48b7-85a3-8ff3ab6e5a60",
"zone": "projects/username-165421/zones/us-east1-b",
"machineType": "projects/username-165421/zones/us-east1-b/machineTypes/f1-micro",
"metadata": {
"items": [{'key':'startup-script','value':default_startup_script}]
},
"tags": {
"items": [
"http-server",
"https-server"
]
},
"disks": [
{
"type": "PERSISTENT",
"boot": True,
"mode": "READ_WRITE",
"autoDelete": True,
"deviceName": "instance-4",
"initializeParams": {
"sourceImage": "projects/username-165421/global/images/image-id",
"diskType": "projects/username-165421/zones/us-east1-b/diskTypes/pd-standard",
"diskSizeGb": "10"
}
}
],
"canIpForward": False,
"networkInterfaces": [
{
"network": "projects/username-165421/global/networks/default",
"subnetwork": "projects/username-165421/regions/us-east1/subnetworks/default",
"accessConfigs": [
{
"name": "External NAT",
"type": "ONE_TO_ONE_NAT"
}
]
}
],
"description": "",
"labels": {},
"scheduling": {
"preemptible": False,
"onHostMaintenance": "MIGRATE",
"automaticRestart": True
},
"serviceAccounts": [
{
"email": "123456-compute#developer.gserviceaccount.com",
"scopes": [
"https://www.googleapis.com/auth/devstorage.read_only",
"https://www.googleapis.com/auth/logging.write",
"https://www.googleapis.com/auth/monitoring.write",
"https://www.googleapis.com/auth/servicecontrol",
"https://www.googleapis.com/auth/service.management.readonly",
"https://www.googleapis.com/auth/trace.append"
]
}
]
}
Now - the instance creates without issue, but the startup script does not fire.
I am creating the instance by running:
compute.instances().insert(
project=project,
zone=zone,
body=config).execute()
All of the samples were retrieved from here.
Once the instance is created and I paste my startup script manually it works without issue.
Does anyone have any idea what I am doing wrong here?
This works. My issue was related to user accounts. I was not logging in as the default user (Eg: username#instance-id).
If you are reading this question just be sure of which username you are intending to run this for and manage accordingly.

Categories