Skip to content

Commit e20229a

Browse files
authored
Fixed Major Update....
1 parent 4fccbf1 commit e20229a

File tree

1 file changed

+168
-19
lines changed

1 file changed

+168
-19
lines changed

sudoers/restart/restart.py

Lines changed: 168 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def check_session_permissions(session_file: str) -> bool:
2727
if not os.path.exists(session_file):
2828
LOGGER.warning(f"Session file {session_file} not found")
2929
return True
30+
3031
if not os.access(session_file, os.W_OK):
3132
LOGGER.error(f"Session file {session_file} is not writable")
3233
try:
@@ -38,18 +39,28 @@ def check_session_permissions(session_file: str) -> bool:
3839
return False
3940
return True
4041

42+
async def cleanup_restart_data():
43+
try:
44+
await restart_messages.delete_many({})
45+
LOGGER.info("Cleaned up any existing restart messages from database")
46+
except Exception as e:
47+
LOGGER.error(f"Failed to cleanup restart data: {e}")
48+
4149
def setup_restart_handler(app: Client):
4250
@app.on_message(filters.command(["restart", "reboot", "reload"], prefixes=COMMAND_PREFIX) & (filters.private | filters.group))
4351
async def restart(client: Client, message):
4452
user_id = message.from_user.id
4553
if not await is_admin(user_id):
4654
return
55+
4756
LOGGER.info(f"Restart command received from user {user_id}")
57+
4858
response = await client.send_message(
4959
chat_id=message.chat.id,
50-
text="**Restarting bot... Please wait.**",
60+
text="**Restarting Bot... Please Wait.**",
5161
parse_mode=ParseMode.MARKDOWN
5262
)
63+
5364
session_file = "SmartTools.session"
5465
if not check_session_permissions(session_file):
5566
await client.edit_message_text(
@@ -59,6 +70,7 @@ async def restart(client: Client, message):
5970
parse_mode=ParseMode.MARKDOWN
6071
)
6172
return
73+
6274
directories = ["downloads", "temp", "temp_media", "data", "repos", "temp_dir"]
6375
for directory in directories:
6476
try:
@@ -67,51 +79,165 @@ async def restart(client: Client, message):
6779
LOGGER.info(f"Cleared directory: {directory}")
6880
except Exception as e:
6981
LOGGER.error(f"Failed to clear directory {directory}: {e}")
82+
7083
log_file = "botlog.txt"
7184
if os.path.exists(log_file):
7285
try:
7386
os.remove(log_file)
7487
LOGGER.info(f"Cleared log file: {log_file}")
7588
except Exception as e:
7689
LOGGER.error(f"Failed to clear log file {log_file}: {e}")
90+
7791
start_script = "start.sh"
92+
main_script = "main.py"
93+
7894
if not os.path.exists(start_script):
79-
LOGGER.error("Start script not found")
95+
if os.path.exists(main_script):
96+
LOGGER.warning("start.sh not found, will try direct python execution")
97+
start_script = None
98+
else:
99+
LOGGER.error("Neither start.sh nor main.py found")
100+
await client.edit_message_text(
101+
chat_id=message.chat.id,
102+
message_id=response.id,
103+
text="**Failed To Restart Due To Unix Issue❌**",
104+
parse_mode=ParseMode.MARKDOWN
105+
)
106+
return
107+
108+
try:
109+
await cleanup_restart_data()
110+
111+
restart_data = {
112+
"chat_id": message.chat.id,
113+
"msg_id": response.id
114+
}
115+
116+
await restart_messages.insert_one(restart_data)
117+
LOGGER.info(f"Stored restart message details for chat {message.chat.id}")
118+
119+
await asyncio.sleep(2)
120+
121+
except Exception as e:
122+
LOGGER.error(f"Failed to store restart message data: {e}")
80123
await client.edit_message_text(
81124
chat_id=message.chat.id,
82125
message_id=response.id,
83-
text="**Failed To Restart Due To Unix Issue❌**",
126+
text="**Failed To Restart Due To Database Issue❌**",
84127
parse_mode=ParseMode.MARKDOWN
85128
)
86129
return
130+
87131
try:
88-
await restart_messages.insert_one({
89-
"chat_id": message.chat.id,
90-
"msg_id": response.id
91-
})
92-
LOGGER.info(f"Stored restart message details for chat {message.chat.id}")
93-
subprocess.Popen(["bash", start_script])
132+
if start_script:
133+
if not os.access(start_script, os.X_OK):
134+
try:
135+
os.chmod(start_script, 0o755)
136+
LOGGER.info(f"Set execute permissions for {start_script}")
137+
except Exception as e:
138+
LOGGER.error(f"Failed to set execute permissions: {e}")
139+
raise
140+
141+
process = subprocess.Popen(
142+
["bash", start_script],
143+
stdin=subprocess.DEVNULL,
144+
stdout=None,
145+
stderr=None,
146+
start_new_session=True
147+
)
148+
LOGGER.info("Started bot using bash script")
149+
else:
150+
import sys
151+
python_executable = sys.executable
152+
153+
process = subprocess.Popen(
154+
[python_executable, main_script],
155+
stdin=subprocess.DEVNULL,
156+
stdout=None,
157+
stderr=None,
158+
start_new_session=True,
159+
cwd=os.getcwd()
160+
)
161+
LOGGER.info("Started bot using direct python execution")
162+
163+
await asyncio.sleep(3)
164+
165+
166+
if process.poll() is not None:
167+
if process.returncode != 0:
168+
raise subprocess.CalledProcessError(process.returncode, start_script or main_script)
169+
else:
170+
LOGGER.info("Start process completed immediately, checking if this is expected")
171+
else:
172+
LOGGER.info("Start process is running in background")
173+
174+
LOGGER.info("Restart executed successfully, shutting down current instance")
175+
await asyncio.sleep(2)
94176
os._exit(0)
95-
except Exception as e:
96-
LOGGER.error(f"Restart command execution failed: {e}")
177+
178+
except subprocess.CalledProcessError as e:
179+
LOGGER.error(f"Start process failed with return code {e.returncode}")
180+
try:
181+
await restart_messages.delete_one({"chat_id": message.chat.id, "msg_id": response.id})
182+
except Exception as db_e:
183+
LOGGER.error(f"Failed to cleanup restart data after script failure: {db_e}")
184+
185+
error_msg = "**Failed To Restart Invalid LF Format❌**" if start_script else "**Failed To Restart Python Script Error❌**"
97186
await client.edit_message_text(
98187
chat_id=message.chat.id,
99188
message_id=response.id,
100-
text="**Failed To Restart Invalid LF Format❌**",
189+
text=error_msg,
101190
parse_mode=ParseMode.MARKDOWN
102191
)
192+
return
193+
194+
except FileNotFoundError:
195+
LOGGER.error("Bash shell not found")
196+
try:
197+
await restart_messages.delete_one({"chat_id": message.chat.id, "msg_id": response.id})
198+
except Exception as db_e:
199+
LOGGER.error(f"Failed to cleanup restart data after bash error: {db_e}")
200+
201+
await client.edit_message_text(
202+
chat_id=message.chat.id,
203+
message_id=response.id,
204+
text="**Failed To Restart Due To Unix Issue❌**",
205+
parse_mode=ParseMode.MARKDOWN
206+
)
207+
return
208+
209+
except Exception as e:
210+
LOGGER.error(f"Restart command execution failed: {e}")
211+
try:
212+
await restart_messages.delete_one({"chat_id": message.chat.id, "msg_id": response.id})
213+
except Exception as db_e:
214+
LOGGER.error(f"Failed to cleanup restart data after general error: {db_e}")
215+
216+
try:
217+
await client.edit_message_text(
218+
chat_id=message.chat.id,
219+
message_id=response.id,
220+
text="**Failed To Restart Due To System Error❌**",
221+
parse_mode=ParseMode.MARKDOWN
222+
)
223+
except Exception as msg_e:
224+
LOGGER.error(f"Failed to update error message: {msg_e}")
225+
return
103226

