1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Run function in background

Discussion in 'Development' started by LucGamesDE, Mar 18, 2017.

  1. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
  2. Primus

    Primus Zombie Pigman

    Messages:
    749
    Not sure if gearman will work for pocketmine. Show us the function. If you are directly changing variables in the main thread it will cause issues.
     
  3. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    I run a command (/info). This will start the function getInfo.

    PHP:
    public function getInfo(/*some variables*/) {
    //$info = from website
    return $info;
    }
     
  4. Primus

    Primus Zombie Pigman

    Messages:
    749
    Use AsyncTask.
     
    dktapps likes this.
  5. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    Can u give me an example? I need to run the function getInfo in there
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Can you send us the function?
     
  7. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    PHP:
    public function getInfo($ip$port$timeout 10) {
    if (
    $ip and $port and $timeout)
    {
            
    $minecraft = @fsockopen("$ip"$port$timeout);
    }
    if(
    $minecraft)
    {
    $status "online";
    } else {
    $status "offline";
    }
    }
     
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Create a class "InfoTask" and extend it with \pocketmine\scheduler\AsyncTask.

    Create a public function "onRun()" in the class.
    PHP:

    private $ip$port$timeout;
    public function 
    __construct($ip$port$timeout 10){
        
    $this->ip $ip;
        
    $this->port $port;
        
    $this->timeout $timeout;
    }

    public function 
    onRun(){
        
    $minecraft = @fsockopen($this->ip$this->port$this->timeout);
        
    $status $minecraft "online" "offline";
        
    $this->setResult($status);
    }

    public function 
    onCompletion(Server $server){
        
    $server->getLogger()->notice("
            Background task has finished. The output was:
        "
    .$this->getResult());
    }
    Call InfoTask from another class:
    PHP:
    //$ip = ...
    //$port = ...
    //$timeout = 10
    Server::getInstance()->getScheduler()->scheduleAsyncTask(new InfoTask($ip$port$timeout));
    You can refer to the docs for function descriptions.
     
    Skullex likes this.
  9. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    Error
    Code:
    onCompletion() must be an instance of MyPlugin\Server,
     
  10. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Add use pocketmine\Server;
     
  11. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    And how can I get the result?
    "Server::getInstance()->getScheduler()->scheduleAsyncTask(new InfoTask($ip, $port, $timeout));" only starts it
     
  12. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    add another parameter to construct it with that will allow it to access your plugin's main class
     
    Primus likes this.
  13. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    What do u mean? Example? In what file/class?
     
  14. Primus

    Primus Zombie Pigman

    Messages:
    749
    It's terrible idea to pass objects from main thread to another.
    You want to send this data to player that issued the command, right? So pass player name to AsyncTask and do this
    PHP:
    onCompletation(Server $server) {
       if((
    $player $server->getPlayer($this->issuer)) {
          
    $player->sendMessage($this->getResult());
       }
    }
     
  15. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    No. I have another function in my main thread where I start the task. There I want to return the status
     
  16. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    What is the best way to do that?
     
  17. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    I tried to get the result in another file. But it gives me an error
    PHP:
    $status $this->getServer()->getScheduler()->scheduleAsyncTask(new StatusTask($ip$port$timeout));
    return 
    $status->getResult();

    //error:  Call to a member function getResult() on null
     
  18. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    that's because ScheduleAsyncTask returns void
     
  19. LucGamesDE

    LucGamesDE Baby Zombie

    Messages:
    170
    And how can I get it than?
     
  20. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    PHP:
    private $ip$port$timeout$server$status;
    public function 
    __construct(Server $server$ip$port$timeout 10){
        
    $this->ip $ip;
        
    $this->port $port;
        
    $this->timeout $timeout;
        
    $this->server $server;
    }

    public function 
    onRun(){
        
    $minecraft = @fsockopen($this->ip$this->port$this->timeout);
        
    $this->status $minecraft == true "online" "offline";
    }

    public function 
    onCompletion(Server $server){
        
    $server->getLogger()->notice("
            Background task has finished. The output was:
        "
    .$this->status);
        
    $server->getPluginManager()->getPlugin->("pluginName")->result $this->status;
    }
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.