大米CMS官网论坛,大米站长联盟,大米站长之家,大米开发者社区
标题:
多进程加锁入库实例
[打印本页]
作者:
追影
时间:
2022-12-16 16:20
标题:
多进程加锁入库实例
<?php
class caijitodb extends Controller
{
public function run($params = null)
{
ini_set('memory_limit', '2048M');
$this->config->set('config_weight_class_id', 1);
$this->config->set('config_language', 'en-gb');
$this->config->set('config_language_id', 1);
$this->config->set('config_length_class_id', 1);
try {
$start_id=0;
$start_file = __DIR__ . DIRECTORY_SEPARATOR . 'todb.txt';
if (is_file($start_file)) {
clearstatcache();
a:
$fp = fopen($start_file, 'r+');
$start_id = @fgets($fp);
var_dump($start_id);
if ($start_id === false) {
sleep(1);
echo '等待解锁'.PHP_EOL;
goto a;
}
flock($fp, LOCK_EX);// 加锁
//$start_id= (int)file_get_contents($start_file);
} else {
echo $start_file.'文件不存在';
exit();
}
$start_id = intval($start_id);
$sql = "select * from i8_pipe where status=0 and id>{$start_id} order by id asc limit 100";
$query = $this->db->query($sql);
$rows = $query->rows;
if ($rows) {
$ids = [];
$total_num = count($rows);
$last_num = ($total_num-1)*1;
$start_id = $rows[$last_num]['id'];
fseek($fp, 0);
fwrite($fp, $start_id);
flock($fp, LOCK_UN);// 解锁
fclose($fp);
foreach ($rows as $k => $row) {
$data = json_decode($row['data'], true);
if ($data) {
try {
$r = $this->load->controller('api/product/add', $data);
if ($r) {
$ids[] = $row['id'];
echo time() . " " . $row['id'] . " into db success" . PHP_EOL;
} else {
echo time() . " " . $row['id'] . " fail" . PHP_EOL;
}
}catch (\Exception $exception){
echo time() . " " . $row['id'] . " fail" . PHP_EOL;
}
}else{
$sql = "delete from i8_pipe where id = ".$row['id'];
$this->db->query($sql);
}
}
if ($ids) {
$sql = "update i8_pipe set status=1 where id in(" . join(',', $ids) . ")";
$this->db->query($sql);
echo time().' success'.PHP_EOL;
}
} else {
flock($fp, LOCK_UN);// 解锁
fclose($fp);
echo 'finished';
exit;
}
$this->run($params);
} catch (\Exception $exception) {
echo $exception->getMessage().PHP_EOL;
if($fp){
flock($fp, LOCK_UN);// 解锁
fclose($fp);
}
}
}
}
复制代码
欢迎光临 大米CMS官网论坛,大米站长联盟,大米站长之家,大米开发者社区 (https://www.damicms.com/bbs/)
Powered by Discuz! X3.1