-
-
Notifications
You must be signed in to change notification settings - Fork 38
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
fget truncate alternative? #104
Comments
For version |
I've been working on getting some test code up and running and this is the farthest I've been able to get so far without any errors. The code runs fine until it gets past $filesystem->detect() in which case there are no errors but no messages ever get relayed to $target_channel. I'm unsure if there's an error within the scope and PHP isn't emitting an error, or if there was a race condition previously where the file is being wiped faster than the contents could be emitted because $file was being updated in the next step before the each loop resolved, or if there's something else entirely that I'm missing.
|
Try this: function chat_relay($filesystem, $guild, string $file_path, string $channel_id)
{
if ($target_channel = $guild->channels->offsetGet($channel_id)) {
$filesystem->detect($file_path)->then(function (\React\Filesystem\Node\FileInterface $file) {
return $file->getContents()->then(function (string $contents) use ($file, $target_channel) {
$contents = explode('\n', $contents);
foreach ($contents as $line) {
$target_channel->sendMessage($line);
}
})->then(function () use ($file) {
$file->putContents('');
});
})->done();
}
} If you're 100% sure function chat_relay($filesystem, $guild, string $file_path, string $channel_id)
{
if ($target_channel = $guild->channels->offsetGet($channel_id)) {
$file = $filesystem->file($file_path);
$file->getContents()->then(function (string $contents) use ($file, $target_channel) {
$contents = explode('\n', $contents);
foreach ($contents as $line) {
$target_channel->sendMessage($line);
}
})->then(function () use ($file) {
$file->putContents('');
})->done();
}
} |
I've been having a lot of trouble with this function since trying to implement it. Our host reports that he's able to start up his bot with this function in it, but that it is either never called or is failing silently. The original code works fine with the hard-coded file path. He has reverted back to the original in the mean time until we can get more reliable logging. |
That is rather crucial information to know. Other bits of information are hostos, PHP version, with event loop extension you are using. |
We got logging and figured out the issue, it was on another line due to a typo that was made at the time of implementation. |
It turns out that when I thought it was working our host had thrown up some old code that was prior to implementing filesystem, and it is indeed not working. I have some logs this time and additional code showing that no errors are thrown and yet no file contents ever appear to get passed through, nor does the file ever get truncated. I have also checked the exact file paths using Logs:
Test code: $ooc_relay = function ($guild, string $file_path, string $channel_id) use ($filesystem)
{
/*
if ($file = fopen($file_path, "r+")) {
while (($fp = fgets($file, 4096)) !== false) {
$fp = str_replace(PHP_EOL, "", $fp);
if ($target_channel = $guild->channels->offsetGet($channel_id)) $target_channel->sendMessage($fp);
else echo "[RELAY] Unable to find channel $target_channel" . PHP_EOL;
}
ftruncate($file, 0); //clear the file
fclose($file);
} else echo "[RELAY] Unable to open $file_path" . PHP_EOL;
*/
echo '[RELAY - PATH] ' . $file_path . PHP_EOL;
if ($target_channel = $guild->channels->offsetGet($channel_id)) {
if ($file = $filesystem->file($file_path)) {
$file->getContents()->then(function (string $contents) use ($file, $target_channel) {
$promise = React\Async\async(function () use ($contents, $file, $target_channel) {
if ($contents) echo '[RELAY - CONTENTS] ' . $contents . PHP_EOL;
$lines = explode(PHP_EOL, $contents);
$promise2 = React\Async\async(function () use ($lines, $target_channel) {
foreach ($lines as $line) {
if ($line) {
echo '[RELAY - LINE] ' . $line . PHP_EOL;
$target_channel->sendMessage($line);
}
}
return;
})();
React\Async\await($promise2);
})();
$promise->then(function () use ($file) {
echo '[RELAY - TRUNCATE]' . PHP_EOL;
$file->putContents('');
}, function (Exception $e) {
echo '[RELAY - ERROR] ' . $e->getMessage() . PHP_EOL;
});
React\Async\await($promise);
})->done();
} else echo "[RELAY - ERROR] Unable to open $file_path" . PHP_EOL;
} else echo "[RELAY - ERROR] Unable to get channel $channel_id" . PHP_EOL;
}; I've also tried your originally posted code and a slight variation of it, but the results are the same. if ($target_channel = $guild->channels->offsetGet($channel_id)) {
if ($file = $filesystem->file($file_path)) {
$file->getContents()->then(function (string $contents) use ($file, $target_channel) {
var_dump($contents);
$contents = explode(PHP_EOL, $contents);
foreach ($contents as $line) {
$target_channel->sendMessage($line);
}
})->then(
function () use ($file) {
$file->putContents('');
}, function (Exception $e) {
echo '[RELAY - getContents Error] ' . $e->getMessage() . PHP_EOL;
}
)->done();
} else echo "[RELAY - ERROR] Unable to open $file_path" . PHP_EOL;
} else echo "[RELAY - ERROR] Unable to get channel $channel_id" . PHP_EOL; |
I'm hoping to port some of my existing code over to ReactPHP Filesystem to make reading lines from a file and truncating it afterwards asynchronous so that my main loop can still be working on other time sensitive tasks, like listening for events fired over a socket. The current documentation doesn't really give me a good idea of how to do this, and only some of the snippets seem to be relevant to my usage. What would be filesystem's equivalent to this?
The text was updated successfully, but these errors were encountered: