14. 14
<?php
$link = mysqli_connect("host", "user", "passw", "db");
$query = 'SELECT * FROM jobs where status = "on"';
$result = $link->query($query);
while($row = mysqli_fecth_array($result)) {
// …....
// do work
// …....
$sql = 'update jobs set status = "off" where id = {$row["id"]}';
$link->query($sql);
}
19. 19
每 10 秒執行一次
<?php
$link = mysqli_connect("myhost","myuser","mypassw","mybd");
$query = 'SELECT * FROM jobs where status = "on"';
$done = false;
while(!$done) {
$result = $link->query($query);
if ($result->num_rows > 0) {
// do work
$sql = 'update jobs set status = "off" where id = {$row["id"]}';
$link->query($sql);
} else {
sleep(10);
}
}
86. 86
Running workers via script
#!/bin/sh
script="index.php cli worker"
folder="/script_path/"
max_worker="5"
php_bin="/usr/bin/php"
count=0
current_count=$(ps aux | grep "index.php cli worker" | grep -v "grep" | wc -l)
if test $current_count -lt $max_worker
then
cd $folder
$php_bin $folder$script &
else
echo "The worker count has been limited."
fi
https://gist.github.com/appleboy/6806323