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

Pre & Post callbacks per task for Tasks Runner #29855

Closed
ivankravets opened this issue Jun 29, 2017 · 33 comments
Closed

Pre & Post callbacks per task for Tasks Runner #29855

ivankravets opened this issue Jun 29, 2017 · 33 comments
Assignees
Labels
api feature-request Request for new features or functionality tasks Task system issues
Milestone

Comments

@ivankravets
Copy link

Copy from #20446 (comment)


Is possible to subscribe to onDidTaskRun(), where would be an ability to catch up some tasks and perform some operations? We have pre and post callbacks in Atom Build for each task.

Simple use case. User open @platformio Serial Monitor via integrated Terminal, we see it. He triggers "Upload" task, we temporary close opened Serial Port Monitors/Terminals, then uploading firmware. If a task fails with an error, we don't re-open Monitor. However, if the upload was successful, we re-open last Serial Port Monitor.

Thanks for the hints!

@dbaeumer
Copy link
Member

Nice idea. With 1.14 we will have task API (without this feature) but now that API for tasks is there in general I think adding these pre / post hooks should not be too complicated.

@ivankravets
Copy link
Author

Do you have some progress on this feature?

Thanks!

@dbaeumer
Copy link
Member

dbaeumer commented Mar 9, 2018

None. However we have planned for this milestone to allow extension to participate in task execution. But no concrete API yet so I don't know if the new API will support this use case.

@ivankravets
Copy link
Author

Oh...1 year... Do you have any idea how could we kill running tasks without annoying warning/popup?

We use this code https://github.com/platformio/platformio-vscode-ide/blob/develop/src/main.js#L215

Thanks!

screen shot 2018-03-22 at 18 02 02

@dbaeumer
Copy link
Member

Currently not since the command use designed to work with user interaction. However there is now proposed API that lets you start and terminate tasks. May be that would help in your scenario. It has events for task start and task terminate as well.

@ivankravets
Copy link
Author

However there is now proposed API that lets you start and terminate tasks.

Do you have some ETA when I can test it?

@dbaeumer
Copy link
Member

@ivankravets the API got added to vscode.proposed.d.ts for this milestone. No concrete deadline for the final API yet.

@Misiu
Copy link

Misiu commented Apr 27, 2018

@dbaeumer any updates on this?

@dbaeumer
Copy link
Member

The new API is still in proposed state since I in cooperated feedback and changed some signatures.

@Misiu
Copy link

Misiu commented Apr 27, 2018

@dbaeumer thanks for quick update :)

@dbaeumer
Copy link
Member

If you have feedback now is the time to provide it :-)

@ivankravets
Copy link
Author

Do we have the only proposed API or also the implementation for it?
I would be happy to test it with Insider build.

@dbaeumer
Copy link
Member

The proposed API has an implementation. But you can't use proposed API from a deployed extension. You can only use it during dev time.

@ivankravets
Copy link
Author

I understand, can I try it locally? I just want to be sure that this new API will help us to resolve our issue platformio/platformio-vscode-ide#49

Could you give me some hints how can I try it?

Thanks!

@dbaeumer
Copy link
Member

@jrieken do we have a wiki / issue explaining how to test proposed API?

@dbaeumer
Copy link
Member

Something small from the release doc: https://code.visualstudio.com/updates/v1_21#_proposed-extension-apis

@ivankravets
Copy link
Author

ivankravets commented May 26, 2018

@dbaeumer sorry for the delay. I've just finally found a time to test new API. It seems that it works. However, I can't make our workflow working with the new API. The idea is simple - if we have running Serial Monitor (tasks, NOT task, they can have different monitor args), we should close it before specific tasks (firmware upload, etc). So, I created a simple draft to reproduce this workflow:

Code

    let restoreTask = null;

    vscode.workspace.onDidStartTask(((e) => {
      console.log('onDidStartTask: ' + e.execution.task.name);
      if (e.execution.task.name === 'Monitor') {
        return;
      }
      vscode.workspace.taskExecutions.forEach((e) => {
        if (e.task.name === 'Monitor') {
          restoreTask = e.task;
          e.terminate();
        }
      });
    }));

    vscode.workspace.onDidEndTask(((e) => {
      console.log('onDidEndTask: ' + e.execution.task.name);
      if (e.execution.task.name === 'Monitor' || !restoreTask) {
        return;
      }
      console.log(restoreTask);
      vscode.workspace.executeTask(restoreTask);
      restoreTask = null;
    }));

Output

onDidStartTask: Monitor
onDidStartTask: Clean
onDidEndTask: Monitor
onDidEndTask: Clean
Object
_definition:Object
_scope:Object
_name:"Monitor"
_source:"PlatformIO"
_execution:Object
_problemMatchers:Array[1]
_hasDefinedMatchers:true
_isBackground:false
_presentationOptions:Object
__id:"platformio.platformio-ide.6bafc0e8a0929eaac96ea489fac18aca"

