예제 #1
0
 private static function get_exclusive_by_semaphore()
 {
     if (JxBotExclusion::$sem = sem_get(ftok(__FILE__, "j"), 1)) {
         if (!sem_acquire(JxBotExclusion::$sem, true)) {
             return false;
         }
         return true;
     }
     throw new Exception("Couldn't acquire System V semaphore.");
     return false;
 }
예제 #2
0
 public static function process_scheduled()
 {
     /* detatch from invoking HTTP process so we can't be interrupted */
     JxBotAsyncLoader::detatch_http_request();
     /* ensure we are the only instance running this process */
     try {
         if (!JxBotExclusion::get_exclusive()) {
             return;
         }
     } catch (Exception $err) {
         JxBotAsyncLoader::log(JxBotAsyncLoader::LOG_LEVEL_ERROR, $err->getMessage(), '');
         return;
     }
     /* iterate through all scheduled files */
     while (true) {
         $stmt = JxBotDB::$db->prepare('SELECT name FROM file WHERE status = \'Scheduled\' ORDER BY name LIMIT 1');
         $stmt->execute();
         $next = $stmt->fetchAll(PDO::FETCH_NUM);
         if (count($next) == 0) {
             return;
         }
         /* we're done */
         $file = $next[0][0];
         /* flag the file to indicate we're processing it, and prevent a loop if something is amiss */
         JxBotAsyncLoader::set_file_status($file, 'Loading');
         /* does the file actually exist? */
         $path = JxBotConfig::aiml_dir() . $file;
         if (!file_exists($path)) {
             JxBotAsyncLoader::set_file_status($file, 'Not Available');
             continue;
         }
         /* run the AIML importer */
         $importer = new JxBotAimlImport();
         $result = $importer->import($path);
         // ! TODO:  The notices, warnings and errors of this mechanism should be
         //          sent to us via our log() method, not passed back in an array. **
         /* check for errors and notices */
         if (is_array($result)) {
             JxBotAsyncLoader::set_file_status($file, 'Loaded');
             JxBotAsyncLoader::log(JxBotAsyncLoader::LOG_LEVEL_NOTICE, 'Loaded.', $file);
             /* log the results */
             foreach ($result as $notice) {
                 JxBotAsyncLoader::log(JxBotAsyncLoader::LOG_LEVEL_WARNING, $notice, $file);
             }
         } else {
             JxBotAsyncLoader::set_file_status($file, 'Load Error');
             JxBotAsyncLoader::log(JxBotAsyncLoader::LOG_LEVEL_ERROR, $result, $file);
         }
     }
 }