workerman also has scheduled tasks

Keywords: Programming PHP Redis MySQL JSON

Download workerman

https://www.workerman.net/download

 

2. Download workerman/mysql

http://doc3.workerman.net/640201

1. The timing function is an anonymous function (closure)

use \Workerman\Worker;
use \Workerman\Lib\Timer;
require_once './Workerman/Autoloader.php';

$task = new Worker();
// Start how many processes to run timing tasks, and pay attention to the concurrency of multiple processes
$task->count = 1;
$task->onWorkerStart = function($task)
{
  // Every 2.5 seconds
  $time_interval = 2.5;
  Timer::add($time_interval, function()
  {
    echo "task run\n";
  });
};

// Run worker
Worker::runAll();

2. Timing function is a common function

require_once './Workerman/Autoloader.php';
use \Workerman\Worker;
use \Workerman\Lib\Timer;

// Ordinary function
function send_mail($to, $content)
{
  echo "send mail ...\n";
}

$task = new Worker();
$task->onWorkerStart = function($task)
{
  $to = 'workerman@workerman.net';
  $content = 'hello workerman';
  // After 10 seconds, execute the task of sending mail. The last parameter is false, which means that it is only run once
  Timer::add(10, 'send_mail', array($to, $content), false);
};

// Run worker
Worker::runAll();

3. Method of timing function as class

require_once './Workerman/Autoloader.php';
use \Workerman\Worker;
use \Workerman\Lib\Timer;

class Mail
{
  // Note that the callback function property must be public
  public function send($to, $content)
  {
    echo "send mail ...\n";
  }
}

$task = new Worker();
$task->onWorkerStart = function($task)
{
  // Send an email in 10 seconds
  $mail = new Mail();
  $to = 'workerman@workerman.net';
  $content = 'hello workerman';
  Timer::add(10, array($mail, 'send'), array($to, $content), false);
};

// Run worker
Worker::runAll();

4. The timing function is a class method (timer is used inside the class)

require_once './Workerman/Autoloader.php';
use \Workerman\Worker;
use \Workerman\Lib\Timer;

class Mail
{
  // Note that the callback function property must be public
  public function send($to, $content)
  {
    echo "send mail ...\n";
  }

  public function sendLater($to, $content)
  {
    // If the callback method belongs to the current class, the first element of the callback array is $this
    Timer::add(10, array($this, 'send'), array($to, $content), false);
  }
}

$task = new Worker();
$task->onWorkerStart = function($task)
{
  // Send an email in 10 seconds
  $mail = new Mail();
  $to = 'workerman@workerman.net';
  $content = 'hello workerman';
  $mail->sendLater($to, $content);
};

// Run worker
Worker::runAll();

6. Static method with class as timing function (with namespace)

namespace Task;
require_once './Workerman/Autoloader.php';
use \Workerman\Worker;
use \Workerman\Lib\Timer;

class Mail
{
  // Note that this is a static method, and the callback function property must also be public
  public static function send($to, $content)
  {
    echo "send mail ...\n";
  }
}

$task = new Worker();
$task->onWorkerStart = function($task)
{
  // Send an email in 10 seconds
  $to = 'workerman@workerman.net';
  $content = 'hello workerman';
  // Calling static methods of classes with namespaces on a regular basis
  Timer::add(10, array('\Task\Mail', 'send'), array($to, $content), false);
};

// Run worker
Worker::runAll();

7. Destroy the current timer in timer (pass $timer? ID in use closure mode)

use \Workerman\Worker;
use \Workerman\Lib\Timer;
require_once './Workerman/Autoloader.php';

$task = new Worker();
$task->onWorkerStart = function($task)
{
  // count
  $count = 1;
  // In order for $timer? ID to be passed to the callback function correctly, $timer? ID must be preceded by an address character&
  $timer_id = Timer::add(1, function()use(&$timer_id, &$count)
  {
    echo "Timer run $count\n";
    // Destroy the current timer after 10 runs
    if($count++ >= 10)
    {
      echo "Timer::del($timer_id)\n";
      Timer::del($timer_id);
    }
  });
};

// Run worker
Worker::runAll();

8. Destroy the current timer in timer (pass $timer? ID in parameter mode)

require_once './Workerman/Autoloader.php';
use \Workerman\Worker;
use \Workerman\Lib\Timer;

class Mail
{
  public function send($to, $content, $timer_id)
  {
    // Temporarily add a count attribute to the current object to record the number of timer runs
    $this->count = empty($this->count) ? 1 : $this->count;
    // Destroy the current timer after 10 runs
    echo "send mail {$this->count}...\n";
    if($this->count++ >= 10)
    {
      echo "Timer::del($timer_id)\n";
      Timer::del($timer_id);
    }
  }
}

$task = new Worker();
$task->onWorkerStart = function($task)
{
  $mail = new Mail();
  // In order for $timer? ID to be passed to the callback function correctly, $timer? ID must be preceded by an address character&
  $timer_id = Timer::add(1, array($mail, 'send'), array('to', 'content', &$timer_id));
};

// Run worker
Worker::runAll();

9. Set timer only in the specified process

There are four processes in a worker instance, and only the timer is set on the process with id number 0.

use Workerman\Worker;
use Workerman\Lib\Timer;
require_once './Workerman/Autoloader.php';

$worker = new Worker();
$worker->count = 4;
$worker->onWorkerStart = function($worker)
{
  // Only set timer on the process with id No. 0, other processes 1, 2 and 3 do not set timer
  if($worker->id === 0)
  {
    Timer::add(1, function(){
      echo "4 individual worker Process, only set timer in process 0\n";
    });
  }
};
// Run worker
Worker::runAll();

shipments.php is used to write scheduled tasks

use Workerman\Worker;
use \Workerman\Lib\Timer;

require_once "Workerman/Autoloader.php";


require_once "Connection.php";

$task = new Worker();

$task->onWorkerStart = function ($task) {

  global $db, $redis;
  $db  = new \Workerman\MySQL\Connection('127.0.0.1', '3306', 'root', 'root', 'test');
  $redis = new Redis();
  $redis->connect('127.0.0.1', 6379);
  $redis->auth("qqq123123.");
  $time_interval = 0.1;
  Timer::add($time_interval, function () {
    global $db, $redis;

    $insert['name'] = 123;

    $db->insert('shipments')->cols($insert)->query();

//    sleep(100);
  });

};


function curlGet($url = '', $options = [])
{
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  if (!empty($options)) {
    curl_setopt_array($ch, $options);
  }
  //https request does not validate certificate and host
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

function newGetOrderInfo($taobao, $orderId)
{
  $taobao = urlencode($taobao);
  $url  = "http://114.55.144.79/taobao/TradeFullinfoGetRequest.php?shop=$taobao&tid=$orderId";
  $json  = curlGet($url);
  return json_decode($json, true)['trade'];
}

Worker::runAll();

 

Posted by StathisG on Thu, 14 Nov 2019 11:12:02 -0800