Lock app execution until previous process complete

I want make some crontab script, that should prevent another execution attempt, when previous one yet not completed.

In PHP, I've used semaphores, like that:

<?php

$semaphore = sem_get(
    crc32( __DIR__), 1
);

if (false === sem_acquire($semaphore, true)) exit;

// ..

But what about Rust? There are a lot of variants, but which one should I choose, maybe use some crate or std asset contain some methods for these needs?

The task will be synchronous.

Thanks

If you want to use operating system semaphores, like in PHP, there's this: GitHub - Evian-Zhang/named-semaphore: Named semaphore for Linux & Windows.

Edit: and also this: crates.io: Rust Package Registry

1 Like

Thanks for the link, yet not sure my previous implementation using semaphores was really the good solution, but it does its job.

maybe some other solutions to lock the app with std?

Using std only, I guess you could try listening on a port, and use that as a "lock". Some people also use locks on files, but I don't think that's very reliable.

I think using OS semaphores is less bad than the other options.

1 Like

If it doesn't have to be cron, you could also use a systemd timer, and have that ensure only one instance is running: systemd.timer

1 Like

hm.. now I think: do I really want crontab as maybe I can just run the app on background as the daemon and run update operations in the loop using timeout?

just not sure how is better to implement that, maybe like that:

loop {
    thread::sleep(operation_duration); 
    // ..
}

I mean, you don't have to sleep, just queue all incoming operation requests and process them one by one, that fulfills your original question of not having more than one thing running concurrently.

In this case, I want just update some feed once per minute, so the sleep is really wanted here.

By using this solution I don't really want the crontab anymore as can operate the schedule inside the app, lol

Of course that's not an answer to the subject, but I'll use this my solution, by add the application into systemd list. Thanks, not sure about closing, let's maybe select your first reply as the solution.