python3 Auto-logout after certain hour has passed - time

I have server application written in python3. It stores date and time of logins and logouts in sqlite3 database. What I need it to do is to check if a certain hour has passed eg. 23:00 and then write to database logout time eg. 24:00:00 to those users that haven't loged out yet (because at 23:00 it's obvious that they forgot to logout) . I don't really know how to code this.
Edit:OK I'll specify my question maybe I wrote it too general, I don't really want it to write somebody for me just a hint how to start. How is possible trigger function after certain hour has passed.

Depends how this program is being run. If it is only running when the user does something (such as a web app, which serves a request, then terminates), you might need to create another job which is run by say cron (in the unix world) which would fire off at 23:00 and see if there's anyone who's been logged on for 10+ hours but hasn't had recent activity. If on the other hand the server program is constantly running, you could look into something like the threading.Timer class to run a method at a specified time.

Related

How to run command after hours from controller action in laravel?

I need to notify users of incoming end time of created data. Let's say need to notify the users after 3hrs of created data. But I dont want to run cron job every hour because this will slow down the system.
You can queue a command then add a delay to it.
use Illuminate\Support\Facades\Artisan;
Artisan::queue('your:command')->delay(60 * 60 * 3);
I haven't tried to delay a queue for hours. That's why I think a scheduled task is more reliable as you know the time when it runs.
Laravel has a Task Scheduler pretty efficient to work with cron.
You only have to configure it once to run once a minute and Laravel does the rest for checking when it needs to run.
The syntax is pretty simple and you find all available configurations on your codebase.
https://laravel.com/docs/9.x/scheduling
This hardly slows down the system since Laravel only runs the necessary.

Laravel . Cron vs Queue

Hello fellow programmers. I google it before posting! Still confused about .
So here is the idea.
Users are planning post to be posted on their social media (concrete time ).
They can change post time even 2 minutes before . Cron would work as expected if it run every minute ,but it seems old solution. On the other hand queue works different (as far as I get) , trying to make better performance which means if there are high number of requests it will not post !!
Is there anything I am missing???
Thanks in advance
I personally prefere old and reliable concepts over new and unreliable one.
About cron - I did set it to run regularry user script (under the user account) and then user can modify that script how often he wants and cron runs it regularry without need for reloading configuration. And the user script can do anything user can - so it can check if time is larger than some value and some unsent messages are pending and eventually send everything needed. So even if it fails send on scheduled time (maybe server down or anything else), it would be send next time the cron hits (maybe every minute)

joomla scheduled task run every second

how can I set a scheduled task that run every second on Joomla website?
I saw different extensions but they make only every minute, like minimum threshold.
Any idea?
If you do this with a plain extension you need enough traffic to trigger the task every second. Even then this would be unreliable. The best way to do this is using a CRON job directly on your web server. This CRON job can call a CLI script or a URL on your server.
https://en.wikipedia.org/wiki/Cron
Here are some useful pages to generate the necessary task entry:
https://crontab.guru/
http://crontab-generator.org/
If you want to run it every second you might want to check this:
Running a cron every 30 seconds
There are funny solution like using sleep to increase the cron time resolution. In addition there is a tiny script in one of the answers which might help as well.
Using a Joomla extension is not reliable at all, since someone must visit the actual website for the (fake) cron to run. So, this whole cron thing when it's a Joomla extension really depends on the traffic of your website, which makes it very unreliable especially if you are developing a mission critical functionality.
Your best option is to use a Linux cron, which cannot run every second, as the minimum for a cron to run is every minute. Any solution requiring the use of sleep or a for loop is not reliable - especially when you take into consideration that you want to run something every second. All the solutions on the Internet for running a cron every second (or less than a minute) are half baked and completely unreliable. In short, you cannot run a cron every second reliably.
An imaginary solution is to have 60 servers, each server is behind the other by 1 second, and then you run the cron from each of these servers every minute. It is important that all these servers are on the same network to prevent any lag.

Using ruby and sinatra how can I schedule a script to run (at an unpredictable time) in the future?

I am designing a reminder type app. Somebody enters into a form, for example, "call me at such and such a time on such and such a day in the future to remind me of something".
This is put into a postgres database. Obviously lots of people (hopefully) will be doing this and scheduling different things at different times in the future.
So, my question is how can ensure that my app checks the database for things it has to do at the right time? Can I:
a) should I, when the entry is made, create an automated script to execute at the time necessary to perform the reminder function? If so, how?
b) get my app to check the database every minute, again if so how? This would seem a huge waste of resources.
Sorry I cannot provide any code for this but I have no idea where to begin. all help gratefully received.
Thank you!
You might wanna use a background job framework like Sidekiq. Sidekiq supports scheduled jobs like this:
Notifier.perform_at(a_time_object, message_to_send)
It seems like Sidekiq also works with Sinatra.
For that particular task I prefer whenever https://github.com/javan/whenever

What's the best way to fetch a POP3 server for new mails every 15 minutes?

I'm developing an app that needs to fetch a POP3 account every 5-15 minutes to check for new email and process it. I have written all the code except for the part where it automatically runs every 5-15 minutes.
I'm using Sinatra, DataMapper and hosting on Heroku which means cron jobs are out of the question, because Heroku only provides hourly cron jobs at best.
I have looked into Delayed::Job which doesn't natively support Sinatra nor DataMapper but there are workarounds for both. Since my Ruby knowledge is limited I couldn't find a way to merge these two forks into one working Delayed::Job for Sinatra/DataMapper solution.
Initially I used Mailman to check for emails which has built-in polling and runs continuously, but since it's not Rack-based it doesn't run on Heroku.
Any pointers on where to go next? Before you say: a different webhost, I should add I really prefer to stick with Heroku because of its ease of use (except of course, for the above issue).
Heroku supports CloudMailin
A simple trick is to write your code contained in a loop, then sleep at the bottom of it for however long you want:
Untested sample code...
loop do
do_something_way_cool()
sleep 5 * 60 # it's in minutes
end
If it has to be contained in the main body of the app then use a Thread to wrap it so the thread does the work. You'll need to figure out your shared data structures to transfer the data out of the loop. Queue is your friend there.

Resources