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

Cannot Cancel ScheduledFuture task after configuration of custom node was updated #10647

Closed
EKalynych opened this issue Apr 26, 2024 · 2 comments
Assignees
Labels
question generic question

Comments

@EKalynych
Copy link

Component

  • Rule Engine

Description
We have code of configurable custom node where init() function creates scheduledFuture task to call some action after configured time (for example 24 hours).
As you can see code checks that if is previous scheduledFuture task is undone - if so, we cancel it and create another.
So if configuration changed in range to 24 hours after last configuration update - init() function executed repeatedly. This mean the previous task should be canceled, and previous scheduled task should not be called.
But it is not working. After each configuration update (within 24 hours) state of scheduledFuture still null and previous task will also be invoked, and not cancelled at all.

Any suggestions please how to fix it? How can we cancel scheduled task if configuration of node was updated before scheduled task is done?
Thanks

Here is code
`

private ScheduledExecutorService scheduledExecutor;
private ScheduledFuture<?> scheduledFuture;
private TbVixCalculateAvailabilityNodeConfiguration config;

@Override
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
    this.config = TbNodeUtils.convert(configuration, TbRaiseScheduledEventNodeConfiguration.class);
    this.scheduledExecutor = Executors.newSingleThreadScheduledExecutor();

    if (scheduledFuture != null && !scheduledFuture.isDone()) {
    // If there is a scheduled task running, cancel it
       scheduledFuture.cancel(true);
    }

    scheduledFuture = scheduledExecutor.schedule(() -> doSomething(ctx), 86400, TimeUnit.MILLISECONDS);
}

`

Environment

  • OS: Ubuntu
  • ThingsBoard: V3.6.1
  • Language: Java
@EKalynych EKalynych added the question generic question label Apr 26, 2024
@ViacheslavKlimov
Copy link
Contributor

ViacheslavKlimov commented Apr 26, 2024

New node instance is created after updating its configuration, and the old one is destroyed.
In order to cancel the previous scheduled task, you need to do that in the destroy method.

@EKalynych
Copy link
Author

New node instance is created after updating its configuration, and the old one is destroyed. In order to cancel the previous scheduled task, you need to do that in the destroy method.

It works.
Thank you Viacheslav.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question generic question
Projects
None yet
Development

No branches or pull requests

3 participants