Skip to content

Commit

Permalink
Sync with Extension:CollabPads@REL1_39-1.0.x
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Vogel committed Nov 15, 2024
1 parent d98aa2d commit 1c2801c
Show file tree
Hide file tree
Showing 33 changed files with 2,012 additions and 188 deletions.
24 changes: 0 additions & 24 deletions .eslintrc.json

This file was deleted.

7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/composer.lock
/package-lock.json
vendor/
node_modules/
mongo/
config.php
.env
3 changes: 0 additions & 3 deletions .phan/config.php

This file was deleted.

9 changes: 0 additions & 9 deletions .stylelintrc.json

This file was deleted.

15 changes: 9 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ COPY bin /usr/src/CollabpadsBackend/bin
COPY src /usr/src/CollabpadsBackend/src
COPY composer.json /usr/src/CollabpadsBackend/
COPY config.docker.php /usr/src/CollabpadsBackend/config.php
RUN cd /usr/src/CollabpadsBackend/ && composer update --ignore-platform-req ext-mongodb
RUN cd /usr/src/CollabpadsBackend/ && composer update --no-dev --ignore-platform-req ext-mongodb

FROM php:8.1-cli
RUN mkdir -p /usr/src/CollabpadsBackend
COPY --from=builder /usr/src/CollabpadsBackend/ /usr/src/CollabpadsBackend/
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
FROM php:8.3-cli
ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
RUN install-php-extensions mongodb-stable
RUN mkdir -p /usr/src/CollabpadsBackend
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
COPY --from=builder /usr/src/CollabpadsBackend/ /usr/src/CollabpadsBackend/
WORKDIR /usr/src/CollabpadsBackend
ENTRYPOINT [ "php", "./bin/server.php" ]
# ps is required for the init.sh script
RUN apt-get update && apt-get install -y procps
ENTRYPOINT [ "sh", "./bin/init.sh" ]
46 changes: 46 additions & 0 deletions bin/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

PHP_BIN=/usr/local/bin/php
SERVER_SCRIPT=./bin/server.php
PID_FILE=/tmp/server.pid

start_server() {
# Log output to stdout
$PHP_BIN $SERVER_SCRIPT &
echo $! > $PID_FILE
}

is_server_running() {
if [ -f $PID_FILE ]; then
PID=`cat $PID_FILE`
if ps -p $PID > /dev/null; then
return 0
else
return 1
fi
else
return 1
fi
}

restart_server() {
printf "Server is not running, restarting...\n"
start_server
}

monitor_server() {
while true; do
is_server_running
if [ $? -eq 0 ]; then
sleep 1
else
restart_server
fi
done
}

# Start the server
printf "Starting the server...\n"
start_server
# Monitor the server
monitor_server
53 changes: 34 additions & 19 deletions bin/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

use GuzzleHttp\Client;
use MediaWiki\Extension\CollabPads\Backend\DAO\DAOFactory;
use MediaWiki\Extension\CollabPads\Backend\Rebaser;
use MediaWiki\Extension\CollabPads\Backend\Socket;
use MongoDB\Driver\Exception\ConnectionTimeoutException;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use Ratchet\Http\HttpServer;
Expand Down Expand Up @@ -42,27 +44,40 @@
$clientOptions = $config['http-client-options'] ?: [];
$logger->debug( 'HTTP Client Options: ' . json_encode( $clientOptions ) );
$httpClient = new Client( $clientOptions );

$retryTime = 5;
for ( $x = 0; ; $x++ ) {
for ( $x = 0; $x < 5; $x++ ) {
$logger->info( "Connecting to MongoDB..." );
try {
$authorDAO = DAOFactory::createAuthorDAO( $config );
$sessionDAO = DAOFactory::createSessionDAO( $config );

$server = IoServer::factory(
new HttpServer(
new WsServer(
new Socket( $config, $httpClient, $authorDAO, $sessionDAO, $logger )
)
),
$port,
$config['request-ip']
);

$logger->info( "System: starting server... count = $x" );
$server->run();
} catch ( Throwable $e ) {
$logger->error( 'Error: ' . $e->getMessage() . " Retrying in $retryTime seconds..." );
$authorDAO = DAOFactory::createAuthorDAO( $config, $logger );
$sessionDAO = DAOFactory::createSessionDAO( $config, $logger );
$authorDAO->cleanConnections();
$sessionDAO->cleanConnections();
break;
} catch ( ConnectionTimeoutException $e ) {
$logger->error( "MongoDB not available: " . $e->getMessage() );
if ( $x === 4 ) {
$logger->error( "Max retries reached, exiting..." );
exit( 1 );
}
$logger->error( "Retrying in $retryTime seconds..." );
sleep( $retryTime );
continue;
}
}

$rebaser = new Rebaser( $sessionDAO );
$rebaser->setLogger( $logger );
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Socket(
$config, $httpClient, $authorDAO, $sessionDAO, $logger, $rebaser
)
)
),
$port,
$config['request-ip']
);

