doudaifu6083 2015-01-04 11:45
浏览 210

需要在php中运行长时间的任务

In a commercial project I need to periodically monitor SMTP and Accounting servers to see if its running and working properly. if its dead update a table in mysql. I cant install any 3rd party app on server hosting php script or use exec or modify php settings through php_ini_set each task takes about 10 to 30 seconds I tried to run tasks through Jquery ajax calls and it worked , but the problem is when the jquery request is running you cant navigate to any other page and xhr.abort(); is not working , and stucks on loading until jquery task finishes.

This is what i tried in my js file

var monitor_running = false;
var xhr;
function monitor() {
    if (document.readyState === "complete") {
        if (monitor_running === false) {
            monitor_call();
        }
        else {
            console.log("jobs Already running ===>");
        }
    }
}
window.onbeforeunload = function () {
    console.log(xhr);
    xhr.abort();
    alert(xhr.status);
};
setInterval(monitor, monitor_interval * 1000);

function monitor_call() {
    monitor_running = true;
    console.log("jobs running");

    xhr = $.ajax({url: './ajax.php',
        data: {
            cmd: 'monitor'
        },
        type: 'post',
        async: true,
        success: function (output) {
            monitor_running = false;
            console.log(output + " job is finished");
        }
    });
}

and in php page :

<?php
include_once '../includes/config.php';
$tpl_obj = new template('admin');
$navigation_obj = new navigation();

$auth = $navigation_obj->admin_is_auth();
if (!$auth) {
    die('Auth Failed !');
}

function monitor() {
    sleep(10);
    echo 'done';
//    $monReport['acc'] = monitor::domon('acc');
//    $monReport['smtp'] = monitor::domon('smtp');
//    $monReport['payment'] = monitor::domon('payment');
//    $monReport['dns'] = monitor::domon('dns');
//    return json_encode($monReport);
}

$cmd_post = filter_input(INPUT_POST, 'cmd');
$cmd_get = filter_input(INPUT_GET, 'cmd');


if ($cmd_post == 'monitor') {
    echo monitor();
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?