File size: 36,921 Bytes
885c1e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e986bc1
 
885c1e6
aae5516
885c1e6
 
 
e986bc1
 
 
 
2b9d493
 
885c1e6
e986bc1
 
 
 
 
 
 
 
aae5516
e986bc1
130224f
11067ff
a6cf495
 
 
 
e986bc1
 
885c1e6
 
 
 
 
 
 
 
 
e986bc1
885c1e6
 
 
e986bc1
885c1e6
e986bc1
885c1e6
 
 
2fa9362
885c1e6
 
e986bc1
885c1e6
 
e986bc1
885c1e6
 
e986bc1
 
 
 
 
 
885c1e6
 
a6cf495
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e986bc1
 
 
885c1e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130224f
 
 
 
 
 
 
 
 
 
885c1e6
130224f
 
 
 
 
 
 
 
 
885c1e6
 
 
2fa9362
885c1e6
 
 
 
 
 
130224f
885c1e6
130224f
885c1e6
 
 
 
 
 
 
 
 
2fa9362
130224f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fa9362
130224f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
885c1e6
 
2fa9362
130224f
 
 
885c1e6
130224f
2fa9362
130224f
 
885c1e6
130224f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fa9362
130224f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fa9362
885c1e6
130224f
2fa9362
130224f
885c1e6
 
 
130224f
 
 
 
885c1e6
 
 
 
 
 
 
130224f
885c1e6
 
 
 
130224f
885c1e6
130224f
885c1e6
 
 
 
130224f
885c1e6
 
 
 
2fa9362
 
885c1e6
 
 
 
 
 
2fa9362
885c1e6
 
130224f
 
 
 
885c1e6
 
 
e986bc1
 
 
885c1e6
 
e986bc1
 
 
885c1e6
 
2fa9362
e986bc1
 
 
 
 
 
 
 
f243335
 
 
e986bc1
 
f243335
 
 
 
e986bc1
f243335
 
 
e986bc1
f243335
 
 
e986bc1
f243335
e986bc1
 
 
 
 
 
 
 
 
f243335
e986bc1
 
 
 
 
 
 
 
 
 
 
 
 
 
2fa9362
885c1e6
e986bc1
 
 
 
2fa9362
e986bc1
885c1e6
e986bc1
2fa9362
 
885c1e6
 
 
 
2fa9362
885c1e6
 
 
e986bc1
 
 
 
 
 
 
2fa9362
885c1e6
 
 
 
 
 
2fa9362
885c1e6
2fa9362
885c1e6
 
e986bc1
2fa9362
 
e986bc1
f243335
e986bc1
f243335
885c1e6
 
 
2fa9362
e986bc1
885c1e6
2fa9362
e986bc1
 
 
 
 
 
 
 
885c1e6
e986bc1
 
885c1e6
f243335
 
885c1e6
 
 
 
2fa9362
f243335
2fa9362
e986bc1
 
2fa9362
885c1e6
 
2fa9362
e986bc1
 
 
885c1e6
 
2fa9362
130224f
e986bc1
 
 
885c1e6
2fa9362
885c1e6
2fa9362
885c1e6
f243335
e986bc1
f243335
e986bc1
 
 
885c1e6
 
 
 
 
e986bc1
 
885c1e6
 
 
2fa9362
e986bc1
 
 
885c1e6
e986bc1
885c1e6
2fa9362
885c1e6
 
2fa9362
885c1e6
 
 
 
 
 
 
e986bc1
 
885c1e6
2fa9362
885c1e6
 
e986bc1
885c1e6
 
2fa9362
 
130224f
885c1e6
e986bc1
130224f
 
 
 
 
 
 
 
 
 
 
 
 
 
885c1e6
2fa9362
885c1e6
 
 
 
2fa9362
885c1e6
 
 
 
 
 
e986bc1
 
885c1e6
 
e986bc1
885c1e6
f243335
e986bc1
885c1e6
 
2fa9362
885c1e6
 
 
 
2fa9362
e986bc1
 
 
885c1e6
 
e986bc1
885c1e6
e986bc1
 
 
 
 
 
2fa9362
69325aa
 
 
 
 
 
 
 
 
 
 
2fa9362
e986bc1
 
 
 
 
 
2fa9362
e986bc1
885c1e6
2fa9362
e986bc1
885c1e6
 
2fa9362
885c1e6
e986bc1
885c1e6
e986bc1
 
885c1e6
 
