Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debugging errors #55

Closed
sayanb opened this issue Jan 10, 2019 · 10 comments
Closed

Debugging errors #55

sayanb opened this issue Jan 10, 2019 · 10 comments

Comments

@sayanb
Copy link

sayanb commented Jan 10, 2019

My code:

        $pool = Pool::create();

    	foreach ($xmlNode->childNodes as $item) {
            $pool[] = async(function () use ($item) {
                     //do stuff
                      ...
                     //do more stuff
                     ...
                }
            })->then(function ($output) {
                //handle success
            })->catch(function ($ex) {
                echo PHP_EOL . "something happened, exception. Error: ".$ex->getMessage();
            })->timeout(function () {
                echo PHP_EOL . "timed out";
            });
    	}
        await($pool);

Output:

something happened, exception. Error:
something happened, exception. Error:
something happened, exception. Error:
something happened, exception. Error:
something happened, exception. Error:
something happened, exception. Error:
something happened, exception. Error:
something happened, exception. Error:
something happened, exception. Error:
something happened, exception. Error:

How do I debug the issue? The exception message doesn't seem to be showing. If I just log the exception, instead of the message, I get:

something happened, exception. Error: Spatie\Async\Output\ParallelError in /home/sayan/Documents/Programming/spatie_test/vendor/spatie/async/src/Output/ParallelError.php:11
Stack trace:
#0 /home/sayan/Documents/Programming/spatie_test/vendor/spatie/async/src/Process/ParallelProcess.php(126): Spatie\Async\Output\ParallelError::fromException('')
#1 /home/sayan/Documents/Programming/spatie_test/vendor/spatie/async/src/Process/ProcessCallbacks.php(51): Spatie\Async\Process\ParallelProcess->resolveErrorOutput()
#2 /home/sayan/Documents/Programming/spatie_test/vendor/spatie/async/src/Pool.php(197): Spatie\Async\Process\ParallelProcess->triggerError()
#3 /home/sayan/Documents/Programming/spatie_test/vendor/spatie/async/src/Pool.php(285): Spatie\Async\Pool->markAsFailed(Object(Spatie\Async\Process\ParallelProcess))
#4 /home/sayan/Documents/Programming/spatie_test/vendor/spatie/async/src/Runtime/ParentRuntime.php(70): Spatie\Async\Pool->Spatie\Async{closure}(17, Array)
#5 /home/sayan/Documents/Programming/spatie_test/vendor/spatie/async/src/helpers.php(15): Spatie\Async\Runtime\ParentRuntime::createProcess(Object(Closure))
#6 /home/sayan/Documents/Programming/spatie_test/test.php(84): async(Object(Closure))
#7 /home/sayan/Documents/Programming/spatie_test/test.php(50): Test->getListings(Object(DOMElement))
#8 /home/sayan/Documents/Programming/spatie_test/test.php(221): Test->process()
#9 {main}

@brendt
Copy link
Contributor

brendt commented Jan 10, 2019

Difficult to say what to do in this specific case, but I'd like to help you debug this further.

The starting point would be https://github.com/spatie/async/blob/master/src/Process/ParallelProcess.php#L117

When a child processes encounters an error, it will serialize it and pass it to the parent process. This is where that is handled. I'd start by inspecting the output from getErrorOutput, to see what's passed from the child process to the parent.

If there's no useful information in the output, we'll have to look in the child process itself. Take a look at https://github.com/spatie/async/blob/master/src/Runtime/ChildRuntime.php. The try/catch is what's passing the exception to the parent process. There's two ways of debugging the child process:

either run the child process on its own, or write the output from within the child process to a file. The latter is often the more easy one. You could for example write the message of the caught exception to a log file.

@brendt
Copy link
Contributor

brendt commented Jan 10, 2019

Please let me know when you found what happened, if possible I'd like this package to handle all errors in a debugable way.

@sayanb
Copy link
Author

sayanb commented Jan 10, 2019

Thanks @brendt . In my code example, where should I look for getErrorOutput()? I see that it's a method in ParallelProcess class, whereas the exception class is ParallelError. .

@brendt
Copy link
Contributor

brendt commented Jan 10, 2019

Sorry if I didn't make myself clear: it's possible that there's in issue within this package with error handling, and these debug tips should be done in the vendor files.

Another option is to fork this repo locally and link it via your composer file.

@sayanb
Copy link
Author

sayanb commented Jan 11, 2019

I decided to implement this in a CodeIgniter 3 project, and now I am getting some warnings.

The full code now:

<?php 
use Spatie\Async\Pool;
use Spatie\Async\Process;

class Paralleltest extends MY_Controller {

