1

The need for the client user is to perform tasks while they aren't currently logged in (so we can assume it is time-based). For example: A user logs in, performs tasks, logs out and gets a 'reward' in 6 hours or so. How would time be kept in this situation as the client isn't logged in?

I have the option for a server and a (persistent) database. A possible solution I thought of was saving the previous user login time and when the next time they log in, it would check if 6 hours had passed and update the database from there.

But, the issue of tasks, users, and hardware rises when I need to update every user's information differently every minute.

Is there a smarter way to design this?

3

It depends on what the exact requirements are.

Retroactive Update You already came up with the idea of simply updating the user's data when he logs in the next time.

This is a simple and robust solution, and I would recommend it if it fulfills your requirements. However, it doesn't work if the update has consequences for other users. For example, if you're maintaining a leadership board, you don't want the user's score to update only when they log back in.

Proactive Update The alternative in this scenario is to store the reward immediately, but have a ActivationTime field that you set to six hours from now.

Whenever you need the current state of the system, you just query all rewards that have already been activated. This solution is almost as simple and just as robust. However, it requires you to know in advance when a reward should be given. If the reward depends on multiple other things that may or may not happen in the future, this will become more complicated - though may still work quite well.

Scheduled Update Alternatively, you can implement a scheduled update mechanism, as suggested by Pedro Tavares.

This has the advantage that you can make things happen just as they are supposed to happen conceptually. For example,

Wait 6 hours
Check something
Give reward

However, you need some way to recover from a system failure during those 6 hours. That essentially means you have to persist scheduled tasks or find a way to recreate them when the system comes back online.

0

I would suggest using some sort of Scheduler to schedule a periodic task that runs in background x hours after a given user logged out from your system.

By using this approach you don't have to check if x hours has passed from the previous log out in order to calculate the reward, everytime a user is logging into your system.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.