dr5648 2018-10-03 08:04
浏览 25

如何使用任务计划程序在每天运行文件时按需将文件上载到数据库

I have below code which is scheduled to run daily at midnight using task scheduler. First my code is connecting to the database (function __construct),then it comparing he files which are in my logs folder and files which are already uploaded(function uploadLogs), then function uploadLog is uploading the file to the database which are not uploaded.The file which is uploaded is written a file in order to duplicate data(function updateRegistery).

Now when the file is uploaded once, then it can't be uploaded again, since the name is already written in the registry file. However I want to run it on daily basis. I really cant think of a way to do that. Can someone please help me with the coding.

My code:

<?php

class logAgent
{
    const CONFIG_FILENAME = "data_config.ini";
    
    private $_dbConn;
    private $_config;
    
    
    function __construct()
    {
        $this->_loadConfig();
        
        
        $this->_dbConn = oci_connect($this->_config['db_usrnm'],
            $this->_config['db_pwd'],
            $this->_config['hostnm_sid']);
    }
    
   public function uploadLogs(){
        
        //Comparison of all the files present in the logs directory and all the files which has been already uploaded
        $f = fopen($this->_config['uploadedRegistry'], 'r');
        $contents = [];
        while (FALSE !== ($row = fgetcsv($f, 1000, $this->_config['filenameTimeSeparator']))){
            $contents[] = $row[0];
        }
        $files = array_map('basename',glob($this->_config['logspath'] . $this->_config['fileextension'],GLOB_BRACE));
        $result = array_diff($files, $contents);
        foreach($result as $r){
            $this->uploadLog($r);
        }
    }
    
    
    private function _loadConfig()
    {
        // Loads config
        $path = dirname(__FILE__) . '/' . self::CONFIG_FILENAME;
        $this->_config = parse_ini_file($path) ;
    }
    
    public function uploadLog($filename) {
        
        //Uploads file to database
        $filename = trim($this->_config['logspath'] . trim($filename));
              
        if(file_exists($filename)){
            $myfile = fopen($filename, "r");
            while(!feof($myfile)) {
                $content= fgets($myfile);
                $carray=explode($this->_config['logFomatDelimiter'],$content);

                list($REQUEST_TIME, $HOSTNAME, $IP_ADDRESS, $WORKFLOW_NAME, $USERID, $EVENT_MESSAGE , $OPTIONAL_DETAILS , $SESSION_COOKIES)=$carray;
                
                $data = array_map(function($v) { return explode('=',trim($v));},explode(';',substr($carray[6],strpos($carray[6],'=')+1)));
                foreach ($data as $datum) {
                    if ($datum[0] == 'TANID') {
                        $SESSION_COOKIES= $datum[0] . "=" . $datum[1];
                        break;
                    }
                }
                $statement = "INSERT INTO AUTH_LOGS(REQUEST_TIME, HOSTNAME, IP_ADDRESS, WORKFLOW_NAME, USERID, EVENT_MESSAGE , OPTIONAL_DETAILS , SESSION_COOKIES) 
                 values(to_date(:REQUEST_TIME, 'YYYY/MM/DD HH24:MI:SS'), :HOSTNAME, :IP_ADDRESS, :WORKFLOW_NAME , :USERID ,:EVENT_MESSAGE ,
                 :OPTIONAL_DETAILS, :SESSION_COOKIES)";
                
                //Preparing an Oracle statement for execution
                $compiled = oci_parse($this->_dbConn, $statement);
                
                //binding values to named parameters
                oci_bind_by_name($compiled, ':REQUEST_TIME', $REQUEST_TIME);
                oci_bind_by_name($compiled, ':HOSTNAME', $HOSTNAME);
                oci_bind_by_name($compiled, ':IP_ADDRESS', $IP_ADDRESS);
                oci_bind_by_name($compiled,':WORKFLOW_NAME', $WORKFLOW_NAME);
                oci_bind_by_name($compiled, ':USERID', $USERID);
                $EVENT_MESSAGE = str_replace('"', '', $EVENT_MESSAGE);
                oci_bind_by_name($compiled, ':EVENT_MESSAGE', $EVENT_MESSAGE);
                $OPTIONAL_DETAILS = str_replace('"', '', $OPTIONAL_DETAILS);
                oci_bind_by_name($compiled, ':OPTIONAL_DETAILS', $OPTIONAL_DETAILS);
                $SESSION_COOKIES = str_replace(')"', '', $SESSION_COOKIES);
                oci_bind_by_name($compiled, ':SESSION_COOKIES', $SESSION_COOKIES);
                
              
                //Executing statement
                oci_execute($compiled, OCI_COMMIT_ON_SUCCESS);
            }
            //closing the file
            fclose($myfile);
            $this->updateRegistry($filename);
            return TRUE;
        }
        else{
            throw new Exception("File doesnot exist");
        }
    } 
    
    public function sendEmail(Exception $e){
        
        $sent = mail($this->_config['recipients'], $this->_config['notificationSubject'], $e);
    }
    
    public function updateRegistry($filename)
    {
        
        $uploadedfilename = fopen($this->_config['uploadedRegistry'], "a");
        fwrite($uploadedfilename, basename($filename . date($this->_config['filenameTimeSeparator'] . 'Ymdhi', time())) . PHP_EOL);
    } 
}

try {
    $logAgent = new logAgent();
    $logAgent->uploadLogs();
}
catch (Exception $e) {
    $logAgent->sendEmail($e);
}
?>

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 2024-五一综合模拟赛
    • ¥15 下图接收小电路,谁知道原理
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
    • ¥15 ETLCloud 处理json多层级问题
    • ¥15 matlab中使用gurobi时报错
    • ¥15 这个主板怎么能扩出一两个sata口
    • ¥15 不是,这到底错哪儿了😭