执行一个Lisp函数在将来执行一次。

huangapple go评论129阅读模式
英文:

Execute a lisp function in the future ONE time

问题

以下是您要翻译的内容:

我有一个API的POST路由,从用户那里接收一些信息。

该API是用Common Lisp构建的。当提交表单时,我想要“等待” 24 小时才执行一个函数。

因此,一种执行计划程序。

我查看了 cl-cron,但我只希望它在我可以手动指定的可变日期执行一次。

如何为长时间运行的Lisp镜像创建这个?

现在代码的样子

;; 注意 - defroute 是 easy-routes 库的一部分

(defroute post-route ("api/post/route" :method :post) (&post name email)

    (validation-function ...)
    (save-to-db ....) 
    (do-something ...) ;; <-- 这 3 个已实现

    (schedule-future-action firstname email seconds-in-future) ;; <-- 未实现 

    (redirect ...) <-- 已实现

)
英文:

I have an API post route that takes in some information from the user.

The API is built with Common Lisp. When the form is submitted, I would like to "wait" for 24 hours to execute a function.

So, a sort of scheduler for execution.

I looked at cl-cron, but I only want this to execute one time at a variable date that i can specify manually.

How can I create this for a long running lisp image?

What the code looks like now

;; note - defroute is part of easy-routes library

(defroute post-route (&quot;api/post/route&quot; :method :post) (&amp;post name email)

    (validation-function ...)
    (save-to-db ....) 
    (do-something ...) ;; &lt; -- these 3 are implemented

    (schedule-future-action firstname email seconds-in-future) ;; &lt;--- not implemented 


    (redirect ...) &lt; -- implemented

)


答案1

得分: 2

我基于coredump的评论创建了一个解决方案。

我构建了一个基于优先级队列的数据结构,用于记录事件。队列保存在本地文件系统中,按事件的时间戳排序(这取决于用户操作)。

当控制器激活时,操作被保存并排序到队列中。

最后,第二个线程运行cl-cron。它每隔1分钟运行一次函数。它读取优先级队列中的第一个项目,如果准备好执行并删除项目,如果不准备好则不执行。

重要的部分是,正如coredump建议的那样,如果Lisp图像因任何原因崩溃,cron作业可以继续之前的工作。

英文:

I created a solution based on coredump's comment.

I built a priority queue data structure that keeps a record of events. The queue is saved on the local file system. It is sorted by the time stamp of the event (which depends on user action).

When the controller activates, actions get saved and sorted into the queue.

Finally, a second thread, runs cl-cron. Which runs a function every 1 minute. It reads the first item in the priority queue, if ready executes and delete item. if not do nothing.

The important part is, as coredump suggested, if the lisp image crashes for whatever reason, the cron job can continue where it left off.

huangapple
  • 本文由 发表于 2023年6月8日 19:26:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431376.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定