Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if(empty($argv[1]))exit(1);
- define('PROC_NAME', $argv[1]);
- define('LOCK_FILE', "/var/run/" . basename(PROC_NAME, ".php") . ".lock");
- if (!tryLock())die("Already running.\n");
- # remove the lock on exit (Control+C doesn't count as 'exit'?)
- register_shutdown_function('unlink', LOCK_FILE);
- $pid = getmypid();
- if(!cli_set_process_title(PROC_NAME)){
- echo "ERROR set_process_title::$pid...\n";
- exit(1);
- }
- require_once __DIR__ . '/vendor/autoload.php';
- use PhpAmqpLib\Connection\AMQPStreamConnection;
- use PhpAmqpLib\Message\AMQPMessage;
- $queue_name='host:search1';
- $connection = new AMQPStreamConnection('host', 5672, 'guest', 'guest');
- $channel = $connection->channel();
- $channel->queue_declare($queue_name, false, true, false, false);
- echo " [*] Waiting for messages. To exit press CTRL+C\n";
- $channel->basic_qos(null,1,null);
- $callback = function (AMQPMessage $msg) {
- echo ' [x] Received ', $msg->body, "\n";
- $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
- };
- $channel->basic_consume($queue_name, '',
- false,
- false, //подтвердить
- false,
- false, $callback);
- while ($channel->is_consuming()) {
- //while(count($channel->callbacks)) {
- $channel->wait();
- }
- $channel->close();
- $connection->close();
- #######################################
- function tryLock()
- {
- # If lock file exists, check if stale. If exists and is not stale, return TRUE
- # Else, create lock file and return FALSE.
- if (@symlink("/proc/" . getmypid(), LOCK_FILE) !== FALSE) # the @ in front of 'symlink' is to suppress the NOTICE you get if the LOCK_FILE exists
- return true;
- # link already exists
- # check if it's stale
- if (is_link(LOCK_FILE) && !is_dir(LOCK_FILE))
- {
- unlink(LOCK_FILE);
- # try to lock again
- return tryLock();
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement