Here are the requirements: (not the real case, just very simplied version)

  1. There is some sort of promotion which lasts for a week.

  2. The selected users who may benefit from the promotion is fixed at the beginning.

  3. Everyday, each selected user gets some bonus(say some credit points) from the system.

I'm thinking about implementing it with state pattern, scheduling some refresh events with a delay of 24 hours. Upon receiving the event, schedule another refresh event with a delay of 24 hours too, until it's finished.

But there is a problem. What if the process (the server) running the program gets shut down then restarted 2 days after? I would like the system to behave correctly by redelivering the promised bonuses of the missed 2 days.

How can I do that?

So, my real question is, ”How do you model something related to theses kind of problems (time related events)?“ What's the nature of this problem? I may have described it but I don't recognize it very well.

Thanks in advance.

up vote 3 down vote accepted

Design the server process in a way it checks from time to time (not necessarily in 24 hours intervals) when the last bonus was given (which you should log in some database, of course, as an attribute of the bonus record). When the difference between the current time and the last assignment time is greater than 24h, assign the next bonus, at least once, but if the time difference is bigger than n x 24h, repeat the assignment n times.

Now, it is not really important any more how often the refresh event is received. 24h is ideal, so the server does not check the bonus time too often or too seldom, but if the event comes after 1 hour or after 50 hours, this will still work correctly. When the server restarts, there could be, for example, an immediate refresh event, so the missing boni will be delivered immediately.

TLDR; use events only for notifications to wake up the server, but do not for time measurements.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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