2012-07-16 23:40:17 -07:00
|
|
|
import datetime
|
2012-12-10 11:37:35 -08:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import time
|
2010-08-31 18:07:47 -04:00
|
|
|
from celery.task import Task
|
2011-04-11 21:57:45 -04:00
|
|
|
from utils import log as logging
|
2012-12-10 11:40:35 -08:00
|
|
|
from utils import s3_utils as s3
|
2011-11-04 09:45:10 -07:00
|
|
|
from django.conf import settings
|
2010-08-31 18:07:47 -04:00
|
|
|
|
2012-07-16 23:40:17 -07:00
|
|
|
class TaskFeeds(Task):
|
|
|
|
name = 'task-feeds'
|
|
|
|
|
|
|
|
def run(self, **kwargs):
|
|
|
|
from apps.rss_feeds.models import Feed
|
|
|
|
settings.LOG_TO_STREAM = True
|
|
|
|
now = datetime.datetime.utcnow()
|
|
|
|
|
|
|
|
# Active feeds
|
|
|
|
feeds = Feed.objects.filter(
|
|
|
|
next_scheduled_update__lte=now,
|
|
|
|
active=True
|
|
|
|
).exclude(
|
|
|
|
active_subscribers=0
|
|
|
|
).order_by('?')
|
2012-12-25 12:08:17 -08:00
|
|
|
active_count = feeds.count()
|
2012-07-16 23:40:17 -07:00
|
|
|
|
|
|
|
# Mistakenly inactive feeds
|
|
|
|
day = now - datetime.timedelta(days=1)
|
2012-12-25 14:58:49 -08:00
|
|
|
inactive_feeds = Feed.objects.filter(
|
2012-07-16 23:40:17 -07:00
|
|
|
last_update__lte=day,
|
|
|
|
queued_date__lte=day,
|
|
|
|
min_to_decay__lte=60*24,
|
2012-08-09 21:18:45 -07:00
|
|
|
active_subscribers__gte=1
|
|
|
|
).order_by('?')[:20]
|
2012-12-25 15:11:15 -08:00
|
|
|
inactive_count = inactive_feeds.count()
|
2012-07-16 23:40:17 -07:00
|
|
|
|
|
|
|
week = now - datetime.timedelta(days=7)
|
2012-12-25 14:58:49 -08:00
|
|
|
old_feeds = Feed.objects.filter(
|
2012-07-16 23:40:17 -07:00
|
|
|
last_update__lte=week,
|
|
|
|
queued_date__lte=day,
|
2012-08-09 21:18:45 -07:00
|
|
|
active_subscribers__gte=1
|
|
|
|
).order_by('?')[:20]
|
2012-12-25 14:58:49 -08:00
|
|
|
old_count = old_feeds.count()
|
2012-12-25 12:08:17 -08:00
|
|
|
|
2012-12-25 15:11:15 -08:00
|
|
|
logging.debug(" ---> ~FBTasking ~SB~FC%s~SN~FB/~FC%s~FB/~FC%s~SN~FB feeds..." % (
|
2012-12-25 12:08:17 -08:00
|
|
|
active_count,
|
|
|
|
inactive_count,
|
|
|
|
old_count,
|
2012-12-25 14:58:49 -08:00
|
|
|
))
|
|
|
|
|
2012-12-25 15:11:15 -08:00
|
|
|
Feed.task_feeds(feeds, verbose=False)
|
|
|
|
if inactive_feeds: Feed.task_feeds(inactive_feeds, verbose=False)
|
|
|
|
if old_feeds: Feed.task_feeds(old_feeds, verbose=False)
|
|
|
|
|
2012-07-16 23:40:17 -07:00
|
|
|
|
2010-09-20 19:22:19 -04:00
|
|
|
class UpdateFeeds(Task):
|
|
|
|
name = 'update-feeds'
|
2010-08-31 18:07:47 -04:00
|
|
|
max_retries = 0
|
2010-09-01 08:19:58 -04:00
|
|
|
ignore_result = True
|
2010-08-31 18:07:47 -04:00
|
|
|
|
2010-09-01 10:37:58 -04:00
|
|
|
def run(self, feed_pks, **kwargs):
|
2010-12-23 12:32:24 -05:00
|
|
|
from apps.rss_feeds.models import Feed
|
2012-02-24 16:43:08 -08:00
|
|
|
from apps.statistics.models import MStatistics
|
|
|
|
|
2012-08-16 23:56:21 -07:00
|
|
|
mongodb_replication_lag = int(MStatistics.get('mongodb_replication_lag', 0))
|
2012-10-23 09:30:14 -07:00
|
|
|
compute_scores = bool(mongodb_replication_lag < 10)
|
2012-08-16 23:56:21 -07:00
|
|
|
|
2012-02-26 22:33:06 -08:00
|
|
|
options = {
|
|
|
|
'fake': bool(MStatistics.get('fake_fetch')),
|
2012-02-27 16:41:34 -08:00
|
|
|
'quick': float(MStatistics.get('quick_fetch', 0)),
|
2012-08-16 23:56:21 -07:00
|
|
|
'compute_scores': compute_scores,
|
|
|
|
'mongodb_replication_lag': mongodb_replication_lag,
|
2012-02-26 22:33:06 -08:00
|
|
|
}
|
2012-02-24 16:43:08 -08:00
|
|
|
|
2010-09-01 10:37:58 -04:00
|
|
|
if not isinstance(feed_pks, list):
|
|
|
|
feed_pks = [feed_pks]
|
|
|
|
|
|
|
|
for feed_pk in feed_pks:
|
2011-04-11 21:57:45 -04:00
|
|
|
try:
|
2012-10-25 16:14:25 -07:00
|
|
|
feed = Feed.get_by_id(feed_pk)
|
2012-03-26 17:04:35 -07:00
|
|
|
feed.update(**options)
|
2011-04-11 21:57:45 -04:00
|
|
|
except Feed.DoesNotExist:
|
|
|
|
logging.info(" ---> Feed doesn't exist: [%s]" % feed_pk)
|
2010-11-07 00:40:26 -04:00
|
|
|
# logging.debug(' Updating: [%s] %s' % (feed_pks, feed))
|
2010-08-31 18:07:47 -04:00
|
|
|
|
2010-09-28 05:16:00 -04:00
|
|
|
class NewFeeds(Task):
|
2010-09-20 19:22:19 -04:00
|
|
|
name = 'new-feeds'
|
2010-09-28 05:16:00 -04:00
|
|
|
max_retries = 0
|
|
|
|
ignore_result = True
|
|
|
|
|
|
|
|
def run(self, feed_pks, **kwargs):
|
2010-12-23 12:32:24 -05:00
|
|
|
from apps.rss_feeds.models import Feed
|
2010-09-28 05:16:00 -04:00
|
|
|
if not isinstance(feed_pks, list):
|
|
|
|
feed_pks = [feed_pks]
|
2012-03-26 12:40:13 -07:00
|
|
|
|
|
|
|
options = {
|
|
|
|
'force': True,
|
|
|
|
}
|
2010-09-28 05:16:00 -04:00
|
|
|
for feed_pk in feed_pks:
|
2012-10-25 16:14:25 -07:00
|
|
|
feed = Feed.get_by_id(feed_pk)
|
2012-03-26 12:40:13 -07:00
|
|
|
feed.update(options=options)
|
2012-03-28 15:49:21 -07:00
|
|
|
|
|
|
|
class PushFeeds(Task):
|
|
|
|
name = 'push-feeds'
|
|
|
|
max_retries = 0
|
|
|
|
ignore_result = True
|
|
|
|
|
|
|
|
def run(self, feed_id, xml, **kwargs):
|
|
|
|
from apps.rss_feeds.models import Feed
|
2012-08-16 23:56:21 -07:00
|
|
|
from apps.statistics.models import MStatistics
|
|
|
|
|
|
|
|
mongodb_replication_lag = int(MStatistics.get('mongodb_replication_lag', 0))
|
2012-09-13 13:54:40 -07:00
|
|
|
compute_scores = bool(mongodb_replication_lag < 60)
|
2012-03-28 15:49:21 -07:00
|
|
|
|
|
|
|
options = {
|
2012-08-16 23:56:21 -07:00
|
|
|
'feed_xml': xml,
|
|
|
|
'compute_scores': compute_scores,
|
|
|
|
'mongodb_replication_lag': mongodb_replication_lag,
|
2012-03-28 15:49:21 -07:00
|
|
|
}
|
2012-10-25 16:14:25 -07:00
|
|
|
feed = Feed.get_by_id(feed_id)
|
2012-03-28 15:49:21 -07:00
|
|
|
feed.update(options=options)
|
2012-12-10 11:37:35 -08:00
|
|
|
|
|
|
|
class BackupMongo(Task):
|
|
|
|
name = 'backup-mongo'
|
|
|
|
max_retries = 0
|
|
|
|
ignore_result = True
|
|
|
|
|
|
|
|
def run(self, **kwargs):
|
|
|
|
COLLECTIONS = "classifier_tag classifier_author classifier_feed classifier_title userstories starred_stories shared_stories category category_site sent_emails social_profile social_subscription social_services statistics feedback"
|
|
|
|
|
|
|
|
date = time.strftime('%Y-%m-%d-%H-%M')
|
|
|
|
collections = COLLECTIONS.split(' ')
|
|
|
|
db_name = 'newsblur'
|
|
|
|
dir_name = 'backup_mongo_%s' % date
|
|
|
|
filename = '%s.tgz' % dir_name
|
|
|
|
|
|
|
|
os.mkdir(dir_name)
|
|
|
|
|
|
|
|
for collection in collections:
|
|
|
|
cmd = 'mongodump --db %s --collection %s -o %s' % (db_name, collection, dir_name)
|
|
|
|
logging.debug(' ---> ~FMDumping ~SB%s~SN: %s' % (collection, cmd))
|
|
|
|
os.system(cmd)
|
|
|
|
|
|
|
|
cmd = 'tar -jcf %s %s' % (filename, dir_name)
|
|
|
|
os.system(cmd)
|
|
|
|
|
2012-12-10 13:33:37 -08:00
|
|
|
logging.debug(' ---> ~FRUploading ~SB~FM%s~SN~FR to S3...' % filename)
|
2012-12-10 11:37:35 -08:00
|
|
|
s3.save_file_in_s3(filename)
|
|
|
|
shutil.rmtree(dir_name)
|
2012-12-10 13:33:37 -08:00
|
|
|
os.remove(filename)
|
|
|
|
logging.debug(' ---> ~FRFinished uploading ~SB~FM%s~SN~FR to S3.' % filename)
|
2013-01-02 12:27:08 -08:00
|
|
|
|
|
|
|
|
|
|
|
class ScheduleImmediateFetches(Task):
|
|
|
|
|
|
|
|
def run(self, feed_ids, **kwargs):
|
|
|
|
from apps.rss_feeds.models import Feed
|
|
|
|
|
|
|
|
if not isinstance(feed_ids, list):
|
|
|
|
feed_ids = [feed_ids]
|
|
|
|
|
|
|
|
Feed.schedule_feed_fetches_immediately(feed_ids)
|
|
|
|
|