e986bc1
 
885c1e6
2fa9362
e986bc1
885c1e6
e986bc1
2fa9362
885c1e6
e986bc1
 
885c1e6
e986bc1
 
 
 
885c1e6
 
 
 
e986bc1
 
885c1e6
130224f
 
 
885c1e6
 
2fa9362
885c1e6
2fa9362
885c1e6
2fa9362
 
e986bc1
11067ff
 
 
 
 
 
 
 
 
2fa9362
885c1e6
 
 
2fa9362
885c1e6
 
 
2fa9362
885c1e6
 
 
 
 
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
import os
import sys
import subprocess

def _ensure_dependencies():
    try:
        import redis
        import playwright
    except ImportError:
        print("[-] Missing dependencies. Auto-installing 'redis' and 'playwright'...")
        subprocess.check_call([sys.executable, "-m", "pip", "install", "redis", "playwright"])
        print("[-] Installing Playwright chromium browser...")
        subprocess.check_call([sys.executable, "-m", "playwright", "install", "chromium"])
        print("[+] Installation complete. Restarting worker...")
        os.execv(sys.executable, [sys.executable] + sys.argv)

_ensure_dependencies()

import json
import logging
import signal
import asyncio
import uuid
import time
import socket
from typing import Any, Dict, List, Optional
import redis.asyncio as redis

# Playwright imports
from playwright.async_api import async_playwright, Page, Browser, BrowserContext

# -----------------------------------------------------------------------------
# Configuration
# -----------------------------------------------------------------------------
REDIS_HOST = os.getenv("REDIS_HOST", "54.244.160.21")
REDIS_PORT = int(os.getenv("REDIS_PORT", "6379"))
REDIS_PASSWORD = os.getenv("REDIS_PASSWORD", None)

LEAD_QUEUE        = "lead_queue"
PROCESSING_QUEUE  = "processing_queue"
RESULT_QUEUE      = "result_queue"
ACCOUNT_POOL_READY    = "account_pool:ready"
ACCOUNT_POOL_ASSIGNED = "account_pool:assigned"
ACCOUNT_POOL_PROCESSING = "account_pool:processing"
ACCOUNT_POOL_COOLDOWN = "account_pool:cooldown"
RETRY_COUNT_KEY   = "retry_count"

# Key that stores a SET of lead emails that are currently in-flight or already done.
# This is the deduplication guard: a lead email is added here before processing
# and removed only if it fails (so it can be retried). If it succeeds it stays
# forever (within the day) so it cannot be sent again.
SENT_LEADS_SET    = "leads:sent_today"

WORKER_TASKS    = int(os.getenv("WORKER_THREADS", "10"))
BATCH_SIZE      = 10          # leads per account session
DEFAULT_COOLDOWN_SECONDS = int(os.getenv("DEFAULT_COOLDOWN_SECONDS", "600"))  # 10 minutes between batches
RESET_SENT_DEDUP_DAILY = os.getenv("RESET_SENT_DEDUP_DAILY", "0").lower() in {"1", "true", "yes", "on"}
ADMIN_SETTINGS_FILE = os.getenv(
    "ADMIN_SETTINGS_FILE",
    r"C:\Users\PRADIP MUDI\Downloads\Telegram Desktop\lead_processor\admin_state.json"
)
MAX_RETRIES     = 3
POLL_TIMEOUT    = 5           # seconds to block on BRPOPLPUSH

logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s] %(message)s")
logger = logging.getLogger("worker")

_shutdown_requested = False

# Machine identity (1 machine = 1 worker in dashboard)
MACHINE_ID = socket.gethostname()

# Shared stats across all coroutines on this machine
_machine_stats = {
    "total_done": 0,
    "total_failed": 0,
    "threads": {},   # task_id -> current_action
}
_stats_lock = asyncio.Lock()

def _signal_handler(signum, frame):
    global _shutdown_requested
    logger.info("Shutdown signal received. Draining worker pool...")
    _shutdown_requested = True

signal.signal(signal.SIGINT,  _signal_handler)
signal.signal(signal.SIGTERM, _signal_handler)

_redis_pool = None

def get_redis_pool():
    global _redis_pool
    if _redis_pool is None:
        _redis_pool = redis.ConnectionPool(
            host=REDIS_HOST, port=REDIS_PORT, password=REDIS_PASSWORD,
            decode_responses=True, max_connections=50
        )
    return _redis_pool

