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

File locking? #484

Open
dbohdan opened this issue Feb 7, 2023 · 3 comments
Open

File locking? #484

dbohdan opened this issue Feb 7, 2023 · 3 comments

Comments

@dbohdan
Copy link
Contributor

dbohdan commented Feb 7, 2023

Is there a way to lock a file on Linux and *BSD from Joker? I could not find one. My goal is to ensure only one instance of a script at a time can modify a file. Is there a way to use Bolt as a locking mechanism? I see the option of shelling out to flock(1) or wrapping my Joker script in a shell script that runs it with flock, but a native solution would be more convenient and elegant.

P.S.: Thanks for making Joker! I run it on NetBSD on SDF. It builds and runs on NetBSD without issue.

@candid82
Copy link
Owner

candid82 commented Feb 8, 2023

Currently there is no way to do it. Looks like Go provides syscall.Flock function that could be wrapped and exposed in Joker, but syscall package is OS-specific and I'd like to avoid OS-specific APIs.
I think you can use Bolt for locking if you don't mind creating a temporary file. E.g.

(def db (joker.bolt/open "temp.db" 0600)) ;; First process will acquire a lock, subsequent processes will block until the lock is release with (joker.bolt/close db) call

;; Do stuff

(joker.bolt/close db) ;; This releases the lock.

@candid82
Copy link
Owner

candid82 commented Feb 8, 2023

I run it on NetBSD on SDF

Just read about SDF. This is really cool!

@dbohdan
Copy link
Contributor Author

dbohdan commented Feb 8, 2023

Avoiding non-portable APIs makes sense. I'll try Bolt; it likely fits my use case. It looks like Bolt implements its own cross-platform abstraction over flock (Windows implementation). There are Go libraries that provide a similar abstraction, but one would have to research their quality and pick one (e.g., gofrs/flock#41 may be a problem with the library and not the OS API).

I'll leave the issue open: other users could be interested, and I might do the research and PR the feature for you to review at some point.

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