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

Write console command to re-calculate counter aggregate value #71

Open
antonkomarev opened this issue Sep 3, 2023 · 7 comments
Open

Comments

@antonkomarev
Copy link
Owner

antonkomarev commented Sep 3, 2023

After fix (by @Brikaa) of counter reset because of the race condition (#66) we need to create console command which will re-calculate correct counter values. It should count lines in storage/{username}-views file and store them to storage/{username}-views-count.

Something like:

cat storage/{username}-views | wc -l | tr -d ' ' > storage/{username}-views-count

But we should iterate thru all the usernames in the storage directory. There are many counter files, so ls -la is hanging for a long time.

One more thing to ensure: we need to make -1 from wc -l, because of the empty line at the end of file.

@antonkomarev
Copy link
Owner Author

It looks like this command can compute correct value:

awk 'END {print NR-1}' storage/{username}-views > storage/{username}-views-count

@Brikaa
Copy link
Contributor

Brikaa commented Sep 3, 2023

I believe wc -l does not count the new line at the end of the file

$ echo -e "hello\nthere" > test.txt && wc -l test.txt
2 test.txt

@antonkomarev
Copy link
Owner Author

Try

echo -e "hello\nthere\n" > test.txt && wc -l test.txt

@Brikaa
Copy link
Contributor

Brikaa commented Sep 3, 2023

That puts two new lines at the end of the file.

@antonkomarev
Copy link
Owner Author

That's exactly how views are stored. We have a blank line on the end of the file.

@Brikaa
Copy link
Contributor

Brikaa commented Sep 3, 2023

Yes, but your example puts two blank lines at the end of the file.
image

@Brikaa
Copy link
Contributor

Brikaa commented Sep 15, 2023

Does the following script using Perl's readdir() for listing the directory content hang? It shouldn't try to read the entire directory content at once.

my $dir = "/app/storage";
opendir(my $dh, $dir) || die "Can't open $dir: $!";
while (my $f = readdir $dh) {
  print "$dir/$f\n";
}
closedir $dh;

Save it as filename.pl and run in the container it using:

perl filename.pl

If it doesn't we can build upon that.

If it hangs, we can consider fixing the user on each HTTP request. We can have a flag that determines if a certain username is fixed, and on each request we check if the flag exists. If it doesn't, we fix the user and create the flag.

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

2 participants