File size: 11,317 Bytes
889459c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284

#!/usr/bin/env python3
"""
خدمة العمل في الخلفية - تشغيل النظام كخدمة خلفية
يمكن التحكم بها عبر HTTP API أو إشارات النظام
"""

import os
import sys
import time
import signal
import logging
import threading
import subprocess
from pathlib import Path
from flask import Flask, jsonify, request
import json
from datetime import datetime
from peer_discovery import PORT

class BackgroundService:
    def __init__(self):
        self.app = Flask(__name__)
        self.is_running = False
        self.services = {}
        self.setup_routes()
        self.setup_logging()
        
    def setup_logging(self):
        """إعداد نظام السجلات"""
        log_dir = Path("logs")
        log_dir.mkdir(exist_ok=True)
        
        logging.basicConfig(
            level=logging.INFO,
            format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
            handlers=[
                logging.FileHandler(log_dir / 'background_service.log'),
                logging.StreamHandler(sys.stdout)
            ]
        )
        self.logger = logging.getLogger('BackgroundService')
        
    def setup_routes(self):
        """إعداد مسارات HTTP API للتحكم في الخدمة"""
        
        @self.app.route('/status')
        def status():
            """حالة الخدمة"""
            return jsonify({
                'status': 'running' if self.is_running else 'stopped',
                'services': {name: service['status'] for name, service in self.services.items()},
                'uptime': time.time() - self.start_time if hasattr(self, 'start_time') else 0
            })
            
        @self.app.route('/start', methods=['POST'])
        def start_services():
            """بدء تشغيل الخدمات"""
            self.start_all_services()
            return jsonify({'message': 'Services started successfully'})
            
        @self.app.route('/stop', methods=['POST'])
        def stop_services():
            """إيقاف الخدمات"""
            self.stop_all_services()
            return jsonify({'message': 'Services stopped successfully'})
            
        @self.app.route('/restart', methods=['POST'])
        def restart_services():
            """إعادة تشغيل الخدمات"""
            self.stop_all_services()
            time.sleep(2)
            self.start_all_services()
            return jsonify({'message': 'Services restarted successfully'})
            
        @self.app.route('/show-ui', methods=['POST'])
        def show_ui():
            """إظهار الواجهة التفاعلية"""
            self.launch_ui()
            return jsonify({'message': 'UI launched'})
            
        @self.app.route('/hide-ui', methods=['POST'])
        def hide_ui():
            """إخفاء الواجهة التفاعلية"""
            self.hide_ui_windows()
            return jsonify({'message': 'UI hidden'})
            
    def start_all_services(self):
        """بدء تشغيل جميع الخدمات الخلفية"""
        self.is_running = True
        self.start_time = time.time()
        
        services_to_start = [
            ('peer_server', 'peer_server.py'),
            ('rpc_server', 'rpc_server.py'),
            ('load_balancer', 'load_balancer.py'),
            ('distributed_executor', 'main.py')
        ]
        
        for service_name, script_file in services_to_start:
            try:
                process = subprocess.Popen(
                    [sys.executable, script_file],
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE,
                    cwd=os.getcwd()
                )
                
                self.services[service_name] = {
                    'process': process,
                    'status': 'running',
                    'started_at': datetime.now().isoformat(),
                    'script': script_file
                }
                
                self.logger.info(f"✅ بدء تشغيل {service_name} (PID: {process.pid})")
                
            except Exception as e:
                self.logger.error(f"❌ فشل في بدء تشغيل {service_name}: {e}")
                self.services[service_name] = {
                    'process': None,
                    'status': 'failed',
                    'error': str(e)
                }
                
    def stop_all_services(self):
        """إيقاف جميع الخدمات"""
        self.is_running = False
        
        for service_name, service_info in self.services.items():
            if service_info.get('process'):
                try:
                    service_info['process'].terminate()
                    service_info['process'].wait(timeout=5)
                    service_info['status'] = 'stopped'
                    self.logger.info(f"🛑 تم إيقاف {service_name}")
                except Exception as e:
                    # إجبار الإيقاف
                    service_info['process'].kill()
                    self.logger.warning(f"⚠️ تم إجبار إيقاف {service_name}: {e}")
                    
    def launch_ui(self):
        """تشغيل الواجهة التفاعلية عند الحاجة"""
        try:
            # تشغيل خادم الواجهة الأمامية
            ui_process = subprocess.Popen(
                ['npm', 'run', 'dev'],
                cwd=os.getcwd(),
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE
            )
            
            self.services['ui_server'] = {
                'process': ui_process,
                'status': 'running',
                'started_at': datetime.now().isoformat()
            }
            
            self.logger.info("🖥️ تم تشغيل الواجهة التفاعلية")
            
            # فتح المتصفح تلقائياً (اختياري)
            import webbrowser
            time.sleep(3)  # انتظار حتى يصبح الخادم جاهزاً
            webbrowser.open('http://localhost:5173')
            
        except Exception as e:
            self.logger.error(f"❌ فشل في تشغيل الواجهة التفاعلية: {e}")
            
    def hide_ui_windows(self):
        """إخفاء نوافذ الواجهة التفاعلية"""
        if 'ui_server' in self.services and self.services['ui_server'].get('process'):
            try:
                self.services['ui_server']['process'].terminate()
                self.services['ui_server']['status'] = 'stopped'
                self.logger.info("🔒 تم إخفاء الواجهة التفاعلية")
            except Exception as e:
                self.logger.error(f"❌ فشل في إخفاء الواجهة التفاعلية: {e}")
                
    def health_check_loop(self):
        """فحص دوري لحالة الخدمات وإعادة تشغيلها عند الحاجة"""
        while self.is_running:
            for service_name, service_info in self.services.items():
                if service_info.get('process') and service_info['status'] == 'running':
                    if service_info['process'].poll() is not None:
                        # الخدمة توقفت بشكل غير متوقع
                        self.logger.warning(f"⚠️ الخدمة {service_name} توقفت، إعادة تشغيل...")
                        self.restart_single_service(service_name)
                        
            time.sleep(30)  # فحص كل 30 ثانية
            
    def restart_single_service(self, service_name):
        """إعادة تشغيل خدمة واحدة"""
        service_info = self.services.get(service_name)
        if not service_info:
            return
            
        script_file = service_info.get('script')
        if script_file:
            try:
                process = subprocess.Popen(
                    [sys.executable, script_file],
                    stdout=subprocess.PIPE,
                    stderr=subprocess.PIPE
                )
                
                self.services[service_name].update({
                    'process': process,
                    'status': 'running',
                    'restarted_at': datetime.now().isoformat()
                })
                
                self.logger.info(f"✅ تم إعادة تشغيل {service_name}")
                
            except Exception as e:
                self.logger.error(f"❌ فشل في إعادة تشغيل {service_name}: {e}")
                
    def setup_signal_handlers(self):
        """إعداد معالجات الإشارات للتحكم في الخدمة"""
        def signal_handler(signum, frame):
            self.logger.info(f"تلقي إشارة {signum}, إيقاف الخدمة...")
            self.stop_all_services()
            sys.exit(0)
            
        signal.signal(signal.SIGTERM, signal_handler)
        signal.signal(signal.SIGINT, signal_handler)
        
    def run_as_daemon(self):
        """تشغيل الخدمة كخدمة خلفية"""
        self.logger.info("🚀 بدء تشغيل الخدمة في الخلفية...")
        
        # بدء الخدمات
        self.start_all_services()
        
        # بدء حلقة الفحص الصحي
        health_thread = threading.Thread(target=self.health_check_loop, daemon=True)
        health_thread.start()
        
        # إعداد معالجات الإشارات
        self.setup_signal_handlers()
        
        # تشغيل خادم HTTP API للتحكم
        self.logger.info("🌐 تشغيل HTTP API على المنفذ 8888")
        self.app.run(host='0.0.0.0', port=8888, debug=False)

def main():
    service = BackgroundService()
    
    if len(sys.argv) > 1:
        command = sys.argv[1]
        
        if command == 'start':
            service.run_as_daemon()
        elif command == 'status':
            # فحص حالة الخدمة
            import requests
            try:
                response = requests.get('http://localhost:8888/status')
                print(json.dumps(response.json(), indent=2, ensure_ascii=False))
            except:
                print("❌ الخدمة غير متاحة")
        elif command == 'stop':
            # إيقاف الخدمة
            import requests
            try:
                response = requests.post('http://localhost:8888/stop')
                print(response.json()['message'])
            except:
                print("❌ فشل في إيقاف الخدمة")
        elif command == 'show-ui':
            # إظهار الواجهة التفاعلية
            import requests
            try:
                response = requests.post('http://localhost:8888/show-ui')
                print(response.json()['message'])
            except:
                print("❌ فشل في إظهار الواجهة التفاعلية")
        else:
            print("الأوامر المتاحة: start, status, stop, show-ui")
    else:
        print("استخدام: python background_service.py [start|status|stop|show-ui]")

if __name__ == "__main__":
    main()