-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.php
More file actions
executable file
·118 lines (95 loc) · 3.12 KB
/
timer.php
File metadata and controls
executable file
·118 lines (95 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/php
<?php
// Git version
// <copyright> Copyright (c) 2012 All Rights Reserved,
// Escurio
// http://www.escurio.com/
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Peter van Es</author>
// <version>1.0</version>
// <email>vanesp@escurio.com</email>
// <date>2013-12-15</date>
// <summary>timer sends a redis pubsub message every minute or so to keep the receiver alive</summary>
/**
* System_Daemon Example Code
*
* If you run this code successfully, a daemon will be spawned
* and stopped directly. You should find a log enty in
* /var/log/simple.log
*
*/
// Make it possible to test in source directory
// This is for PEAR developers only
ini_set('include_path', ini_get('include_path').':..');
// Include Class
error_reporting(E_ALL);
require_once "System/Daemon.php";
// No PEAR, run standalone
System_Daemon::setOption("usePEAR", false);
// Setup
$options = array(
'appName' => 'timer',
'appDir' => dirname(__FILE__),
'appDescription' => 'Sends pubsub message with time every minute',
'authorName' => 'Peter van Es',
'authorEmail' => 'vanesp@escurio.com',
'sysMaxExecutionTime' => '0',
'sysMaxInputTime' => '0',
'sysMemoryLimit' => '1024M',
'appRunAsGID' => 20, // dialout group for /dev/ttyS1
'appRunAsUID' => 1000,
);
System_Daemon::setOptions($options);
System_Daemon::log(System_Daemon::LOG_INFO, "Daemon not yet started so this ".
"will be written on-screen");
// Spawn Deamon!
System_Daemon::start();
System_Daemon::log(System_Daemon::LOG_INFO, "Daemon: '".
System_Daemon::getOption("appName").
"' spawned! This will be written to ".
System_Daemon::getOption("logLocation"));
// get access credentials
include('access.php');
date_default_timezone_set('Europe/Amsterdam');
// DEBUG
$debug = false;
// don't timeout!
set_time_limit(0);
// include Redis pub sub functionality
include('redis.php');
if ($debug) System_Daemon::info("timer.php: Ready to send clock ticks...\n");
while (true) {
if ($debug) System_Daemon::info("timer.php: Publishing tick\n");
// update Redis using socketstream message
$msg = new PubMessage;
$msg->setParams('Tick', '', '', time());
// check if redis is still connected
if (!$redis->isConnected()) {
try {
$redis->connect();
}
catch (Exception $e) {
$message = date('Y-m-d H:i') . " timer.php: Cannot connect to Redis " . $e->getMessage() . "\n";
if ($debug) System_Daemon::notice($message);
}
}
if ($redis->isConnected()) {
try {
$redis->publish('ss:event', json_encode($msg));
}
catch (Exception $e) {
$message = date('Y-m-d H:i') . " timer.php: Cannot publish to Redis " . $e->getMessage() . "\n";
if ($debug) System_Daemon::notice($message);
}
}
// and now sleep for a minute or so
sleep (60);
} // while
System_Daemon::stop();
?>