async def get_cooldown_seconds(client: redis.Redis) -> int:
    try:
        raw = await client.get("config:account_cooldown_seconds")
        if raw is None:
            if os.path.exists(ADMIN_SETTINGS_FILE):
                with open(ADMIN_SETTINGS_FILE, "r", encoding="utf-8") as f:
                    settings = json.load(f).get("settings", {})
                raw = settings.get("cooldown_seconds")
            if raw is None:
                return DEFAULT_COOLDOWN_SECONDS
        value = int(raw)
        return max(60, min(value, 24 * 3600))
    except Exception as e:
        logger.warning(f"Could not read cooldown setting, using default: {e}")
        return DEFAULT_COOLDOWN_SECONDS

# -----------------------------------------------------------------------------
# Automation Engine
# -----------------------------------------------------------------------------
class AutomationEngine:
    """Robust Playwright automation engine"""

    def __init__(self):
        self.playwright = None
        self.browser: Optional[Browser] = None
        self.context: Optional[BrowserContext] = None
        self.page: Optional[Page] = None

    async def start(self, headless: bool = True):
        self.playwright = await async_playwright().start()
        browser_options = {
            "headless": headless,
            "args": [
                "--no-sandbox",
                "--disable-setuid-sandbox",
                "--disable-dev-shm-usage",
                "--disable-accelerated-2d-canvas",
                "--no-first-run",
                "--no-zygote",
                "--single-process",
                "--disable-gpu"
            ]
        }
        self.browser = await self.playwright.chromium.launch(**browser_options)
        
        # Use a realistic User-Agent to avoid "Headless" detection
        user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36"
        
        self.context = await self.browser.new_context(
            user_agent=user_agent,
            locale="en-US",
            timezone_id="UTC",
            viewport={"width": 1280, "height": 800}
        )
        self.page = await self.context.new_page()
        # Aggressive network blocking for super low bandwidth
        excluded_resource_types = {"image", "font", "media"}

        async def route_interceptor(route):
            if route.request.resource_type in excluded_resource_types:
                return await route.abort()
            return await route.continue_()

        await self.page.route("**/*", route_interceptor)

    async def stop(self):
        if self.context:
            await self.context.close()
        if self.browser:
            await self.browser.close()
        if self.playwright:
            await self.playwright.stop()

    async def login_microsoft(self, email: str, temp_pass: str) -> bool:
        """Robust Login using TAP (Temporary Access Pass) flow with branching logic"""
        try:
            logger.info(f"Logging in as {email} using TAP flow...")
            login_url = (
                "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?"
                "client_id=4765445b-32c6-49b0-83e6-1d93765276ca&"
                "redirect_uri=https%3A%2F%2Fwww.office.com%2Flandingv2&"
                "response_type=code%20id_token&"
                "scope=openid%20profile%20https%3A%2F%2Fwww.office.com%2Fv2%2FOfficeHome.All&"
                "response_mode=form_post"
            )
            await self.page.goto(login_url, timeout=60000)

            # 1. Email Step
            email_field = self.page.locator('input[type="email"], input[name="loginfmt"]')
            await email_field.wait_for(timeout=30000)
            await email_field.fill(email)
            await asyncio.sleep(1)
            
            # Click and also press Enter to be sure
            await self.page.locator('input[type="submit"], #idSIButton9').click()
            await self.page.keyboard.press("Enter")
            
            logger.info("Email submitted, waiting for redirect...")
            await asyncio.sleep(5)

            # 2. Detect next state (TAP vs Password vs Security)
            # Increase attempts to 10 for slower environments
            for attempt in range(10):
                curr_url = self.page.url
                logger.info(f"Login Progress (Attempt {attempt+1}): {curr_url}")

                # Case A: Temporary Access Pass field
                tap_field = self.page.locator('input[name="otc"], input[placeholder="Temporary Access Pass"]')
                if await tap_field.is_visible(timeout=3000):
                    logger.info("TAP field detected.")
                    await tap_field.fill(temp_pass)
                    await self.page.locator('input[type="submit"], #idSIButton9').click()
                    await self.page.keyboard.press("Enter")
                    break

                # Case B: Password field
                pwd_field = self.page.locator('input[type="password"]')
                if await pwd_field.is_visible(timeout=1000):
                    logger.warning("Microsoft asked for Password instead of TAP. Attempting to switch...")
                    
                    # 1. Try direct TAP link
                    tap_switch_selectors = [
                        'text="Use a Temporary Access Pass instead"',
                        '#idA_PWD_SwitchToOTC',
                        'a:has-text("Temporary Access Pass")'
                    ]
                    for sel in tap_switch_selectors:
                        link = self.page.locator(sel)
                        if await link.is_visible(timeout=2000):
                            logger.info(f"Switching to TAP via {sel}")
                            await link.click()
                            await asyncio.sleep(3)
                            continue
                    
                    # 2. Try "Other ways to sign in" or "Sign in another way"
                    other_ways_selectors = [
                        'text="Other ways to sign in"',
                        'text="Sign in another way"',
                        '#idA_PWD_SwitchToOther'
                    ]
                    for sel in other_ways_selectors:
                        link = self.page.locator(sel)
                        if await link.is_visible(timeout=2000):
                            logger.info(f"Opening '{sel}' to find TAP option.")
                            await link.click()
                            await asyncio.sleep(2)
                            
                            # Now look for TAP again
                            tap_link = self.page.locator('text="Temporary Access Pass"')
                            if await tap_link.is_visible(timeout=3000):
                                await tap_link.click()
                                await asyncio.sleep(3)
                                break
                    
                    # Re-check if we are now on TAP page
                    if await self.page.locator('input[placeholder="Temporary Access Pass"]').is_visible(timeout=2000):
                        continue
                        
                    logger.error("Could not find TAP option on password page.")
                    return False

                # Case C: "Stay signed in?"
                if "kmsi" in curr_url.lower() or await self.page.locator('text="Stay signed in?"').is_visible(timeout=1000):
                    logger.info("Handling 'Stay signed in?' prompt.")
                    await self.page.locator('input[type="submit"], #idSIButton9').click()
                    await self.page.keyboard.press("Enter")
                    break

                # Case D: Security / More info required
                if "proofup" in curr_url.lower() or await self.page.locator('text="More information required"').is_visible(timeout=1000):
                    logger.info("Handling 'More information required' prompt.")
                    await self.page.locator('input[type="submit"], #idSIButton9').click()
                    await self.page.keyboard.press("Enter")
                    await asyncio.sleep(5)
                    continue
                
                # Case E: Account Disambiguation (Work/School vs Personal)
                work_btn = self.page.locator('text="Work or school account"')
                if await work_btn.is_visible(timeout=1000):
                    logger.info("Selecting 'Work or school account'.")
                    await work_btn.click()
                    await asyncio.sleep(3)
                    continue

                # Fallback: Take screenshot if still on login page and stuck
                if attempt == 5 and "login.microsoftonline.com" in curr_url:
                    try:
                        os.makedirs("screenshots", exist_ok=True)
                        ss_path = f"screenshots/stuck_login_{email.split('@')[0]}.png"
                        await self.page.screenshot(path=ss_path)
                        logger.warning(f"Login stuck. Diagnostic screenshot saved to {ss_path}")
                    except: pass

                await asyncio.sleep(3)

            # 3. Final verification and MFA Method Selection
            await asyncio.sleep(5)
            
            # Check for incorrect TAP
            if await self.page.locator('text="Your Temporary Access Pass is incorrect."').is_visible(timeout=2000):
                logger.error(f"Incorrect Temporary Access Pass for {email}")
                return False

            # Case: "Keep your account secure" / "More information required"
            if "proofup" in self.page.url.lower() or await self.page.get_by_role("heading", name="Let's keep your account secure").is_visible(timeout=5000):
                logger.info("On security setup page. Clicking Next...")
                await self.page.get_by_role("button", name="Next").click()
                await asyncio.sleep(5)

            # 4. Force "Email" method for lead processing
            logger.info(f"Attempting to select 'Email' as MFA method... (URL: {self.page.url})")
            try:
                # If we are on mysignins, we might need to click "Add method" or select from list
                if "mysignins" in self.page.url.lower():
                    logger.info(f"Detected MySignins environment. URL: {self.page.url}")
                    
                    # Wait for page to load more than just "Skip to main content"
                    for load_attempt in range(10):
                        elements = await self.page.locator("button, a").all_inner_texts()
                        if len(elements) > 1:
                            logger.info(f"MySignins elements loaded: {elements}")
                            break
                        logger.info("Waiting for MySignins buttons to appear...")
                        await asyncio.sleep(3)
                    
                    # Log final state for diagnostics
                    try:
                        os.makedirs("screenshots", exist_ok=True)
                        ss_path = f"screenshots/mysignins_{email.split('@')[0]}.png"
                        await self.page.screenshot(path=ss_path)
                    except: pass

                    # Try to find Email method button directly on page
                    email_btns = [
                        'button:has-text("Email")',
                        '[data-testid="email-method-button"]',
                        'text="Email Receive a code to reset"',
                        'button:has-text("Receive a code to reset")',
                        'div[role="button"]:has-text("Email")'
                    ]
                    for sel in email_btns:
                        btn = self.page.locator(sel)
                        if await btn.is_visible(timeout=2000):
                            logger.info(f"Clicking Email method via {sel}")
                            await btn.click()
                            await asyncio.sleep(5)
                            break
                    
                    # If it says "I want to set up a different method" or "Set up a different way to sign in"
                    diff_method_selectors = [
                        'text="I want to set up a different method"',
                        'text="Set up a different way to sign in"',
                        'button:has-text("different way")',
                        'button:has-text("different method")'
                    ]
                    for sel in diff_method_selectors:
                        btn = self.page.locator(sel)
                        if await btn.is_visible(timeout=2000):
                            logger.info(f"Clicking transition link: {sel}")
                            await btn.click()
                            await asyncio.sleep(3)
                            break

                # Look for "Choose different method" link if not already on the right page
                mfa_selectors = [
                    'button[data-testid="choose-different-method-link"]',
                    'text="I want to set up a different method"',
                    'text="Set up a different way to sign in"',
                    'a:has-text("different method")',
                    '#otherWaysLink',
                    'text="I want to use a different method"'
                ]
                
                found_method_link = False
                for sel in mfa_selectors:
                    btn = self.page.locator(sel)
                    if await btn.is_visible(timeout=3000):
                        logger.info(f"Found MFA method link: {sel}")
                        await btn.click()
                        found_method_link = True
                        await asyncio.sleep(3)
                        break
                
                if found_method_link:
                    # Look for Email option in the resulting list
                    email_opt_selectors = [
                        'button:has-text("Email")',
                        '[data-testid="email-method-button"]',
                        'text="Email Receive a code to reset"',
                        'div[role="button"]:has-text("Email")'
                    ]
                    for sel in email_opt_selectors:
                        opt = self.page.locator(sel)
                        if await opt.is_visible(timeout=5000):
                            logger.info(f"Selecting Email method from list via {sel}")
                            await opt.click()
                            await asyncio.sleep(5)
                            break
                else:
                    logger.warning("Could not find 'Choose different method' link.")
                    # Final attempt to find any 'Email' button on current page
                    email_btn = self.page.locator('button:has-text("Email"), [data-testid="email-method-button"]')
                    if await email_btn.is_visible(timeout=2000):
                        logger.info("Found Email button as fallback. Clicking.")
                        await email_btn.click()
                        await asyncio.sleep(5)

            except Exception as e:
                logger.warning(f"Error during MFA method selection: {e}")

            logger.info(f"Login flow completed for {email}. Final URL: {self.page.url}")
            return True
        except Exception as e:
            logger.error(f"Login failed for {email}: {e}")
            try:
                os.makedirs("screenshots", exist_ok=True)
                await self.page.screenshot(path=f"screenshots/login_fail_{email.split('@')[0]}.png")
            except: pass
            return False

    async def process_lead(self, email: str) -> tuple[bool, Optional[str]]:
        """Process a single lead OTP flow"""
        try:
            # Click Email method if visible
            email_btn = self.page.locator('button:has-text("Email")')
            if await email_btn.is_visible(timeout=10000):
                await email_btn.click()

            # Fill lead email
            email_input = self.page.locator('[data-testid="email-input"]')
            await email_input.wait_for(timeout=60000)
            await email_input.fill(email)
            await self.page.locator('[data-testid="reskin-step-next-button"]').click(timeout=60000)

            # Wait for OTP or Error
            await self.page.wait_for_selector(
                '[data-testid="email-verify-challenge-otp-input"], [data-testid="message-bar-error"]',
                timeout=60000
            )

            if await self.page.locator('[data-testid="message-bar-error"]').is_visible():
                text = await self.page.locator('[data-testid="message-bar-error"]').inner_text()
                if "too many" in text.lower():
                    return False, "rate_limit"
                return False, text

            if await self.page.locator('[data-testid="email-verify-challenge-otp-input"]').is_visible():
                await self.page.locator('[data-testid="backButton"]').click()
                logger.info(f"OTP Flow Success: {email}")
                return True, None

            return False, "otp_not_found"
        except Exception as e:
            try:
                os.makedirs("screenshots", exist_ok=True)
                await self.page.screenshot(path=f"screenshots/lead_fail_{email.split('@')[0]}.png")
            except: pass
            return False, str(e)


