我应该如何实现数据库 cron 作业记录器?
我使用 PHP 和 Oracle,并使用 crontab 在预定时间执行 PHP 脚本。我当前的日志记录/审计解决方案涉及简单的日志文件。我想将 cron 执行日志保存到数据库中。
现在我正在尝试设计流程,以便当 cron 作业启动时我会在 CronExecution
表中创建一条记录。然后,每次我想为该 cron 记录一些内容时,我都会在 CronEvent
表中放入一条记录,该表将有一个指向 CronExecution
表的外键。
我计划使用 PRAGMA AUTONOMOUS
pl/sql 过程记录所有事件。通过此过程,我将能够以一致的方式记录其他 pl/sql 过程内部的事件以及我的 PHP 代码中的事件。
为了使其更加健壮,我的计划是在数据库日志调用失败时将错误记录到文件中。
还有人写过类似的东西吗?根据您的经验,您有什么建议?
I use PHP and Oracle, with crontab executing the PHP scripts at scheduled times. My current logging/auditing solution involves simple log files. I'd like to save my cron execution logs to a database instead.
Right now I'm trying to design the process so that when a cron job starts I create a record in an CronExecution
table. Then every time I want to log something for that cron I'll put a record in a CronEvent
table which will have a foreign key to the CronExecution
table.
I plan to log all events using a PRAGMA AUTONOMOUS
pl/sql procedure. With this procedure I will be able to log events inside of other pl/sql procedures and also from my PHP code in a consistent manner.
To make it more robust, my plan is to have a fallback to log errors to a file in the event that the database log calls fail.
Has anybody else written something similar? What suggestions do you have based on your experience?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,我已经这样做过好几次了。
CronExecution
表是一个好主意。但是,我不知道您是否真的需要创建CronEvent
表。相反,只需给自己一个“状态”列并更新该列即可。您会发现这使得故障转移到文件变得更加容易。当您构建大量此类
CronExecutions
时,您可能会对CronEvents
兴趣减弱,而对执行的完整状态更感兴趣。将所有更新调用包装在存储过程中。你肯定是正确的。
在您的
CronExecution
中包含一个“计划”将会很方便。很容易有很多 cron 作业,但无法将“这需要多长时间?”和“这应该什么时候运行”之间的点联系起来。 。在完成工作时添加“下一次计划运行”将使您的生活变得更加轻松。Yep, I've done this several times.
The
CronExecution
table is a good idea. However, I don't know that you really need to create theCronEvent
table. Instead, just give yourself a "status" column and update that column.You'll find that makes failing over to file much easier. As you build lots of these
CronExecutions
, you'll probably have less interest inCronEvents
and more interest in the full status of the execution.Wrap all of the update calls in stored procedures. You definitely have that correct.
Including a 'schedule' in your
CronExecution
will prove handy. It's very easy to have a lot of cron jobs and not be able to connect the dots on "how long did this take?" and "when is this supposed to run". Including a "next scheduled run" on completion of a job will make your life much easier.您将需要阅读有关 DBMS_SCHEDULER 的文档。
实施起来需要一些工作(什么不是?),但它允许您从数据库内控制计划的一次性作业。这包括操作系统级别的工作。
You will want to read the documentation on DBMS_SCHEDULER.
It's a little bit of work to implement (what isn't?), but it will allow you to control scheduled and one-time jobs from within the database. That includes OS-level jobs.