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

namespace Zotlabs\Daemon;

use Zotlabs\Lib\Queue as LibQueue;

class Queue {

	static public function run($argc, $argv) {
		$queue_id = ($argc > 1) ? $argv[1] : '';

		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

		$oldqItems = q("select outq_posturl, outq_hash from outq where outq_created < %s - INTERVAL %s",
			db_utcnow(),
			db_quoteinterval('3 DAY')
		);

		if ($oldqItems) {
			foreach ($oldqItems as $qItem) {
				$h = parse_url($qItem['outq_posturl']);
				$site_url = $h['scheme'] . '://' . $h['host'] . ((!empty($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($site_url),
					db_utcnow(),
					db_quoteinterval('1 MONTH')
				);
			}

			$old_hashes = ids_to_querystr($oldqItems, 'outq_hash', true);

			logger('Removing ' . count($oldqItems) . ' old queue entries');
			dbq("DELETE FROM outq WHERE outq_hash IN ($old_hashes)");

		}

		$deliveries = [];

		if ($queue_id) {
			$qItems = q("SELECT * FROM outq WHERE outq_hash = '%s' LIMIT 1",
				dbesc($queue_id)
			);
			logger('queue deliver: ' . $qItems[0]['outq_hash'] . ' to ' . $qItems[0]['outq_posturl'], LOGGER_DEBUG);
			LibQueue::deliver($qItems[0]);
		}
		else {
			$qItems = q("SELECT outq_hash FROM outq WHERE outq_scheduled < %s ",
				db_utcnow()
			);

			if ($qItems) {
				foreach ($qItems as $qItem) {
					$deliveries[] = $qItem['outq_hash'];
				}

				shuffle($deliveries);
				do_delivery($deliveries, true);
			}
		 }
	}

}