# -----------------------------------------------------------------------------
# Worker Async Task Logic
# -----------------------------------------------------------------------------
class WorkerTask:
    def __init__(self, task_id: str):
        self.task_id   = task_id
        self.client    = redis.Redis(connection_pool=get_redis_pool())
        self.leads_done   = 0
        self.leads_failed = 0
        self.current_action = "Starting"

    # ------------------------------------------------------------------
    # Fetch exactly BATCH_SIZE UNIQUE leads from the queue.
    # Uses a Redis SET (SENT_LEADS_SET) to skip leads already processed
    # today.  Each lead that passes the dedup check is atomically claimed
    # via SETNX so no two threads can process the same email at once.
    # ------------------------------------------------------------------
    async def _fetch_unique_batch(self, account_email: str) -> List[Dict[str, Any]]:
        """
        Pops leads from the isolated account_queue provided by the Assigner.
        Global and Account-level dedup are already handled by Assigner, 
        but we still check locally as a secondary guard.
        """
        unique_leads = []
        account_queue = f"account_queue:{account_email}"
        used_key = f"used:{account_email}"
        skipped = 0
        
        try:
            # We pop up to BATCH_SIZE items from the account queue
            raw_leads = await self.client.lrange(account_queue, 0, BATCH_SIZE - 1)
            if not raw_leads:
                return []
                
            # Remove them from the list
            await self.client.ltrim(account_queue, len(raw_leads), -1)

            for raw_lead in raw_leads:
                try:
                    data = json.loads(raw_lead)
                except Exception:
                    continue

                lead_email = data.get("email", "").strip().lower()
                if not lead_email:
                    continue

                # Secondary dedup guard
                already_sent = await self.client.sismember(used_key, lead_email)
                if already_sent:
                    skipped += 1
                    logger.debug(f"Skipping lead {lead_email} - already used by {account_email}")
                    continue

                # Claim this lead for this account
                claimed = await self.client.sadd(used_key, lead_email)
                if not claimed:
                    skipped += 1
                    continue

                data["email"] = lead_email
                unique_leads.append({"_raw": raw_lead, "data": data})

        except Exception as e:
            logger.error(f"Error fetching unique batch: {e}")

        if skipped:
            logger.info(f"[Dedup] Skipped {skipped} duplicate leads in this batch fetch.")

        return unique_leads

    async def _cleanup_and_push_results(self, leads: List[Dict[str, Any]], results: List[Dict[str, Any]], account_email: str):
        if not leads:
            return
        try:
            pipe = self.client.pipeline()
            for lead, result in zip(leads, results):
                pipe.lpush(RESULT_QUEUE, json.dumps(result))

                lead_id = lead["data"].get("id", str(uuid.uuid4()))
                if result["success"]:
                    pipe.hdel(RETRY_COUNT_KEY, lead_id)
                else:
                    # On failure, un-claim from SENT_LEADS_SET so it can be
                    # retried next time (unless max retries exceeded).
                    retries = int(result.get("_retries", 0))
                    if retries < MAX_RETRIES:
                        lead_email = lead["data"].get("email", "")
                        pipe.srem(f"used:{account_email}", lead_email)

            await pipe.execute()
        except Exception as e:
            logger.error(f"Error in cleanup and push: {e}")

    async def run(self):
        logger.info(f"Task {self.task_id} started. Batch Size: {BATCH_SIZE}")

        while not _shutdown_requested:
            # Update shared machine stats
            async with _stats_lock:
                _machine_stats["threads"][self.task_id] = self.current_action
                _machine_stats["total_done"]   = sum(t.leads_done   for t in _all_worker_tasks)
                _machine_stats["total_failed"] = sum(t.leads_failed for t in _all_worker_tasks)

            # ----------------------------------------------------------
            # 1. Lease ONE account from Assigned pool (atomic BRPOPLPUSH)
            # ----------------------------------------------------------
            email = await self.client.brpoplpush(ACCOUNT_POOL_ASSIGNED, ACCOUNT_POOL_PROCESSING, timeout=5)
            if not email:
                self.current_action = "Idle"
                continue

            acc_key  = f"account:{email}"
            acc_pass = await self.client.hget(acc_key, "temp_pass")

            if not acc_pass:
                logger.warning(f"Account {email} has no temp_pass. Moving to pending_refresh.")
                await self.client.sadd("account_pool:pending_refresh", email)
                continue

            # ----------------------------------------------------------
            # 2. Fetch up to 10 UNIQUE leads
            # ----------------------------------------------------------
            self.current_action = "Fetching Leads"
            batch = await self._fetch_unique_batch(email)

            if not batch:
                # No leads — return account to ready pool and clean up assignment
                await self.client.lrem(ACCOUNT_POOL_PROCESSING, -1, email)
                await self.client.sadd(ACCOUNT_POOL_READY, email)
                self.current_action = "Idle"
                await asyncio.sleep(1)
                continue

            logger.info(f"Task {self.task_id}: Account={email}, Leads in account_queue={len(batch)}")

            results = []
            engine  = AutomationEngine()

            try:
                await engine.start(headless=True)

                # ----------------------------------------------------------
                # 3. Login once per account session
                # ----------------------------------------------------------
                self.current_action = f"Logging in: {email}"
                login_success = await engine.login_microsoft(email, acc_pass)

                for item_idx, item in enumerate(batch):
                    lead_data  = item["data"]
                    lead_id    = lead_data.get("id", str(uuid.uuid4()))
                    raw_data   = item["_raw"]
                    lead_email = lead_data.get("email")

                    self.current_action = f"Processing: {lead_email}"

                    if not login_success:
                        # Login failed — requeue lead to lead_queue, unclaim, and abort account
                        pipe = self.client.pipeline()
                        pipe.lpush(LEAD_QUEUE, raw_data)
                        pipe.srem(f"used:{email}", lead_email)   # ← un-claim so it can be retried
                        await pipe.execute()

                        results.append({
                            "lead_id": lead_id,
                            "success": False,
                            "error": "login_failed",
                            "worker_id": self.task_id,
                            "row_number": lead_data.get("row_number"),
                            "_skip": True,
                        })
                        item["_skip"] = True
                        continue

                    # --------------------------------------------------
                    # 4. Process each lead
                    # --------------------------------------------------
                    try:
                        retries    = int(await self.client.hget(RETRY_COUNT_KEY, lead_id) or 0)
                        start_time = time.time()

                        success, err = await engine.process_lead(lead_email)
                        elapsed = (time.time() - start_time) * 1000

                        results.append({
                            "lead_id": lead_id,
                            "success": success,
                            "output": {"error": err} if not success else {},
                            "error": err if not success else None,
                            "processing_time_ms": elapsed,
                            "worker_id": self.task_id,
                            "row_number": lead_data.get("row_number"),
                            "_retries": retries,
                        })

                        if success:
                            self.leads_done += 1
                            logger.info(f"[+] Sent lead: {lead_email} via {email}")
                        else:
                            self.leads_failed += 1
                            logger.warning(f"[-] Failed lead: {lead_email}. Reason: {err}")

                        abort_batch = False
                        if err == "rate_limit":
                            logger.warning("Rate limit hit — stopping batch early.")
                            abort_batch = True
                        elif err and "Timeout" in str(err):
                            logger.warning("Page timeout detected (broken page) — stopping batch early.")
                            abort_batch = True

                        if abort_batch:
                            # Requeue remaining leads in the batch so they aren't wasted/lost
                            if item_idx + 1 < len(batch):
                                pipe = self.client.pipeline()
                                for future_item in batch[item_idx + 1:]:
                                    pipe.lpush(LEAD_QUEUE, future_item["_raw"])
                                    pipe.srem(f"used:{email}", future_item["data"].get("email", ""))
                                    future_item["_skip"] = True
                                await pipe.execute()
                            break

                    except Exception as e:
                        logger.error(f"Error processing lead {lead_id}: {e}")
                        retries += 1
                        await self.client.hset(RETRY_COUNT_KEY, lead_id, retries)

                        if retries >= MAX_RETRIES:
                            results.append({
                                "lead_id": lead_id,
                                "success": False,
                                "error": str(e),
                                "worker_id": self.task_id,
                                "row_number": lead_data.get("row_number"),
                                "_retries": retries,
                            })
                        else:
                            # Requeue and un-claim
                            pipe = self.client.pipeline()
                            pipe.lpush(LEAD_QUEUE, raw_data)
                            pipe.srem(f"used:{email}", lead_email)
                            await pipe.execute()
                            item["_skip"] = True

            except Exception as e:
                logger.error(f"Engine exception: {e}")
            finally:
                await engine.stop()

            # ----------------------------------------------------------
            # 5. Account routing after batch
            # ----------------------------------------------------------
            if not login_success:
                await self.client.sadd("account_pool:pending_refresh", email)
                logger.info(f"Account {email} login failed → pending_refresh.")
            else:
                # Count only leads that were actually attempted (not skipped/requeued)
                actually_sent = sum(
                    1 for r in results
                    if not r.get("_skip") and r["success"]
                )
                leads_sent = await self.client.hincrby(acc_key, "leads_sent_today", actually_sent)

                now = time.time()
                cooldown_seconds = await get_cooldown_seconds(self.client)
                pipe = self.client.pipeline()
                pipe.hset(acc_key, "cooldown_until", now + cooldown_seconds)
                pipe.sadd(ACCOUNT_POOL_COOLDOWN, email)
                pipe.srem("account_pool:done_for_today", email)
                await pipe.execute()
                logger.info(
                    f"Account {email} batch done ({leads_sent} leads total) "
                    f"-> Cooldown {round(cooldown_seconds / 60, 1)}m."
                )

            # ----------------------------------------------------------
            # 6. Push results to result_queue and clean processing_queue
            # ----------------------------------------------------------
            final_batch   = [b for b in batch  if not b.get("_skip")]
            valid_ids     = {b["data"].get("id") for b in final_batch}
            final_results = [r for r in results if r.get("lead_id") in valid_ids and not r.get("_skip")]

            await self._cleanup_and_push_results(final_batch, final_results, email)


