-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathaction.php
144 lines (127 loc) · 4.11 KB
/
action.php
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
defined('TWITTERBOT') or die('Restricted.');
require_once('config.php');
require_once('storage.php');
/**
* This is the abstract superclass that all other Twitterbot Actions must
* extend. Namely, they require a "run" method that can be invoked from
* the main method of the application. All other implemented functions
* are at your discretion.
*
* @author Shannon Quinn
*/
abstract class Action {
const FAILURE = 0;
const SUCCESS = 1;
protected $name; // name of the class
protected $isActive; // is this action active?
protected $timeout = 2; // minutes until this action times out
protected $nextAttempt; // specific time this event will fire next
protected $frequency; // minutes to increment the next firing time
protected $db; // the database
protected $currentStatus = Action::SUCCESS;
protected $previousStatus = Action::SUCCESS;
/**
* Default constructor.
* @param string $name The name of this action.
* @param boolean $active Whether or not this action is active.
* @param array $params Key/value pairs of parameters.
*/
public function __construct($name, $active, $params) {
// assign all the variables
foreach ($params as $k => $v) {
$this->$k = $v;
}
$this->name = $name;
$this->isActive = $active;
}
/**
* This method also needs to be implemented by the subclasses. Dictates
* how the action runs.
* NOTE: If you need access to a database connection, do it in here!
*
* @return Action::SUCCESS if the method runs successfully,
* Action::FAILURE otherwise.
*/
public abstract function run();
/**
* Calculates the time for the next firing of this action.
*/
public function setNextAttempt() {
$this->nextAttempt = time() + ($this->frequency * 60);
}
/**
* Accessor for the nextAttempt field.
* @return The unix timestamp of when this action should fire.
*/
public function getNextAttempt() {
return $this->nextAttempt;
}
/**
* Accessor for the timeout count.
* @return The number of seconds until this action should be timed out if
* it has not completed.
*/
public function getTimeout() {
return $this->timeout * 60;
}
/**
* This method can be called after the run() method to perform
* post-processing.
*
* In this case, it logs any failures (if the run() return value is
* Action::FAILURE) and saves the necessary values to the database.
* @param int $status The return code from the child process exiting.
*/
public function post_run($status) {
$this->db = Storage::getDatabase();
// log the status of this action
if ($status !== $this->currentStatus) {
$this->previousStatus = $this->currentStatus;
}
if ($status === self::FAILURE) {
if ($this->currentStatus === self::FAILURE) {
// failed consecutive times
$this->db->log($this->name, "Still have not recovered from previous" .
"error!");
} else {
// this is the first time the action has failed
$this->db->log($this->name, "Error has occurred!");
}
} else {
// Action::SUCCESS. Log this only if the previous status
// was Action::FAILURE, so we know we've recovered from something.
if ($this->previousStatus === Action::FAILURE) {
$this->db->log($this->name, "Recovered from previous failure.");
} else {
$this->db->log($this->name, "Successful run!");
}
}
// set the current status
$this->currentStatus = $status;
// destroy the database connection
unset($this->db);
}
/**
* Simple accessor for the state of this action.
* @return True if this Action is active, false otherwise.
*/
public function isActive() {
return $this->isActive;
}
/**
* Change the active state of this action. This is changed through
* signaling.
* @param boolean $state The new active state of this action (true or false).
*/
public function setActive($state) {
$this->isActive = $state;
}
/**
* Accessor for this action's name.
* @return The name (unique identifier) of this action.
*/
public function getName() {
return $this->name;
}
}