Timer tasks are common in WEB applications. How to use PHP to implement timer tasks is roughly as follows Two options: 1) Use the Crontab command to write a shell script, call the PHP file in the script, and then execute the script regularly; 2) Use ignore_user_abort() and set_time_limit() to run the script without the browser. The former uses the characteristics of Linux and has little to do with PHP itself. The latter has limited usage scenarios and can only trigger the script with one HTTP request and exit after execution. So how do we use pure PHP to implement pure timer tasks and adapt to the business needs of cognitive tasks?
Basic knowledge
This program is developed under Linux and runs in cli mode. Here is a brief introduction to the basic knowledge.
Principle of implementation
Use a three-dimensional array to save all tasks that need to be executed. The first-level index is the timestamp, and the value is the method of executing the task, callback parameters, etc. The specific array form is as follows:
<span>array</span><span>( </span>'1438156396' => <span>array</span><span>( </span><span>array</span>(1,<span>array</span>('Class','Func'), <span>array</span>(), <span>true</span>),<span> ) ) 说明</span>: 1438156396<span> 时间戳 </span><span>array</span>(1,<span>array</span>('Class','Func'), <span>array</span>(), <span>true</span><span>) 参数依次表示</span>: 执行时间间隔,回调函数,传递给回调函数的参数,是否持久化(ture则一直保存在数据中,否则执行一次后删除)
These tasks can be methods of any class. Since it is a scheduled task, we need something similar to timing. This solution uses a semaphore to send the SIGALRM signal to the current process every second, capture the signal, trigger the signal processing function, loop through the data, and determine whether there is a current time required to perform the task. If so, use a callback to trigger it and pass the parameters to the method.
<span> 1</span> <?<span>php </span><span> 2</span> <span>/*</span><span>* </span><span> 3</span> <span>*定时器 </span><span> 4</span> <span>*/</span> <span> 5</span> <span>class</span><span> Timer </span><span> 6</span> <span>{ </span><span> 7</span> <span>//</span><span>保存所有定时任务</span> <span> 8</span> <span>public</span> <span>static</span> <span>$task</span> = <span>array</span><span>(); </span><span> 9</span> <span> 10</span> <span>//</span><span>定时间隔</span> <span> 11</span> <span>public</span> <span>static</span> <span>$time</span> = 1<span>; </span><span> 12</span> <span> 13</span> <span>/*</span><span>* </span><span> 14</span> <span> *开启服务 </span><span> 15</span> <span> *@param $time int </span><span> 16</span> <span>*/</span> <span> 17</span> <span>public</span> <span>static</span> <span>function</span> run(<span>$time</span> = <span>null</span><span>) </span><span> 18</span> <span> { </span><span> 19</span> <span>if</span>(<span>$time</span><span>) </span><span> 20</span> <span> { </span><span> 21</span> self::<span>$time</span> = <span>$time</span><span>; </span><span> 22</span> <span> } </span><span> 23</span> self::<span>installHandler(); </span><span> 24</span> pcntl_alarm(1<span>); </span><span> 25</span> <span> } </span><span> 26</span> <span>/*</span><span>* </span><span> 27</span> <span> *注册信号处理函数 </span><span> 28</span> <span>*/</span> <span> 29</span> <span>public</span> <span>static</span> <span>function</span><span> installHandler() </span><span> 30</span> <span> { </span><span> 31</span> pcntl_signal(SIGALRM, <span>array</span>('Timer','signalHandler'<span>)); </span><span> 32</span> <span> } </span><span> 33</span> <span> 34</span> <span>/*</span><span>* </span><span> 35</span> <span> *信号处理函数 </span><span> 36</span> <span>*/</span> <span> 37</span> <span>public</span> <span>static</span> <span>function</span><span> signalHandler() </span><span> 38</span> <span> { </span><span> 39</span> self::<span>task(); </span><span> 40</span> <span>//</span><span>一次信号事件执行完成后,再触发下一次</span> <span> 41</span> pcntl_alarm(self::<span>$time</span><span>); </span><span> 42</span> <span> } </span><span> 43</span> <span> 44</span> <span>/*</span><span>* </span><span> 45</span> <span> *执行回调 </span><span> 46</span> <span>*/</span> <span> 47</span> <span>public</span> <span>static</span> <span>function</span><span> task() </span><span> 48</span> <span> { </span><span> 49</span> <span>if</span>(<span>empty</span>(self::<span>$task</span><span>)) </span><span> 50</span> {<span>//</span><span>没有任务,返回</span> <span> 51</span> <span>return</span><span> ; </span><span> 52</span> <span> } </span><span> 53</span> <span>foreach</span>(self::<span>$task</span> <span>as</span> <span>$time</span> => <span>$arr</span><span>) </span><span> 54</span> <span> { </span><span> 55</span> <span>$current</span> = <span>time</span><span>(); </span><span> 56</span> <span> 57</span> <span>foreach</span>(<span>$arr</span> <span>as</span> <span>$k</span> => <span>$job</span><span>) </span><span> 58</span> {<span>//</span><span>遍历每一个任务</span> <span> 59</span> <span>$func</span> = <span>$job</span>['func']; <span>/*</span><span>回调函数</span><span>*/</span> <span> 60</span> <span>$argv</span> = <span>$job</span>['argv']; <span>/*</span><span>回调函数参数</span><span>*/</span> <span> 61</span> <span>$interval</span> = <span>$job</span>['interval']; <span>/*</span><span>时间间隔</span><span>*/</span> <span> 62</span> <span>$persist</span> = <span>$job</span>['persist']; <span>/*</span><span>持久化</span><span>*/</span> <span> 63</span> <span> 64</span> <span>if</span>(<span>$current</span> == <span>$time</span><span>) </span><span> 65</span> {<span>//</span><span>当前时间有执行任务 </span><span> 66</span> <span> 67</span> <span> //调用回调函数,并传递参数</span> <span> 68</span> <span>call_user_func_array</span>(<span>$func</span>, <span>$argv</span><span>); </span><span> 69</span> <span> 70</span> <span>//</span><span>删除该任务</span> <span> 71</span> <span>unset</span>(self::<span>$task</span>[<span>$time</span>][<span>$k</span><span>]); </span><span> 72</span> <span> } </span><span> 73</span> <span>if</span>(<span>$persist</span><span>) </span><span> 74</span> {<span>//</span><span>如果做持久化,则写入数组,等待下次唤醒</span> <span> 75</span> self::<span>$task</span>[<span>$current</span>+<span>$interval</span>][] = <span>$job</span><span>; </span><span> 76</span> <span> } </span><span> 77</span> <span> } </span><span> 78</span> <span>if</span>(<span>empty</span>(self::<span>$task</span>[<span>$time</span><span>])) </span><span> 79</span> <span> { </span><span> 80</span> <span>unset</span>(self::<span>$task</span>[<span>$time</span><span>]); </span><span> 81</span> <span> } </span><span> 82</span> <span> } </span><span> 83</span> <span> } </span><span> 84</span> <span> 85</span> <span>/*</span><span>* </span><span> 86</span> <span> *添加任务 </span><span> 87</span> <span>*/</span> <span> 88</span> <span>public</span> <span>static</span> <span>function</span> add(<span>$interval</span>, <span>$func</span>, <span>$argv</span> = <span>array</span>(), <span>$persist</span> = <span>false</span><span>) </span><span> 89</span> <span> { </span><span> 90</span> <span>if</span>(<span>is_null</span>(<span>$interval</span><span>)) </span><span> 91</span> <span> { </span><span> 92</span> <span>return</span><span>; </span><span> 93</span> <span> } </span><span> 94</span> <span>$time</span> = <span>time</span>()+<span>$interval</span><span>; </span><span> 95</span> <span>//</span><span>写入定时任务</span> <span> 96</span> self::<span>$task</span>[<span>$time</span>][] = <span>array</span>('func'=><span>$func</span>, 'argv'=><span>$argv</span>, 'interval'=><span>$interval</span>, 'persist'=><span>$persist</span><span>); </span><span> 97</span> <span> } </span><span> 98</span> <span> 99</span> <span>/*</span><span>* </span><span>100</span> <span> *删除所有定时器任务 </span><span>101</span> <span>*/</span> <span>102</span> <span>public</span> <span>function</span><span> dellAll() </span><span>103</span> <span> { </span><span>104</span> self::<span>$task</span> = <span>array</span><span>(); </span><span>105</span> <span> } </span><span>106</span> }
This is the core part of the timer class. There is a static variable that stores all the tasks that need to be performed. Why is it static here? Think about it for yourself. When the process receives the SIGALRM signal, the signalHandler function is triggered, and then the array is traversed sequentially. Check whether there are tasks that need to be executed at the current time. If there are any tasks that need to be executed at the current time, call back and pass parameters to delete the current job. Then check whether a persistent task is to be done. If so, continue to write the current job into the event array and wait for the next trigger. Finally, the current job will be deleted. The process sets an alarm signal. It can be seen that this timer, as long as it is triggered once, will be triggered again from the inside, achieving the purpose of self-loop.
<span> 1</span> <?<span>php </span><span> 2</span> <span> 3</span> <span>class</span><span> DoJob </span><span> 4</span> <span>{ </span><span> 5</span> <span>public</span> <span>function</span> job( <span>$param</span> = <span>array</span><span>() ) </span><span> 6</span> <span> { </span><span> 7</span> <span>$time</span> = <span>time</span><span>(); </span><span> 8</span> <span>echo</span> "Time: {<span>$time</span>}, Func: ".<span>get_class</span>()."::".<span>__FUNCTION__</span>."(".json_encode(<span>$param</span>).")\n"<span>; </span><span> 9</span> <span> } </span><span>10</span> }
This is the callback class and function. For the convenience of explanation, a lot of debugging information has been added. The Timer class and callback are available. Let’s see what the usage scenario is like.
<span> 1</span> <?<span>php </span><span> 2</span> <span> 3</span> <span>require_once</span>(__DIR__."/Timer.php"<span>); </span><span> 4</span> <span>require_once</span>(__DIR__."/DoJob.php"<span>); </span><span> 5</span> <span> 6</span> <span> 7</span> Timer::<span>dellAll(); </span><span> 8</span> <span> 9</span> Timer::add( 1, <span>array</span>('DoJob','job'), <span>array</span>(),<span>true</span><span>); </span><span>10</span> <span>11</span> Timer::add( 3, <span>array</span>('DoJob','job'),<span>array</span>('a'=>1), <span>false</span><span>); </span><span>12</span> <span>13</span> <span>echo</span> "Time start: ".<span>time</span>()."\n"<span>; </span><span>14</span> Timer::<span>run(); </span><span>15</span> <span>16</span> <span>while</span>(1<span>) </span><span>17</span> <span>{ </span><span>18</span> <span>sleep</span>(1<span>); </span><span>19</span> <span> pcntl_signal_dispatch(); </span><span>20</span> }
The code is very short. Two jobs are registered here, and then the timer is run to capture the signal trigger action in an infinite loop. If it is not captured, the pre-registered processing function will not be triggered. Such an automatic The development of the loop timer is completed. The running results are as follows:
Just like the tasks added by our scene class, two tasks were executed at 90, one is a persistent job without parameters, and the other is a non-persistent job with parameters, and then the non-persistent job is no longer Execute.
Summary