Multithreading & settings can now be replaced directly on deserialization.

This commit is contained in:
Daniil Gentili 2018-03-02 13:32:48 +01:00
parent e515e9d103
commit e2aef54979
18 changed files with 58 additions and 12 deletions

View File

@ -216,6 +216,11 @@ To load a serialized session:
$MadelineProto = new \danog\MadelineProto\API('session.madeline');
```
To load a serialized session, replacing settings on deserialization:
```
$MadelineProto = new \danog\MadelineProto\API('session.madeline', $settings);
```
If the scripts shutsdown normally (without ctrl+c or fatal errors/exceptions), the session will be serialized automatically.

View File

@ -16,7 +16,7 @@ require 'vendor/autoload.php';
$settings = ['app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e']]; //, 'connection_settings' => ['all' => ['test_mode' => true]]];
try {
$MadelineProto = new \danog\MadelineProto\API('bot.madeline');
$MadelineProto = new \danog\MadelineProto\API('bot.madeline', $settings);
} catch (\danog\MadelineProto\Exception $e) {
\danog\MadelineProto\Logger::log($e->getMessage());
$MadelineProto = new \danog\MadelineProto\API($settings);

View File

@ -160,7 +160,7 @@ Easy as pie:
```
$call->storage["pony"] = "fluttershy";
var_dump($call->storage["pony"]); // fluttershy
\danog\MadelineProto\Logger::log($call->storage["pony"]); // fluttershy
```
Note: when modifying this property, *never* overwrite the previous values. Always either modify the values of the array separately like showed above, or use array_merge.

View File

@ -220,6 +220,11 @@ To load a serialized session:
$MadelineProto = new \danog\MadelineProto\API('session.madeline');
```
To load a serialized session, replacing settings on deserialization:
```
$MadelineProto = new \danog\MadelineProto\API('session.madeline', $settings);
```
If the scripts shutsdown normally (without ctrl+c or fatal errors/exceptions), the session will be serialized automatically.

View File

@ -160,7 +160,7 @@ Easy as pie:
```
$call->storage["pony"] = "fluttershy";
var_dump($call->storage["pony"]); // fluttershy
\danog\MadelineProto\Logger::log($call->storage["pony"]); // fluttershy
```
Note: when modifying this property, *never* overwrite the previous values. Always either modify the values of the array separately like showed above, or use array_merge.

View File

@ -160,7 +160,7 @@ Easy as pie:
```
$call->storage["pony"] = "fluttershy";
var_dump($call->storage["pony"]); // fluttershy
\danog\MadelineProto\Logger::log($call->storage["pony"]); // fluttershy
```
Note: when modifying this property, *never* overwrite the previous values. Always either modify the values of the array separately like showed above, or use array_merge.

View File

@ -160,7 +160,7 @@ Easy as pie:
```
$call->storage["pony"] = "fluttershy";
var_dump($call->storage["pony"]); // fluttershy
\danog\MadelineProto\Logger::log($call->storage["pony"]); // fluttershy
```
Note: when modifying this property, *never* overwrite the previous values. Always either modify the values of the array separately like showed above, or use array_merge.

View File

@ -160,7 +160,7 @@ Easy as pie:
```
$call->storage["pony"] = "fluttershy";
var_dump($call->storage["pony"]); // fluttershy
\danog\MadelineProto\Logger::log($call->storage["pony"]); // fluttershy
```
Note: when modifying this property, *never* overwrite the previous values. Always either modify the values of the array separately like showed above, or use array_merge.

View File

@ -160,7 +160,7 @@ Easy as pie:
```
$call->storage["pony"] = "fluttershy";
var_dump($call->storage["pony"]); // fluttershy
\danog\MadelineProto\Logger::log($call->storage["pony"]); // fluttershy
```
Note: when modifying this property, *never* overwrite the previous values. Always either modify the values of the array separately like showed above, or use array_merge.

View File

@ -160,7 +160,7 @@ Easy as pie:
```
$call->storage["pony"] = "fluttershy";
var_dump($call->storage["pony"]); // fluttershy
\danog\MadelineProto\Logger::log($call->storage["pony"]); // fluttershy
```
Note: when modifying this property, *never* overwrite the previous values. Always either modify the values of the array separately like showed above, or use array_merge.

View File

@ -160,7 +160,7 @@ Easy as pie:
```
$call->storage["pony"] = "fluttershy";
var_dump($call->storage["pony"]); // fluttershy
\danog\MadelineProto\Logger::log($call->storage["pony"]); // fluttershy
```
Note: when modifying this property, *never* overwrite the previous values. Always either modify the values of the array separately like showed above, or use array_merge.

View File

@ -160,7 +160,7 @@ Easy as pie:
```
$call->storage["pony"] = "fluttershy";
var_dump($call->storage["pony"]); // fluttershy
\danog\MadelineProto\Logger::log($call->storage["pony"]); // fluttershy
```
Note: when modifying this property, *never* overwrite the previous values. Always either modify the values of the array separately like showed above, or use array_merge.

