S
S
Sergey Gutovsky2018-06-22 21:10:27
Laravel
Sergey Gutovsky, 2018-06-22 21:10:27

How to handle expiration?

There are models Task(task), Skill(skill) . The task is created by a person, he indicates the due date deadline_at . Skill has a score field (the number of points a skill has). To the task you can through the interim. table to attach skills and indicate how they will change when the task is completed / not completed.
Example
There is a task to pump up The user
indicates that when the skill is completed , the strength will receive 5 points, otherwise the skill will lose 10.
When the user next enters the page, the user sees that the deadline has expired, and the task to pump up has failed, and the strength skill has lost 10 points.
So here's how to do this rear processing. I need to check the database in an unknown way and, if there are tasks that have expired, set the finished_at field to them and update the associated skills.
I have tried doing it through the Task Scheduler . In principle, everything worked: every minute a request was sent to the database, the necessary tasks were found, and both skills and tasks were updated
. But here's the problem. This search happens once a minute. But what if the task expires in the interval between Task Scheduler work , then when entering the task page, the user will see that the deadline has expired, but the task is not marked completed, and the skills associated with it have not lost points.
How to deal with it? And in general, how is the check for expiration implemented in large projects. (Stories inVC , which are removed after 24 hours). After all, the trick is that you need not just to filter the table through where(...) , but you also need to update it if necessary, and all this should happen before the main work starts. project logic

Answer the question

In order to leave comments, you need to log in

3 answer(s)
J
JhaoDa, 2018-06-22
@GutOf

Once every N minutes, collect tasks that will soon be due. Queue their deletion to the minute/second.
PS And fewer tags.

F
FeNUMe, 2018-06-22
@FeNUMe

IMHO, for the task you described, it makes no sense to constantly pull the base and set the flag, instead, in the client part, just check
the current_time > deadline_at
and, based on the result, perform the necessary manipulations.

D
Dmitry Evgrafovich, 2018-06-23
@Tantacula

Learn the queues, when creating a task, add a task at the time of its completion to check whether this particular task has expired (and whether it has been processed yet), plus, implement the advice from FeNUMein case the tasks are completed ahead of time (for this, the first check is needed to see if the task has been completed or not. Ideally, that would be to remove the task from the queue, but Laravel does not allow this at the moment). If you do what you noted as a solution, then when there are no tasks, your resources will idle, taking up processor time (I don’t give a damn about this until you switched to paying for resources, but why get used to the shit code?), And when (if) the service will become huge and there will be too many tasks, it will choke in tasks, the queues will evenly distribute the load and will not allow you to code in an attempt to solve this problem, since they can be parallelized into several threads.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question