Summary

As you can see, everything works good excluding the last step with vscode.workspace.executeTask(restoreTask);. VSCode does not open the last task automatically. I even printed whole Task object. Is this object invalid?

@ivankravets
Copy link
Author

@dbaeumer do you plan to move this API to stable branch in this May release? This is the last blocking issue for us.

@dbaeumer
Copy link
Member

I already moved it to stable for the May release.

@ivankravets can you provide me with a simple GitHub respository I can clone that demos your problem. Then I will look into fixing it for May.

@ivankravets
Copy link
Author

It's very easy to reproduce, you don't need to have a separate repo. Try to assign "execution.task" instance from vscode.workspace.onDidStartTask to some global variable. See line 10 in https://hastebin.com/ojiqafezev.javascript

So, later in vscode.workspace.onDidEndTask and in line 22 vscode.workspace.executeTask(restoreTask); you can't re-run this "remembered" task again.

@dbaeumer
Copy link
Member

This actually works for me with a simple echo task that I restart when it ended.

cast

@ivankravets
Copy link
Author

ivankravets commented May 30, 2018

See example above, it should be "previous" task, not current.

  1. Start a task A, in onStart remember an instance of execution.task to a global variable
  2. Start a task B, onEnd for this task, re-run the task A using global instance.

@dbaeumer
Copy link
Member

I doubt that this makes a difference but I give it a try

@dbaeumer
Copy link
Member

Works for me. Cast below:

  • start echo and remember it
  • start compile
  • when finished start the remembered echo task

cast

@ivankravets
Copy link
Author

@dbaeumer Could you show me your code? I've just tried the latest Insider build and it does not work for me even for the current task:

    vscode.workspace.onDidStartTask(((e) => {
      console.log('onDidStartTask: ' + e.execution.task.name);
    }));

    vscode.workspace.onDidEndTask(((e) => {
      console.log('onDidEndTask: ' + e.execution.task.name);
      vscode.workspace.executeTask(e.execution.task);
    }));  

Is my code correct? vscode.workspace.executeTask(e.execution.task); ?

@ivankravets
Copy link
Author

ivankravets commented May 30, 2018

I see strange error in develop console. Already tried ~/Downloads/Visual\ Studio\ Code\ -\ Insiders.app/Contents/Resources/app/bin/code --enable-proposed-api platformio-ide but it raises the same error.

To be honest, I don't understand. How does half of new task API work and another doesn't? It seems that vscode.workspace.executeTask does not work for me.

screen shot 2018-05-30 at 22 17 41

@dbaeumer
Copy link
Member

@ivankravets this is the code I used:

    let echoTask: vscode.Task;
    vscode.tasks.onDidStartTask((event) => {
        console.log(`Started ${event.execution.task.name}`);
        if (event.execution.task.name.startsWith('echo')) {
            echoTask = event.execution.task;
        }
    });
    vscode.tasks.onDidEndTask((event) => {
        if (event.execution.task.name.startsWith('compile') && echoTask) {
            vscode.tasks.executeTask(echoTask);
        }
    });

@dbaeumer
Copy link
Member

The exception you are seeing is #50858.

However it should have still worked when running the extension out of source. I will fix #50858 today.

@ivankravets
Copy link
Author

Ok, I've finally found a problem

  1. What is a difference between vscode.tasks.executeTask and vscode.workspace.executeTask? I used to the last before.
  2. console.log(vscode.tasks.executeTask); => null
    Why? "Version 1.24.0-insider (1.24.0-insider), 2018-05-30T07:00:30.548Z"

@dbaeumer
Copy link
Member

The vscode.workspace.executeTask is still proposed and will be removed next milestone. The API started in vscode.workspace since we got more task related API we create a separate namespace vscode.tasks for it. So all task API will be available in the tasks namespace in the future.

Internally they forward to the same implementation. BTW I fixed #50858. Will be available tomorrow in insider.

@ivankravets
Copy link
Author

ivankravets commented May 31, 2018

@dbaeumer Why in the latest insider build vscode.tasks.executeTask === null? Will 8498460 fix that?

@dbaeumer
Copy link
Member

dbaeumer commented Jun 1, 2018

@ivankravets i checked build

Version 1.24.0-insider
Commit a881696
Date 2018-06-01T05:16:40.235Z
Shell 1.7.12
Renderer 58.0.3029.110
Node 7.9.0
Architecture x64

and there everything is correct on the vscode.tasks namespace.

@ivankravets
Copy link
Author

Finally, it works now!!! Thank you so much! 👍 🍺 😊

@vscodebot vscodebot bot locked and limited conversation to collaborators Jul 16, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api feature-request Request for new features or functionality tasks Task system issues
Projects
None yet
Development

No branches or pull requests

5 participants
@Misiu @ivankravets @dbaeumer @Lixire and others