View File

@ -4,3 +4,4 @@ description: Documentation of old mtproto layers
---
# Documentation of old mtproto layers
[Layer v73](API_docs_v73/)

View File

@ -34,6 +34,7 @@ class API extends APIFactory
$realpaths['lockfile'] = fopen($realpaths['lockfile'], 'r');
\danog\MadelineProto\Logger::log('Waiting for shared lock of serialization lockfile...');
flock($realpaths['lockfile'], LOCK_SH);
\danog\MadelineProto\Logger::log('Shared lock acquired, deserializing...');
try {
$unserialized = file_get_contents($realpaths['file']);
@ -94,7 +95,7 @@ class API extends APIFactory
public function __destruct()
{
if (\danog\MadelineProto\Logger::$has_thread && is_object(\Thread::getCurrentThread())) {
if (\danog\MadelineProto\Logger::$has_thread && is_object(\Thread::getCurrentThread()) || Logger::is_fork()) {
return;
}
if (!is_null($this->session)) {
@ -122,6 +123,10 @@ class API extends APIFactory
public function __set($name, $value)
{
if ($name === 'settings') {
if (Logger::is_fork()) {
\danog\MadelineProto\Logger::log('Detected fork');
$this->API->__wakeup();
}
return $this->API->__construct($value);
}

View File

@ -109,15 +109,25 @@ class APIFactory
public $namespace = '';
public $API;
public $lua = false;
public $pid;
public function __construct($namespace, $API)
{
$this->namespace = $namespace.'.';
$this->API = $API;
$this->pid = getmypid();
}
public function __call($name, $arguments)
{
if (Logger::is_fork()) {
\danog\MadelineProto\Logger::log('Detected fork');
$this->API->reset_session();
foreach ($this->API->datacenter->sockets as $datacenter) {
$datacenter->close_and_reopen();
}
}
if ($this->API->setdem) {
$this->API->setdem = false;
$this->API->__construct($this->API->settings);

View File

@ -32,6 +32,8 @@ class Logger
public static $bigint = true;
public static $colors = [];
public static $isatty = false;
private static $pid;
const ULTRA_VERBOSE = 5;
const VERBOSE = 4;
const NOTICE = 3;
@ -66,6 +68,13 @@ class Logger
}
}
public static function is_fork() {
if (self::$pid === null) {
self::$pid = getmypid();
}
return self::$pid !== getmypid();
}
/*
* Constructor function
* Accepts various logger modes:
@ -100,10 +109,13 @@ class Logger
if (\danog\MadelineProto\Logger::$has_thread && is_object(\Thread::getCurrentThread())) {
$prefix .= ' (t)';
}
if (\danog\MadelineProto\Logger::is_fork()) {
$prefix .= ' (p)';
}
if (!is_string($param)) {
$param = json_encode($param, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
$param = str_pad(basename(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)[0]['file'], '.php').$prefix.': ', 16 + strlen($prefix))."\t".$param;
$param = str_pad(basename(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0]['file'], '.php').$prefix.': ', 16 + strlen($prefix))."\t".$param;
switch (self::$mode) {
case 1:
error_log($param);

View File

@ -194,6 +194,12 @@ class MTProto
}
$force = false;
$this->reset_session();
$backtrace = debug_backtrace(0, 3);
if (isset($backtrace[2]['function']) && isset($backtrace[2]['class']) && isset($backtrace[2]['args']) && count($backtrace[2]['args']) === 2 && $backtrace[2]['class'] === 'danog\\MadelineProto\\API' && $backtrace[2]['function'] === '__magic_construct') {
Logger::log('Updating settings on wakeup');
$this->parse_settings(array_replace_recursive($this->settings, $backtrace[2]['args'][1]));
}
if (!isset($this->v) || $this->v !== self::V) {
\danog\MadelineProto\Logger::log(\danog\MadelineProto\Lang::$current_lang['serialization_ofd'], Logger::WARNING);
foreach ($this->datacenter->sockets as $dc_id => $socket) {

View File

@ -63,6 +63,7 @@ class Serialization
$realpaths['lockfile'] = fopen($realpaths['lockfile'], 'w');
\danog\MadelineProto\Logger::log('Waiting for exclusive lock of serialization lockfile...');
flock($realpaths['lockfile'], LOCK_EX);
\danog\MadelineProto\Logger::log('Lock acquired, serializing');
try {
$wrote = file_put_contents($realpaths['tempfile'], serialize($instance));
@ -95,6 +96,7 @@ class Serialization
$realpaths['lockfile'] = fopen($realpaths['lockfile'], 'r');
\danog\MadelineProto\Logger::log('Waiting for shared lock of serialization lockfile...');
flock($realpaths['lockfile'], LOCK_SH);
\danog\MadelineProto\Logger::log('Lock acquired, deserializing');
try {
$unserialized = file_get_contents($realpaths['file']);