Solved Scheduling an arbitrarily long synchronous task

Discussion in 'Plugin Development' started by cakenggt, Feb 1, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    cakenggt

    In my current plugin I am scheduling a task which may run for an arbitrarily long amount of time. I am attempting to run the task synchronously using the scheduler, but I am running into problems where the server is timing out. It appears as if the server executes the arbitrarily long task and then waits for it to finish before doing anything else.

    Is there a way that I can keep this from timing out, or is this an inherent problem with synchronous bukkit tasks? If it is an inherent problem, is there a way I can run it async and still call bukkit methods?

    Here is a link to the project: https://github.com/cakenggt/SpellScript
    You will find the arbitrarily long command here: https://github.com/cakenggt/SpellSc.../aleclownes/SpellScript/NodeRunnable.java#L30

    The javascript it is trying to evaluate contains a while(true) statement which will run forever.
     
  2. Offline

    1Rogue Retired Staff

    Yes that is how threads work

    You shouldn't create a ScriptEngineManager instance every time you run the runnable, create that object once in your Runnable's constructor and then use it each time.

    I also don't understand why you have wrapper classes like this, it's a waste of resources:

    https://github.com/cakenggt/SpellScript/blob/master/src/com/aleclownes/SpellScript/NodeWrapper.java
     
  3. Offline

    cakenggt

    @1Rogue Each script that is run needs to have a separate scope than any other script, which is why I'm using different managers.

    Do you think that I need to run the scripts asynchronously, then when I need to call bukkit methods, have them be called as a bukkit task through the scheduler?
     
  4. Offline

    1Rogue Retired Staff

    So then only have a single manager per script, instead of a single manager per execution.

    If you save the manager as a field then you'd have a single manager per runnable instance.

    Evaluating the logic of your script asynchronously would be fine, then you could use something such as a Callable<T> and use .callSyncMethod in order to callback to the main thread.
     
    cakenggt likes this.
  5. Offline

    mythbusterma

    @cakenggt

    If they are causing noticeable lag on the server, it seems pretty obvious to me that you need to do it asynchronously. Make sure you make any data you use on both threads thread-safe.
     
    cakenggt likes this.
  6. Offline

    cakenggt

    @mythbusterma
    I've decided to run the tasks asynchronously and to surround any bukkit methods where I am modifying data with the following code:
    Code:
    sched.callSyncMethod(p, new Callable<Boolean>(){
        @Override
        public Boolean call(){
            //code here
            return true;
        }
    });
     
  7. Offline

    mythbusterma

    @cakenggt

    Any data you use has to be properly synchronized between threads, otherwise you're going to run into discrepancies.
     
Thread Status:
Not open for further replies.

Share This Page