# Global list of all WorkerTask instances on this machine
_all_worker_tasks: list["WorkerTask"] = []


async def machine_heartbeat_loop():
    """Sends ONE heartbeat key for the whole machine every 5s."""
    client = redis.Redis(connection_pool=get_redis_pool())
    key    = f"worker:machine:{MACHINE_ID}"

    while not _shutdown_requested:
        async with _stats_lock:
            done    = _machine_stats["total_done"]
            failed  = _machine_stats["total_failed"]
            threads = dict(_machine_stats["threads"])

        total        = done + failed
        success_rate = round((done / total * 100), 1) if total > 0 else 0
        busy_count   = sum(1 for a in threads.values() if a not in ("Idle", "Starting"))

        payload = {
            "id":             MACHINE_ID,
            "status":         "busy" if busy_count > 0 else "online",
            "active_threads": busy_count,
            "max_threads":    WORKER_TASKS,
            "leads_done":     done,
            "leads_failed":   failed,
            "success_rate":   success_rate,
            "current_action": next(
                (a for a in threads.values() if a not in ("Idle", "Starting")),
                "Idle"
            ),
            "version":   "3.0.0",
            "last_seen": time.time(),
        }
        blob = json.dumps(payload)
        await client.setex(key, 30, blob)
        await client.set(f"{key}:snapshot", blob)
        await asyncio.sleep(5)