$logger->info( "System: starting server..." );
$server->run();
4 changes: 3 additions & 1 deletion config.docker.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
'db-name' => getenv( 'COLLABPADS_BACKEND_MONGO_DB_NAME' ) ?: 'collabpads',
'db-user' => getenv( 'COLLABPADS_BACKEND_MONGO_DB_USER' ) ?: '',
'db-password' => getenv( 'COLLABPADS_BACKEND_MONGO_DB_PASSWORD' ) ?: '',
'db-defaultauthdb' => getenv( 'COLLABPADS_BACKEND_MONGO_DB_DEFAULT_AUTH_DB' ) ?: 'admin',
'log-level' => getenv( 'COLLABPADS_BACKEND_LOG_LEVEL' ) ?: 'warn',
'http-client-options' => $httpClientOptions
'http-client-options' => $httpClientOptions,
'behaviourOnError' => getenv( 'COLLBAPADS_BACKEND_BEHAVIOUR_ON_ERROR' ) ?: 'reinit',
];
3 changes: 2 additions & 1 deletion config.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
'db-user' => '',
'db-password' => '',
'log-level' => 'INFO',
'http-client-options' => []
'http-client-options' => [],
'behaviourOnError' => 'reinit'
];
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ services:
COLLABPADS_BACKEND_MONGO_DB_PASSWORD: ${COLLABPADS_BACKEND_MONGO_DB_PASSWORD}
COLLABPADS_BACKEND_LOG_LEVEL: ${COLLABPADS_BACKEND_LOG_LEVEL}
COLLABPADS_BACKEND_HTTP_CLIENT_OPTIONS: ${COLLABPADS_BACKEND_HTTP_CLIENT_OPTIONS}
COLLBAPADS_BACKEND_BEHAVIOUR_ON_ERROR: ${COLLBAPADS_BACKEND_BEHAVIOUR_ON_ERROR}
extra_hosts:
- "host.docker.internal:host-gateway"
collabpads-database:
Expand All @@ -24,4 +25,4 @@ services:
# environment:
# ME_CONFIG_MONGODB_SERVER: collabpads-database
# ports:
# - 8091:8081
# - 8091:8081
3 changes: 2 additions & 1 deletion sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ COLLABPADS_BACKEND_MONGO_DB_NAME=collabpads
COLLABPADS_BACKEND_MONGO_DB_USER=
COLLABPADS_BACKEND_MONGO_DB_PASSWORD=
COLLABPADS_BACKEND_LOG_LEVEL=warn
COLLABPADS_BACKEND_HTTP_CLIENT_OPTIONS={}
COLLABPADS_BACKEND_HTTP_CLIENT_OPTIONS={}
COLLBAPADS_BACKEND_BEHAVIOUR_ON_ERROR=reinit
11 changes: 8 additions & 3 deletions src/DAO/DAOFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,34 @@
use Exception;
use MediaWiki\Extension\CollabPads\Backend\IAuthorDAO;
use MediaWiki\Extension\CollabPads\Backend\ICollabSessionDAO;
use Psr\Log\LoggerInterface;
use UnexpectedValueException;

class DAOFactory {

/**
* @param array $config
* @param LoggerInterface $logger
* @return ICollabSessionDAO
* @throws Exception
*/
public static function createSessionDAO( array $config ): ICollabSessionDAO {
public static function createSessionDAO( array $config, LoggerInterface $logger ): ICollabSessionDAO {
if ( $config['db-type'] === 'mongo' ) {
return new MongoDBCollabSessionDAO( $config );
$instance = new MongoDBCollabSessionDAO( $config );
$instance->setLogger( $logger );
return $instance;
}

throw new UnexpectedValueException( "Invalid database type '{$config['db-type']}'" );
}

/**
* @param array $config
* @param LoggerInterface $logger
* @return IAuthorDAO
* @throws Exception
*/
public static function createAuthorDAO( array $config ): IAuthorDAO {
public static function createAuthorDAO( array $config, LoggerInterface $logger ): IAuthorDAO {
if ( $config['db-type'] === 'mongo' ) {
return new MongoDBAuthorDAO( $config );
}
Expand Down
14 changes: 10 additions & 4 deletions src/DAO/MongoDBAuthorDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MediaWiki\Extension\CollabPads\Backend\DAO;

use MediaWiki\Extension\CollabPads\Backend\IAuthorDAO;
use MediaWiki\Extension\CollabPads\Backend\Model\Author;

class MongoDBAuthorDAO extends MongoDBDAOBase implements IAuthorDAO {

Expand Down Expand Up @@ -55,12 +56,13 @@ public function deleteConnection( int $connectionId, int $authorId ) {
public function getSessionByConnection( int $connectionId ): int {
$result = $this->collection->find(
[ 'a_sessions.c_id' => $connectionId ],
[ 'projection' => [ 'a_sessions.s_id.$' => 1 ] ]
[ 'projection' => [ 'a_sessions.$' => 1 ] ]
);

foreach ( $result as $row ) {
return $row["a_sessions"][0]["s_id"];
}
return 0;
}

/**
Expand All @@ -72,8 +74,9 @@ public function getAuthorByConnection( int $connectionId ) {
);

foreach ( $result as $row ) {
return $row;
return new Author( $row['a_id'], $row['a_name'] );
}
return null;
}

/**
Expand All @@ -92,6 +95,7 @@ public function getConnectionByName( int $sessionId, string $authorName ) {
}
return $output;
}
return [];
}

/**
Expand All @@ -103,8 +107,9 @@ public function getAuthorByName( string $authorName ) {
);

foreach ( $result as $row ) {
return $row;
return new Author( $row['a_id'], $authorName );
}
return null;
}

/**
Expand All @@ -116,8 +121,9 @@ public function getAuthorById( int $authorId ) {
);

foreach ( $result as $row ) {
return $row;
return new Author( $authorId, $row['a_name'] );
}
return null;
}

/**
Expand Down
Loading

0 comments on commit 1c2801c

Please sign in to comment.