Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-run task on folder close in addition to open #65986

Closed
markm77 opened this issue Jan 3, 2019 · 8 comments
Closed

Auto-run task on folder close in addition to open #65986

markm77 opened this issue Jan 3, 2019 · 8 comments
Assignees
Labels
feature-request Request for new features or functionality *out-of-scope Posted issue is not in scope of VS Code tasks Task system issues
Milestone

Comments

@markm77
Copy link

markm77 commented Jan 3, 2019

Thanks very much for adding "run on folder open" to tasks. I wish to request a "run on folder close" to complement this to e.g. stop services that are started on folder open.

I describe my use case in this comment: #54775 (comment).

Another use case is process clean-up. For instance, when using Debug Start with nodemon-hosted Node JS processes, if you quit VSCode "node" processes are left orphaned (and consuming CPU) as shown in Activity Monitor. I would like to do process clean-up in a folder-close task rather than manually in Activity Monitor after quitting VS Code.

@vscodebot vscodebot bot added the tasks Task system issues label Jan 3, 2019
@alexr00 alexr00 added the feature-request Request for new features or functionality label Jan 4, 2019
@alexr00 alexr00 added this to the On Deck milestone Jan 4, 2019
@alexr00
Copy link
Member

alexr00 commented Oct 8, 2019

On your launch config you can specify a postDebugTask. Does that solve your use-case?

@alexr00 alexr00 added the info-needed Issue requires more information from poster label Oct 8, 2019
@markm77
Copy link
Author

markm77 commented Oct 8, 2019

Hi @alexr00, nice to hear from you. I tend to set a number of things up before debug such as "stop remote process on server and activate SSH port forwarding to local" and e.g. start local Redis service which would be really nice to reverse on folder close. These are things you don't want to turn on and off each time you start and stop debugging. The clean-up stuff is also not very appropriate to run as a postDebugTask as my two commands here, "killall node" and "killall Chromium" (for Puppeteer debris), both have undesirable effects when run as a postDebugTask. (The undesirable effects are that "killall node" also kills a legitimate process - IIRC it kills nodemon - and in fact would also stop all other Node debug processes (we don't yet have a Stop All command but this might get close!) and "killall Chromium" would present examination of failed Puppeteer tests.)

Appreciate this might not be the most urgent thing in the world but I think it's the right approach and is symmetrical with the fantastic task-run-on-folder-open you implemented. 😀

@alexr00
Copy link
Member

alexr00 commented Oct 9, 2019

@markfinlabs makes sense, I just wanted to make sure there wasn't already something out there that would solve the problem!

@alexr00 alexr00 removed the info-needed Issue requires more information from poster label Oct 9, 2019
@markm77
Copy link
Author

markm77 commented Oct 9, 2019

Sure, no trouble.

@alexr00 alexr00 added the *out-of-scope Posted issue is not in scope of VS Code label Nov 9, 2020
@alexclst
Copy link

I've just began using the onOpen run config of Tasks as a way to automatically start a development server when I open my workspace. I would like to be able to use a onClose one to stop them as well. So, that is just what my use case for such a feature would be. Just to add to the mix of people requesting this feature for if and when it gets worked on.

@lassebomh
Copy link

lassebomh commented Sep 6, 2023

I would also love this.

In my current setup I use "runOn": "folderOpen" to run docker compose task, that starts my cache and database:

{
    "label": "Start development services",
    "type": "docker-compose",
    "presentation": {
        "reveal": "silent",
        "close": true,
    },
    "runOptions": {
        "runOn": "folderOpen",
    },
    "dockerCompose": {
        "up": {
            "detached": true,
            "build": true
        },
        "files": [
            "${workspaceFolder}/.ci/dev/docker-compose.yml"
        ],
    },
},

This is nice because the task disappears when the services has started, and then avoiding clutter my terminal view. However with this config there doesn't seem to be any way of stopping the services, once I leave the folder / VSCode.

My current solution

I do not run docker compose in detached mode. This takes advantage of the fact that if the task is left running indefinitely, you can emulate folderClose by handling the exit signal sent by VSCode to the task, which in this case stops the docker compose services. Here is an example with a bash script:

#!/bin/bash
on_folder_close() {
    
    echo This is run when the folder / VSCode closes
    
    trap - SIGINT
    kill -- -$$
}

trap on_folder_close SIGINT

echo This is run when the folder is opened

sleep infinity

While this produce the expected behavior, someone else should probably look it over, since I am not an expert on signal handling or bash. The disadvantage here is that the task will always be visible in the terminal view, but it helps if you set presentation.reveal to "silent". Here is my final task:

{
    "label": "Start development services",
    "type": "docker-compose",
    "presentation": {
        "reveal": "silent",
    },
    "runOptions": {
        "runOn": "folderOpen",
    },
    "dockerCompose": {
        "up": {
            "detached": false,
            "build": true
        },
        "files": [
            "${workspaceFolder}/docker-compose.yml"
        ],
    },
},

@Hyperclaw79
Copy link

@alexr00 I know this has been closed as out of scope, but is there any way to get this considered again?
I have another use case where this would really help:
I have a local Kubernetes cluster and one running on GKE. My Ingress points to a fixed domain, i.e., stocksalot.tech. While I am developing locally, I modify my /etc/hosts file to redirect this domain to localhost. But this is a pretty manual process where I have to open and edit the file before every development session and again open and edit it after the session and then close VSCode. If I can configure a task to automatically reset the hosts file before closing VSCode, it would really beneficial.

@alexr00 alexr00 assigned meganrogge and unassigned alexr00 Sep 22, 2023
@dylan-k
Copy link

dylan-k commented Jan 10, 2024

I imagine a feature like this would be quite powerful to use in addition to the Cloud Changes feature. You could save changes on folderClose and retrieve them elsewhere on folderOpen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request Request for new features or functionality *out-of-scope Posted issue is not in scope of VS Code tasks Task system issues
Projects
None yet
Development

No branches or pull requests

8 participants
@dylan-k @alexclst @markm77 @Hyperclaw79 @meganrogge @lassebomh @alexr00 and others