-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
528 lines (430 loc) · 18.4 KB
/
Copy pathtasks.py
File metadata and controls
528 lines (430 loc) · 18.4 KB
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
"""
Background tasks for asynchronous processing using Celery.
"""
import os
import logging
import random
import uuid
import threading
from typing import List, Dict, Any
from datetime import datetime, timezone, date, timedelta
from celery import Task, chord, group
from celery.exceptions import SoftTimeLimitExceeded
from supabase import create_client, Client
from celery_config import celery
from utils.scraper import scrape_followers
from utils.gender import detect_gender, filter_by_gender
from utils.batch_processor import batch_insert_profiles, batch_update_assignments
logger = logging.getLogger(__name__)
# ===================================================================
# SUPABASE CLIENT WITH CONNECTION POOLING (THREAD-SAFE)
# ===================================================================
# Use singleton pattern with thread lock to prevent race conditions
_supabase_client = None
_supabase_lock = threading.Lock()
def get_supabase_client() -> Client:
"""
Initialize and return Supabase client.
OPTIMIZED FOR 500K+ SCALE:
- Thread-safe singleton pattern to reuse client across Celery tasks
- Connection pooling with configurable limits
- Prevents connection exhaustion in background workers
"""
global _supabase_client
# Double-checked locking pattern for thread safety
if _supabase_client is not None:
return _supabase_client
with _supabase_lock:
# Check again inside lock (another thread might have initialized it)
if _supabase_client is not None:
return _supabase_client
# Initialize new client
supabase_url = os.getenv('SUPABASE_URL')
supabase_key = os.getenv('SUPABASE_SERVICE_ROLE_KEY')
if not supabase_url or not supabase_key:
raise ValueError("SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY environment variables are required.")
# Configure connection pooling for scale
# Free Tier: Conservative pooling for Celery workers
# Default: 5 connections per worker (safe for free tier)
pool_size = int(os.getenv('SUPABASE_POOL_SIZE', '5'))
_supabase_client = create_client(
supabase_url,
supabase_key,
options={
'db': {
'schema': 'public'
},
'auth': {
'auto_refresh_token': False,
'persist_session': False
},
'global': {
'headers': {
'x-client-info': 'instagram-scraper-celery/1.0'
}
}
}
)
logger.info(f"✓ Supabase client initialized in Celery worker (pool: {pool_size}, tier: free)")
return _supabase_client
return _supabase_client
class BaseTask(Task):
"""Base task with error handling and retry logic."""
autoretry_for = (Exception,)
retry_kwargs = {'max_retries': 3}
retry_backoff = True
retry_backoff_max = 600 # 10 minutes
retry_jitter = True
@celery.task(base=BaseTask, bind=True, name='tasks.scrape_account_batch')
def scrape_account_batch(
self,
job_id: str,
accounts: List[str],
target_gender: str = 'male',
max_per_account: int = 5,
batch_number: int = 1,
base_id: str = None, # Multi-tenant isolation
platform: str = 'instagram' # ADDED: Platform support
) -> Dict[str, Any]:
"""
Scrape a batch of accounts (max 50) and return filtered profiles.
OPTIMIZED FOR 500K+ SCALE WITH MULTI-TENANT & MULTI-PLATFORM SUPPORT:
- RLS context set for tenant isolation
- Platform-aware scraping (Instagram, TikTok, Threads, X)
- Retry logic for Apify failures
- Memory-efficient processing
Args:
job_id: Unique job identifier
accounts: List of account usernames (max 50)
target_gender: Target gender filter ('male' or 'female')
max_per_account: Max followers to scrape per account
batch_number: Batch number for logging
base_id: Multi-tenant identifier for RLS (REQUIRED)
platform: Social media platform (instagram, threads, tiktok, x)
Returns:
Dictionary with scraped and filtered profiles
"""
try:
logger.info(f"[Job {job_id}] Batch {batch_number}: Scraping {len(accounts)} {platform} accounts (base_id={base_id})")
# Set RLS context for multi-tenant isolation
if base_id:
from utils.rls_context import set_rls_context
set_rls_context(base_id)
logger.info(f"[Job {job_id}] RLS context set for base_id={base_id}")
else:
logger.warning(f"[Job {job_id}] No base_id provided - RLS not set!")
# Update job progress
supabase = get_supabase_client()
# Step 1: Scrape followers (with platform support)
followers = scrape_followers(accounts, max_per_account, platform=platform)
logger.info(f"[Job {job_id}] Batch {batch_number}: Scraped {len(followers)} followers from {platform}")
# Step 2: Detect gender
followers_gender = detect_gender(followers)
# Step 3: Filter by target gender
filtered_followers = filter_by_gender(followers_gender, target_gender)
logger.info(f"[Job {job_id}] Batch {batch_number}: Filtered to {len(filtered_followers)} profiles")
# Step 4: Format profiles for return
complete_profiles = []
for username in filtered_followers.keys():
follower_info = followers.get(username, {})
complete_profiles.append({
'id': follower_info.get('id', username),
'username': username,
'full_name': follower_info.get('full_name', ''),
})
# Update job progress
try:
# Increment profiles_scraped count
job = supabase.table('scrape_jobs')\
.select('profiles_scraped')\
.eq('job_id', job_id)\
.execute()
current_count = job.data[0]['profiles_scraped'] if job.data else 0
new_count = current_count + len(complete_profiles)
supabase.table('scrape_jobs')\
.update({
'profiles_scraped': new_count,
'updated_at': datetime.now(timezone.utc).isoformat()
})\
.eq('job_id', job_id)\
.execute()
except Exception as e:
logger.warning(f"Failed to update job progress: {str(e)}")
return {
'job_id': job_id,
'batch_number': batch_number,
'profiles': complete_profiles,
'total_scraped': len(followers),
'total_filtered': len(complete_profiles)
}
except SoftTimeLimitExceeded:
logger.error(f"[Job {job_id}] Batch {batch_number}: Task exceeded time limit")
raise
except Exception as e:
logger.error(f"[Job {job_id}] Batch {batch_number}: Error - {str(e)}")
# Update job with error
try:
supabase = get_supabase_client()
supabase.table('scrape_jobs')\
.update({
'status': 'failed',
'error_message': str(e),
'updated_at': datetime.now(timezone.utc).isoformat()
})\
.eq('job_id', job_id)\
.execute()
except:
pass
raise
@celery.task(base=BaseTask, bind=True, name='tasks.aggregate_scrape_results')
def aggregate_scrape_results(self, batch_results: List[Dict], job_id: str, base_id: str = None) -> Dict[str, Any]:
"""
Aggregate results from all batch tasks and store in database.
Args:
batch_results: List of results from scrape_account_batch tasks
job_id: Unique job identifier
base_id: Multi-tenant identifier for RLS (REQUIRED)
Returns:
Summary of aggregation
"""
try:
logger.info(f"[Job {job_id}] Aggregating results from {len(batch_results)} batches (base_id={base_id})")
# Set RLS context for multi-tenant isolation
if base_id:
from utils.rls_context import set_rls_context
set_rls_context(base_id)
supabase = get_supabase_client()
# Combine all profiles from batches
all_profiles = []
total_scraped = 0
total_filtered = 0
for result in batch_results:
if result and 'profiles' in result:
all_profiles.extend(result['profiles'])
total_scraped += result.get('total_scraped', 0)
total_filtered += result.get('total_filtered', 0)
logger.info(f"[Job {job_id}] Total profiles: {len(all_profiles)}, Scraped: {total_scraped}, Filtered: {total_filtered}")
# Batch insert into scrape_results (chunks of 1000)
batch_size = 1000
inserted_count = 0
for i in range(0, len(all_profiles), batch_size):
batch = all_profiles[i:i + batch_size]
# Prepare records for insertion
records = []
for profile in batch:
records.append({
'job_id': job_id,
'profile_id': profile['id'],
'username': profile['username'],
'full_name': profile.get('full_name', ''),
'created_at': datetime.now(timezone.utc).isoformat()
})
# Insert batch
try:
supabase.table('scrape_results').insert(records).execute()
inserted_count += len(records)
logger.info(f"[Job {job_id}] Inserted batch: {inserted_count}/{len(all_profiles)}")
except Exception as e:
logger.error(f"[Job {job_id}] Failed to insert batch: {str(e)}")
# Update job status to completed
supabase.table('scrape_jobs')\
.update({
'status': 'completed',
'total_scraped': total_scraped,
'total_filtered': total_filtered,
'profiles_scraped': len(all_profiles),
'progress': 100.0,
'completed_at': datetime.now(timezone.utc).isoformat(),
'updated_at': datetime.now(timezone.utc).isoformat()
})\
.eq('job_id', job_id)\
.execute()
logger.info(f"[Job {job_id}] Aggregation complete: {inserted_count} profiles stored")
return {
'job_id': job_id,
'total_profiles': len(all_profiles),
'total_scraped': total_scraped,
'total_filtered': total_filtered,
'inserted': inserted_count
}
except Exception as e:
logger.error(f"[Job {job_id}] Aggregation error: {str(e)}")
# Update job with error
try:
supabase = get_supabase_client()
supabase.table('scrape_jobs')\
.update({
'status': 'failed',
'error_message': f"Aggregation failed: {str(e)}",
'updated_at': datetime.now(timezone.utc).isoformat()
})\
.eq('job_id', job_id)\
.execute()
except:
pass
raise
@celery.task(base=BaseTask, bind=True, name='tasks.ingest_profiles_batch')
def ingest_profiles_batch(
self,
batch_id: str,
profiles: List[Dict],
batch_number: int = 1,
base_id: str = None # ADDED: Multi-tenant isolation
) -> Dict[str, Any]:
"""
Ingest a batch of profiles into Supabase (max 1000).
Args:
batch_id: Unique batch identifier
profiles: List of profile dictionaries
batch_number: Batch number for logging
base_id: Multi-tenant identifier for RLS (REQUIRED)
Returns:
Summary of ingestion
"""
try:
logger.info(f"[Batch {batch_id}] #{batch_number}: Ingesting {len(profiles)} profiles (base_id={base_id})")
# Set RLS context for multi-tenant isolation
if base_id:
from utils.rls_context import set_rls_context
set_rls_context(base_id)
supabase = get_supabase_client()
# Use batch processor for insertion
# Pass base_id to batch processor
if not base_id:
logger.error(f"[Batch {batch_id}] No base_id provided for ingestion!")
raise ValueError("base_id is required for profile ingestion")
inserted_raw, added_to_global, skipped = batch_insert_profiles(
supabase,
profiles,
base_id=base_id, # FIXED: Now passing base_id
batch_size=500
)
logger.info(f"[Batch {batch_id}] #{batch_number}: Complete - raw={inserted_raw}, global={added_to_global}, skipped={skipped}")
return {
'batch_id': batch_id,
'batch_number': batch_number,
'inserted_raw': inserted_raw,
'added_to_global': added_to_global,
'skipped': skipped
}
except Exception as e:
logger.error(f"[Batch {batch_id}] #{batch_number}: Error - {str(e)}")
raise
@celery.task(base=BaseTask, bind=True, name='tasks.daily_pipeline_orchestrator')
def daily_pipeline_orchestrator(
self,
campaign_date: str = None,
profiles_per_table: int = 180,
base_id: str = None # ADDED: Multi-tenant isolation
) -> Dict[str, Any]:
"""
Orchestrate the entire daily pipeline: selection, distribution, sync, cleanup.
Args:
campaign_date: Campaign date (YYYY-MM-DD) or None for today
profiles_per_table: Number of profiles per VA table
base_id: Multi-tenant identifier for RLS (REQUIRED)
Returns:
Summary of pipeline execution
"""
try:
logger.info(f"Starting daily pipeline orchestration (base_id={base_id})")
# Set RLS context for multi-tenant isolation
if base_id:
from utils.rls_context import set_rls_context
set_rls_context(base_id)
supabase = get_supabase_client()
# Parse campaign date
if campaign_date:
campaign_date_obj = datetime.strptime(campaign_date, '%Y-%m-%d').date()
else:
campaign_date_obj = date.today()
num_va_tables = int(os.getenv('NUM_VA_TABLES', 80))
target_count = num_va_tables * profiles_per_table
logger.info(f"Pipeline config: date={campaign_date_obj}, target={target_count}")
# === STEP 1: DAILY SELECTION ===
logger.info("Step 1: Daily Selection")
campaign_id = str(uuid.uuid4())
supabase.table('campaigns').insert({
'campaign_id': campaign_id,
'campaign_date': campaign_date_obj.isoformat(),
'total_assigned': 0,
'status': False,
'created_at': datetime.now(timezone.utc).isoformat()
}).execute()
# Select unused profiles
available_profiles = supabase.table('global_usernames')\
.select('id, username, full_name')\
.eq('used', False)\
.limit(target_count)\
.execute()
if not available_profiles.data:
raise Exception("No unused profiles available")
selected_profiles = available_profiles.data
total_selected = len(selected_profiles)
# Mark as used
for profile in selected_profiles:
supabase.table('global_usernames')\
.update({
'used': True,
'used_at': datetime.now(timezone.utc).isoformat()
})\
.eq('id', profile['id'])\
.execute()
# Create placeholder assignments
assignments = []
for profile in selected_profiles:
assignments.append({
'assignment_id': str(uuid.uuid4()),
'campaign_id': campaign_id,
'va_table_number': 0,
'position': 0,
'id': profile['id'],
'username': profile['username'],
'full_name': profile['full_name'],
'status': 'pending',
'assigned_at': datetime.now(timezone.utc).isoformat()
})
supabase.table('daily_assignments').insert(assignments).execute()
supabase.table('campaigns')\
.update({'total_assigned': total_selected})\
.eq('campaign_id', campaign_id)\
.execute()
logger.info(f"Step 1 complete: {total_selected} profiles selected")
# === STEP 2: DISTRIBUTION ===
logger.info("Step 2: Distribution")
# Shuffle assignments
random.shuffle(assignments)
# Assign to VA tables
distributed_count = 0
current_table = 1
current_position = 1
for assignment in assignments:
supabase.table('daily_assignments')\
.update({
'va_table_number': current_table,
'position': current_position
})\
.eq('assignment_id', assignment['assignment_id'])\
.execute()
distributed_count += 1
current_position += 1
if current_position > profiles_per_table:
current_position = 1
current_table += 1
if current_table > num_va_tables:
break
logger.info(f"Step 2 complete: {distributed_count} profiles distributed")
# === STEP 3: AIRTABLE SYNC ===
# Note: Airtable sync moved to separate endpoint for reliability
# This can be called via /api/airtable-sync/<campaign_id>
logger.info("Pipeline complete (Airtable sync should be called separately)")
return {
'success': True,
'campaign_id': campaign_id,
'selected': total_selected,
'distributed': distributed_count,
'campaign_date': campaign_date_obj.isoformat()
}
except Exception as e:
logger.error(f"Pipeline orchestration error: {str(e)}")
raise