aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Daemon/Queue.php
blob: e1f4b73de92064862c0aa3a24cec0ff97803ed7d (plain) (blame)
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
<?php /** @file */

namespace Zotlabs\Daemon;

require_once('include/queue_fn.php');
require_once('include/zot.php');

class Queue {

	static public function run($argc, $argv) {

		require_once('include/items.php');
		require_once('include/bbcode.php');

		if ($argc > 1)
			$queue_id = $argv[1];
		else
			$queue_id = EMPTY_STR;

		logger('queue: start');

		// delete all queue items more than 3 days old
		// but first mark these sites dead if we haven't heard from them in a month

		$r = q("select outq_posturl from outq where outq_created < %s - INTERVAL %s",
			db_utcnow(), db_quoteinterval('3 DAY')
		);
		if ($r) {
			foreach ($r as $rr) {
				$h       = parse_url($rr['outq_posturl']);
				$desturl = $h['scheme'] . '://' . $h['host'] . (isset($h['port']) ? ':' . $h['port'] : '');
				q("update site set site_dead = 1 where site_dead = 0 and site_url = '%s' and site_update < %s - INTERVAL %s",
					dbesc($desturl),
					db_utcnow(), db_quoteinterval('1 MONTH')
				);
			}
		}

		q("DELETE FROM outq WHERE outq_created < %s - INTERVAL %s",
			db_utcnow(), db_quoteinterval('3 DAY')
		);

		if ($queue_id) {
			$r = q("SELECT * FROM outq WHERE outq_hash = '%s' LIMIT 1",
				dbesc($queue_id)
			);
		}
		else {

			// For the first 12 hours we'll try to deliver every 15 minutes
			// After that, we'll only attempt delivery once per hour. 
			// This currently only handles the default queue drivers ('zot' or '') which we will group by posturl 
			// so that we don't start off a thousand deliveries for a couple of dead hubs.
			// The zot driver will deliver everything destined for a single hub once contact is made (*if* contact is made).
			// Other drivers will have to do something different here and may need their own query.

			// Note: this requires some tweaking as new posts to long dead hubs once a day will keep them in the 
			// "every 15 minutes" category. We probably need to prioritise them when inserted into the queue
			// or just prior to this query based on recent and long-term delivery history. If we have good reason to believe
			// the site is permanently down, there's no reason to attempt delivery at all, or at most not more than once 
			// or twice a day. 

			$sqlrandfunc = db_getfunc('rand');

			$r = q("SELECT *,$sqlrandfunc as rn FROM outq WHERE outq_delivered = 0 and outq_scheduled < %s order by rn limit 1",
				db_utcnow()
			);
			while ($r) {
				foreach ($r as $rv) {
					queue_deliver($rv);
				}
				$r = q("SELECT *,$sqlrandfunc as rn FROM outq WHERE outq_delivered = 0 and outq_scheduled < %s order by rn limit 1",
					db_utcnow()
				);
			}
		}
		if (!$r)
			return;

		foreach ($r as $rv) {
			queue_deliver($rv);
		}
	}
}