async def main():
    logger.info("=" * 50)
    logger.info(f"Starting Async Playwright Worker [{MACHINE_ID}]. Tasks: {WORKER_TASKS}")
    logger.info("=" * 50)

    client = redis.Redis(connection_pool=get_redis_pool())
    if RESET_SENT_DEDUP_DAILY:
        import datetime
        now = datetime.datetime.now(datetime.timezone.utc)
        tomorrow = now.replace(hour=0, minute=0, second=0, microsecond=0) + datetime.timedelta(days=1)
        ttl = int((tomorrow - now).total_seconds())
        await client.expire(SENT_LEADS_SET, ttl)
        logger.info(f"SENT_LEADS_SET TTL set to {ttl}s (expires at UTC midnight).")
    else:
        logger.info("SENT_LEADS_SET daily auto-reset disabled. Worker state stays stable across resets.")

    for i in range(WORKER_TASKS):
        t = WorkerTask(f"worker-{uuid.uuid4().hex[:6]}")
        _all_worker_tasks.append(t)

    task_coroutines = [t.run() for t in _all_worker_tasks]
    await asyncio.gather(machine_heartbeat_loop(), *task_coroutines)


if __name__ == "__main__":
    try:
        asyncio.run(main())
    except KeyboardInterrupt:
        logger.info("Worker node shutting down.")