2

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.

1 Answer 1

4

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 agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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