I'm needing to trigger an action at a particular date/time either in python or by another service.
Let's say I have built an application that stores the expiry dates of memberships in a database. I'm needing to trigger a number of actions when the member expires (for example, changing the status of the membership and sending an expiry email to the member), which is fine - I can deal with the actions.
However, what I am having trouble with is how do I get these actions to trigger when the expiry date is reached? Are there any concepts or best practices that I should stick to when doing this?
Currently, I've achieved this by executing a Google Cloud Function every day (via Google Cloud Scheduler) which checks if the membership expiry is equal to today, and completes the action if it is. I feel like this solution is quite 'hacky'.
I'm not sure which database you are using but I'm inferring you have a table that have the "membership" details of all your users. And each day you run a Cron job that queries this table to see which row has "expiration_date = today", is that correct?.
I believe that's an efficient way to do it (it will be faster if you have few columns on that table).
Related
I am building a Python script which retrieves a set of information for all Slack users of the organization that I work. Currently, I was asked by the stakeholders to retrieve the last time that our organization's users were active on Slack. I came across the users.getPresence method but this can only return a timestamp for the owner of the token. Has anybody worked on that before? Thanks.
It depends a bit how you define "active on Slack".
If getting the date and time a user was logged in last is enough, take a look at team.accessLogs, which will give you the timestamp of the last login for every user (date_last).
This will not reflect whether the user performed any activity, e.g. posting a message or uploading a file though.
However, note that this API method works for paid plans only.
I'm looking for a way to keep track of users that are online/offline. So if I present all users in a list i could have an icon or some kind of flag to show this. Is this built in in Django's default Auth system?
My first thought was to simply have a field in my profiles called last_logout in the models and update it with the date/time each time user logged out.
With this info and the built in last_login I should be able to make some kind of function to determine if the user is loggedin/online right?
Or should I just have a boolean field called "online" that I can change when user logs in and out?
With only django it will be hard to do. For such task async frameworks are more suitable.
For example, tornado.
Users won't do logout explicitly every time they go offline. They just close their browser and that's it. You can't know it with only django auth app. It is not designed for such tasks.
Even if you will check for not expired session, it not gives you all online users, because session can be non-expired for 30 days.
So to get real online users, possible solutions are:
Every user will send some data via javascript to your server, for example every 10 seconds. You can fetch that data on server and put user into cache and set cache key to be alive for 10 seconds. So when you need to know, who are online now, you'll check your cache. But it is not a good solution, because it will need a lot of server resources.
Use async framework (tornado) at server side (you can setup separate process for exact requests). And use websockets (SockJS is a good library for that at client side). It is a more complicated solution, but it is better.
You have to consider what exactly means for the users to be "online". Since any user can close the browser window any time and without the server knowing about that action, you'd end up having lots of false "online" users.
You have two basic options:
Keep track of the user's last activity time. Every time the user loads a page you'd update the value of the timer. To get a list of all online users you'd need to select the ones with an activity before X minutes. This is what is done by some web forums.
Open a websocket, long polling connection or some heartbeat to the server. This is what Facebook chat does. You'd need more than just django, since to keep a connection open another kind of server-side resources are needed.
I am building an app in google app engine. I am storing entities of a kind with multiple properties and one of them is date and time property.
I need to check whether that date and time property exceeds or "expires" by comparing with the current date and time.
I do not prefer polling.
I am thinking about writing a daemon which continuously scans through all of the entities to check whether it is expired or not.
The point is, i need to send push notification if one of the entity is "Expired."
I may be totally wrong about this concept. Please suggest a better way.
There are two approaches that you might consider:
Run a cron job which will periodically query the Datastore for expired entities and send notifications.
When you add an entity, create a task to notify a user when it expires. You can set an ETA on a task. This option is only available if your entities expire in less than 30 days.
I want to implement a reminder system, where a reminder email will be sent to the users based on some date field in their userprofile.How can I implement this ? Many suggested me not to use celery. Told me to use cron tab.
This is how I do, is this good way? If not, please suggest me a good way.
If cron:
Planning to run a python management command daily, that will check if there is some reminder for that day, by checking dates of all the users.
I dont think this is good, I think, whenever the user select their reminder date, then that date will be registered some how and job will be run only on that date, but I am not getting how to implement this.
Way I know to implement this:
Create a database table named 'reminderdates', and whenever a reminder is registered, add that date and user_id to that table. So, run a cron job everyday that checks if that day is among the dates in the table. If yes job will be run. Also, I will run another cron job that will delete all the older reminderdates from the table so the table size wont get increased with time.
The simplest solution is to setup a cron job to hit a URL on the site that performs the task. I would recommend Celery though. Here's why (from Django/Celery Quickstart):
Cron has the advantage of simplicity, but it's not not ideal for the job. You have to take steps to ensure that regular users of the site cannot hit those URLs directly. It also forces you to manage an external configuration. What if you forget to perform the configuration on the qa or production servers? It would be safer and easier if the configuration was in the code for the site.
You will find a tutorial on how to implement tasks with celery on that previous link.
As for your reminderdates table idea, it should work. You can simplify it a little bit if you try this:
Adding a column with a sent status. Your task will check for date and sent status. If current day matches reminder date AND status is "unsent", then send the reminder.
You don't need a secondary cron job to delete older reminders. The same task that sends the reminders can send each one and delete it after (in which case the sent/unsent status would not be needed).
I work on a page in Django, where users can set custom reminders for different dates (max. 3 per date). The reminders should send via e-mail. Its similar to Google Calendar, where you can set multiple reminders for each event in x-minutes, x-hour or x-days before the date starts.
I wonder, how I can solve it combined with Django. Since there will be a lot of users and dates, which should of course also run perfomant.
Should I do this with a cron job? Is there a python way?
The other traditional way is to use django-celery: http://pypi.python.org/pypi/django-celery/
You can use the celerybeat command to run periodical tasks. Also you can start pending tasks from a django view.
You can use a cron job. To create a management command: refer to the documentation here
Also, you can create the email generation as a queue based, distributed implementation for enhanced performance. You can use Django-mailer app for the same.