-
Notifications
You must be signed in to change notification settings - Fork 0
/
node_scheduler.module
150 lines (135 loc) · 4.1 KB
/
node_scheduler.module
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
145
146
147
148
149
150
<?php
/**
* Implements hook_cron().
*/
function node_scheduler_cron() {
$queue = DrupalQueue::get('node_scheduler_queue');
// Select all scheduled actions that were scheduled to happen by now
$now = time();
$query = db_select('node_scheduler', 'ns');
$query->fields('ns')
->condition('scheduled_time', $now, '<')
->condition('queued_time', 0);
$result = $query->execute();
// Put each scheduled action in the queue to run
while ($item = $result->fetch()) {
$queue->createItem($item);
// Set queued time on this item so that we know it was added to the queue
$query = db_update('node_scheduler');
$query->fields(array('queued_time' => $now))
->condition('nid', $item->nid)
->condition('schedule_key', $item->schedule_key)
->execute();
}
// Delete old rows from the table, if they were executed more than 30 days ago
// TODO: Could use a variable to set how long to keep rows in the table
$one_month_ago = $now - (3600 * 24 * 30);
db_delete('node_scheduler')
->condition('executed_time', $one_month_ago, '<')
->condition('executed_time', 0, '>')
->execute();
}
/**
* Implements hook_cron_queue_info().
*/
function node_scheduler_cron_queue_info() {
$queues = array();
$queues['node_scheduler_queue'] = array(
//function to call for each item
'worker callback' => 'node_scheduler_queue_callback',
// seconds to spend working on the queue
// TODO: This should ideally be a variable
'time' => 10,
);
return $queues;
}
/**
* Implements hook_hook_info().
*/
function node_scheduler_hook_info() {
return array(
'node_scheduler_action' => array(
'group' => 'node_scheduler',
),
);
}
/**
* Callback function for queue node_scheduler_queue
*
* This function invokes the hook for other modules to run operations on this queue item.
*/
function node_scheduler_queue_callback($item) {
try {
module_invoke_all('node_scheduler_action', $item->nid, $item->schedule_key);
// Set executed time on this item so that we know it was successfully run
$now = time();
$query = db_update('node_scheduler');
$query->fields(array('executed_time' => $now))
->condition('nid', $item->nid)
->condition('schedule_key', $item->schedule_key)
->execute();
}
catch (Exception $e) {
watchdog(
'node_scheduler',
'The scheduled action !schedule_key on the node !nid could not be executed.',
array(
'!nid' => $item->nid,
'!schedule_key' => $item->schedule_key,
),
WATCHDOG_ERROR
);
}
}
/**
* Public function node_scheduler_clear_actions()
*
* Remove all unqueued, scheduled actions matching a schedule_key
*/
function node_scheduler_clear_actions($schedule_key, $keep_queued_actions = TRUE) {
$query = db_delete('node_scheduler');
$query->condition('schedule_key', $schedule_key);
if ($keep_queued_actions) {
$query->condition('queued_time', 0);
}
$query->execute();
}
/**
* Public function node_scheduler_schedule_action()
*
* Schedule an action to fire on a node at some point in the future (requires cron)
*/
function node_scheduler_schedule_action($nid, $schedule_key, $scheduled_time, $reschedule = TRUE) {
// If reschedule flag is set then remove any unqueued actions matching nid/key
if ($reschedule) {
node_scheduler_remove_action($nid, $schedule_key);
}
$now = time();
if ($scheduled_time < $now) {
$scheduled_time = $now;
}
// Insert this action into the node_scheduler table
$query = db_insert('node_scheduler')
->fields(array(
'nid' => $nid,
'schedule_key' => $schedule_key,
'scheduled_time' => $scheduled_time
))
->execute();
}
/**
* Public function node_scheduler_remove_action()
*
* Remove any unqueued, scheduled actions possibly matching nid/schedule_key
*/
function node_scheduler_remove_action($nid = 'all', $schedule_key = 'all') {
$query = db_delete('node_scheduler');
if ($schedule_key <> 'all' ) {
$query->condition('schedule_key', $schedule_key);
}
if ($nid <> 'all') {
$query->condition('nid', $nid);
}
$query->condition('queued_time', 0);
$query->execute();
}