104227
@app.on_message(filters.command(["stop", "kill", "off"], prefixes=COMMAND_PREFIX) & (filters.private | filters.group))
105228
async def stop(client: Client, message):
106229
user_id = message.from_user.id
107230
if not await is_admin(user_id):
108231
return
232+
109233
LOGGER.info(f"Stop command received from user {user_id}")
234+
110235
response = await client.send_message(
111236
chat_id=message.chat.id,
112237
text="**Stopping bot and clearing data...**",
113238
parse_mode=ParseMode.MARKDOWN
114239
)
240+
115241
directories = ["downloads", "temp", "temp_media", "data", "repos"]
116242
for directory in directories:
117243
try:
@@ -120,26 +246,49 @@ async def stop(client: Client, message):
120246
LOGGER.info(f"Cleared directory: {directory}")
121247
except Exception as e:
122248
LOGGER.error(f"Failed to clear directory {directory}: {e}")
249+
123250
log_file = "botlog.txt"
124251
if os.path.exists(log_file):
125252
try:
126253
os.remove(log_file)
127254
LOGGER.info(f"Cleared log file: {log_file}")
128255
except Exception as e:
129256
LOGGER.error(f"Failed to clear log file {log_file}: {e}")
257+
258+
try:
259+
await cleanup_restart_data()
260+
LOGGER.info("Cleaned up restart data before stopping")
261+
except Exception as e:
262+
LOGGER.error(f"Failed to cleanup restart data during stop: {e}")
263+
130264
try:
131265
await client.edit_message_text(
132266
chat_id=message.chat.id,
133267
message_id=response.id,
134268
text="**Bot stopped successfully, data cleared**",
135269
parse_mode=ParseMode.MARKDOWN
136270
)
271+
272+
await asyncio.sleep(2)
273+
274+
try:
275+
subprocess.run(["pkill", "-f", "main.py"], check=False, timeout=5)
276+
except (subprocess.TimeoutExpired, FileNotFoundError):
277+
LOGGER.warning("pkill command failed or timed out, using direct exit")
278+
137279
os._exit(0)
280+
138281
except Exception as e:
139-
LOGGER.error(f"Failed to stop bot: {e}")
140-
await client.edit_message_text(
141-
chat_id=message.chat.id,
142-
message_id=response.id,
143-
text="**Failed To Stop Bot Due To Telegram Limit ❌ **",
144-
parse_mode=ParseMode.MARKDOWN
145-
)
282+
LOGGER.error(f"Failed to update stop message: {e}")
283+
try:
284+
await client.edit_message_text(
285+
chat_id=message.chat.id,
286+
message_id=response.id,
287+
text="**Failed To Stop Bot Due To Telegram Limit ❌**",
288+
parse_mode=ParseMode.MARKDOWN
289+
)
290+
except Exception as msg_e:
291+
LOGGER.error(f"Failed to update error stop message: {msg_e}")
292+
293+
await asyncio.sleep(2)
294+
os._exit(0)

0 commit comments

Comments
 (0)