    public function spatie() {
    	$cities = array(
    		array('sydney', 'melbourne', 'brisbane'),
    		array('san francisco', 'boston', 'detroit'),
    		array('london, manchester, bristol'),
    		array('beijing, shanghai, shenzhen'),
    		array('kolkata', 'mumbai', 'delhi')
    	);

    	$start = time();
    	$pool = Pool::create();

    	echo PHP_EOL . "is parallelism supported? " . Pool::isSupported();

		foreach ($cities as $key => $city) {
			echo PHP_EOL . "sending city list ".json_encode($city)." to child, which will capitalise them and sleep for 5 seconds";
		    
		    $pool[] = async(function () use ($city) {
		        foreach ($city as $cityKey => $cityVal) {
		        	$upperCity = $this->capitalize($cityVal);
		        	$city[$cityKey] = $upperCity;
		        }
		        
		        sleep(5);
		        return json_encode($city);
		    })->then(function ($output) {
		        echo PHP_EOL . "back in main thread, got $output from child thread";
		    })->catch(function ($exception) {
        		echo PHP_EOL . "something happened. Error: " . $exception->getMessage();
    		});
		}

		await($pool);
		$end = time();
		echo PHP_EOL . "goodbye from parent. Time taken: " . ($end - $start) . PHP_EOL;
    }

    private function capitalize($cityVal) {
    	return strtoupper($cityVal);
    }
    
}

The logs are

is parallelism supported? 1
sending city list ["sydney","melbourne","brisbane"] to child, which will capitalise them and sleep for 5 seconds
sending city list ["san francisco","boston","detroit"] to child, which will capitalise them and sleep for 5 seconds
sending city list ["london, manchester, bristol"] to child, which will capitalise them and sleep for 5 seconds
sending city list ["beijing, shanghai, shenzhen"] to child, which will capitalise them and sleep for 5 seconds
sending city list ["kolkata","mumbai","delhi"] to child, which will capitalise them and sleep for 5 seconds
something happened. Error: PHP Warning: Class 'Paralleltest' not found in /home/sayan/Documents/Programming/testing/vendor/opis/closure/src/SerializableClosure.php on line 232
PHP Warning: call_user_func() expects parameter 1 to be a valid callback, no array or string given in /home/sayan/Documents/Programming/testing/vendor/spatie/async/src/Runtime/ChildRuntime.php on line 25

something happened. Error: PHP Warning: Class 'Paralleltest' not found in /home/sayan/Documents/Programming/testing/vendor/opis/closure/src/SerializableClosure.php on line 232
PHP Warning: call_user_func() expects parameter 1 to be a valid callback, no array or string given in /home/sayan/Documents/Programming/testing/vendor/spatie/async/src/Runtime/ChildRuntime.php on line 25

something happened. Error: PHP Warning: Class 'Paralleltest' not found in /home/sayan/Documents/Programming/testing/vendor/opis/closure/src/SerializableClosure.php on line 232
PHP Warning: call_user_func() expects parameter 1 to be a valid callback, no array or string given in /home/sayan/Documents/Programming/testing/vendor/spatie/async/src/Runtime/ChildRuntime.php on line 25

something happened. Error: PHP Warning: Class 'Paralleltest' not found in /home/sayan/Documents/Programming/testing/vendor/opis/closure/src/SerializableClosure.php on line 232
PHP Warning: call_user_func() expects parameter 1 to be a valid callback, no array or string given in /home/sayan/Documents/Programming/testing/vendor/spatie/async/src/Runtime/ChildRuntime.php on line 25

something happened. Error: PHP Warning: Class 'Paralleltest' not found in /home/sayan/Documents/Programming/testing/vendor/opis/closure/src/SerializableClosure.php on line 232
PHP Warning: call_user_func() expects parameter 1 to be a valid callback, no array or string given in /home/sayan/Documents/Programming/testing/vendor/spatie/async/src/Runtime/ChildRuntime.php on line 25

goodbye from parent. Time taken: 0

@sayanb
Copy link
Author

sayanb commented Jan 11, 2019

Note, if I replace

$upperCity = $this->capitalize($cityVal);

with

$upperCity = strtoupper($cityVal);
It works, so the issue seems to be in the child failing to create the object somehow?

@brendt
Copy link
Contributor

brendt commented Jan 11, 2019

Yeah, you cannot simply reference the $this context from within a child process. Remember that it's a completely new PHP process, and only the function passed in the async function will be executed there.

Take a look at tasks if you want to better structure your code: https://github.com/spatie/async#working-with-tasks

I'm going to close this issue for now, as there doesn't seem to be a problem in the package. But feel free to keep commenting.

@brendt brendt closed this as completed Jan 11, 2019
@sayanb
Copy link
Author

sayanb commented Jan 11, 2019

Thank you. If the README.md could be updated with a simple example of this library working in an object-oriented setup (similar to the async/await examples, with actual code), it would be really helpful! I had a look at the Tasks link, but the usage wasn't exactly clear to me.

@developeronboard
Copy link

yes sayanb real case examples are not found..did you found any codes related to this??if so kindly reply me.

@iprastha
Copy link

Thank you. If the README.md could be updated with a simple example of this library working in an object-oriented setup (similar to the async/await examples, with actual code), it would be really helpful! I had a look at the Tasks link, but the usage wasn't exactly clear to me.

I'm in the same position, with codeigniter 3 project as well. Did you find any solution to this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants