خرید بک لینک

Vote count: 0

I'm working on a closed system web application to aid companies in their everyday online commerce chores. That means on the one hand that it won't be open to the public, on the other: it will have to deal with large amounts of data while maintaining a fluent work experience.

This is why I tued to web workers in JS to run all sorts of database access and data loading in the background. My understanding is, that not only the main UI/main JS remains uninterrupted but also the different web workers run without hindering each other.

I now have the following setup:

mainJS: function statusCheck which runs on pageload:

function statusCheck() {
    if(typeof(w__statusCheck) == "undefined") {
        var w__statusCheck = new Worker("...statusCheck.js");
        w__statusCheck.postMessage("go");
        w__statusCheck.onmessage = function(e) {
            var message = JSON.parse(e.data);
            if(message.text!=undefined) displayMessage(message.text);
        }
}

statusCheck.js which is the worker simply goes like this:

function checkStatus() {
        console.log("statusCheck started");
        // I will leave standard parts out:
            // creating and testing the ajax variable against different browsers

        ajaxRequest.onreadystatechange = function() {
            if(ajaxRequest.readyState == 4) {
                self.postMessage(ajaxRequest.responseText);

                var timer;
                timer = self.setTimeout(function(){
                    checkStatus();
                }, 1000);
            }
        }

        ajaxRequest.open("GET", "...worker_statusCheck.php", true);
        ajaxRequest.send(null);
    }

    this.onmessage = function(e){
        checkStatus();
    };

As you can see, this restarts itself every second (for now). The intervall might be longer in production. worker_statusCheck.php simply gets different things from the database and knits them into a JSON object which gives me the system status.

This works beautifully.

Now I have another worker which is supposed to get initiated by a click on a link to effectively call some php to perform actions:

function loadWorker(url="") {
    console.log("loadWorker started");

    if(url!="") {
        var uniqueID = "XXX" // creating a random ID based on timestamp and Math.random()

    if(typeof(window[uniqueID]) == "undefined") {
        var variables = { ajaxURL: url };
        window[uniqueID] = new Worker("....loadWorker.js");
        window[uniqueID].postMessage(JSON.stringify(variables));
        window[uniqueID].onmessage = function(e) {
            var message = JSON.parse(e.data);
            if(message["success"]!=undefined) {
                variables["close"] = "yes";
                window[uniqueID].postMessage(JSON.stringify(variables));
            }
        }
}

With every click on a certain link this gets called, creates a uniquely named worker, runs it, receives the data and tells the worker to close().

The php again does its thing and writes a progress update in the DB after each step of the lengthy procedure. These progress updates I fetch from the DB with the above repeating statusCheck.

Now, I can see the entries in the DB with timestamp, so I know they get written each at their time. So, both workers do their job and run reliably. But I have noticed, that whenever I initiate the manual (randomly named) worker the statusCheck actually stops performing. It just gets stuck... I was able to confirm this with console output from both workers. So it's not the main JS that seems stuck, but the statusCheck actually pauses... and resumes when loadWorker is done.

Am I missing something fundamental here? Any insight would be appreciated since I'm new to this concept of web workers.

Thanx :)

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: جمعه 1 مرداد 1395 ساعت: 4:08

صفحه بندی