Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Feature request: flock #6293

Closed
7underlines opened this issue Sep 2, 2020 · 5 comments
Closed

Feature request: flock #6293

7underlines opened this issue Sep 2, 2020 · 5 comments
Labels
Feature Request This issue is made to request a feature.

Comments

@7underlines
Copy link
Contributor

Hi, does v have something like flock from PHP? https://www.php.net/manual/en/function.flock.php
Because I'm looking for a way to prevent double running a long process that is started via cron.
On discord someone mentioned that I could maybe use os.signal, but I'm not so sure how to implement it.

@7underlines 7underlines added the Feature Request This issue is made to request a feature. label Sep 2, 2020
@dumblob
Copy link
Contributor

dumblob commented Sep 5, 2020

  1. your process shall create a temporary directory (because that is on most OSes atomical unlike creating a file)
  2. if it failed (because the directory already existed), quit immediately
  3. otherwise (i.e. the directory was successfully created) create a signal handler for SIGINT QUIT HUP ABRT TERM EXIT which deletes the created temporary directory
  4. add defer{ remove_my_lock_directory() } in main()
  5. nothing else, that's it 😉

@7underlines
Copy link
Contributor Author

7underlines commented Sep 7, 2020

@dumblob Thank you! It's working so far. I've implemented it like this:

fn on_signal() {
	flock_dir := [os.temp_dir(), '.myapp_custom_flock_processing'].join(os.path_separator)
	os.rmdir(flock_dir)
	exit(1)
}

fn main() {
	flock_dir := [os.temp_dir(), '.myapp_custom_flock_processing'].join(os.path_separator)
	os.mkdir(flock_dir) or {exit(0)}

	os.signal(1, on_signal)
	os.signal(2, on_signal)
	os.signal(3, on_signal)
	os.signal(6, on_signal)
	os.signal(15, on_signal)

	// here I do stuff

	os.rmdir(flock_dir)
}

@dumblob
Copy link
Contributor

dumblob commented Sep 7, 2020

@logTom glad it works for you. And oh, I forgot to remove the dir at return from main(). I've edited my suggestion above 😉.

@7underlines
Copy link
Contributor Author

Ah, there is one caveat - this doesn't work if v panics or I stop the program midway with an exit(1) for debugging. I also tried it with defer.

@dumblob
Copy link
Contributor

dumblob commented Sep 8, 2020

Ah, there is one caveat - this doesn't work if v panics or I stop the program midway with an exit(1) for debugging. I also tried it with defer.

I'm glad you shout here about this unwise decision. I'm shouting as well. We'll all cry one day about this IMHO biggest issue of V. Read more about panic() being the most harmful feature in V: #2030 (comment) (IMHO this antifeature has the potential to diminish widespread use of V).

On the positive side, defer{} section shall be called irrespectively of panic() if I'm not mistaken. So you're observing a buggy behavior IMHO.

@medvednikov and others, would you "dare" to contemplate about panic() being more and more harmful? In light of channels being currently implemented into the language this'll become a hot topic some day sooner or later anyway.

@vlang vlang locked and limited conversation to collaborators Sep 22, 2021

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
Feature Request This issue is made to request a feature.
Projects
None yet
Development

No branches or pull requests

3 participants