【发布时间】:2014-04-28 08:49:30
【问题描述】:
我编写了一个类,它从 xml 文件同步数据库并通过电子邮件报告任何警报。
xml 包含产品价格和库存。
仅当 xml 文件时间比上次同步的文件时间更新时才会执行该方法。
这是第一个问题。我怀疑服务器(随机)出于某种原因更改了文件时间,因为虽然没有生成新的 xml 文件,但同步方法仍在运行。
xml文件从本地服务器导出,通过ftp客户端上传到远程服务器 (同步返回)
第二个问题是,在交通繁忙的时间,do_sync 方法会运行不止一次,因为我在电子邮件中不止一次收到警报。
我明白为什么它会被多次调用,所以我创建了一个标志syncing_now,以防止执行。
错误是标志存储在数据库中,由于第一次调用必须更新数据库,所有其他调用都可以运行该方法。
<?php class Sync extends Model{
public function __construct(){
parent::__construct();
$this->syncing_now = $this->db->get($syncing_now);
}//END constructor
public function index(){
if($this->determine_sync()){
$this->do_sync();
}else{
return FALSE;
}
}
public function determine_sync(){
if( filemtime($file) <= $this->db->last_sync() or !$this->$syncing_now){
return FALSE;
}else{
return TRUE;
}
}
public function do_sync(){
$this->db->update('syncing_now', TRUE);
//the sync code works fine..
$this->db->update('syncing_now', FALSE);
}
}
那么我该怎么做才能只运行一次该方法以及如何追踪文件时间更改发生的原因?
感谢所有帮助。
【问题讨论】:
-
您是否也使用syncing_now 进行第一次通话?
-
查看
LOCK_TABLES并使用表格进行同步? dev.mysql.com/doc/refman/5.1/en/lock-tables.html