script/stream/recorder_stream.py
author Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
Tue, 18 Dec 2012 12:26:05 +0100
changeset 883 8ae3d91ea4ae
parent 739 script/stream/recorder_tweetstream.py@350ffcb7ae4d
child 884 07f1c6854df9
permissions -rw-r--r--
after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11
54d7f1486ac4 implement get_oauth_token
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 9
diff changeset
     1
from getpass import getpass
54d7f1486ac4 implement get_oauth_token
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 9
diff changeset
     2
from iri_tweet import models, utils
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
     3
from iri_tweet.models import TweetSource, TweetLog, ProcessEvent
290
2ddd11ec2da2 - twitter is https only now
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 289
diff changeset
     4
from multiprocessing import (Queue as mQueue, JoinableQueue, Process, Event, 
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
     5
    get_logger)
11
54d7f1486ac4 implement get_oauth_token
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 9
diff changeset
     6
from optparse import OptionParser
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
     7
from sqlalchemy.exc import OperationalError
289
a5eff8f2b81d improve session maker creation + models version + add model version in db
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 272
diff changeset
     8
from sqlalchemy.orm import scoped_session
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
     9
import Queue
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
    10
import StringIO
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
    11
import anyjson
199
514e0ee0c68a add a duration. not quitewhat expected but that will do
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 82
diff changeset
    12
import datetime
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
    13
import inspect
883
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
    14
import iri_tweet.stream
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    15
import logging
11
54d7f1486ac4 implement get_oauth_token
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 9
diff changeset
    16
import os
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    17
import re
883
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
    18
import requests.auth
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
    19
import shutil
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
    20
import signal
11
54d7f1486ac4 implement get_oauth_token
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 9
diff changeset
    21
import socket
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    22
import sqlalchemy.schema
11
54d7f1486ac4 implement get_oauth_token
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 9
diff changeset
    23
import sys
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
    24
import threading
206
6d642d650470 Improve tweet recorder log info
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 199
diff changeset
    25
import time
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
    26
import traceback
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    27
import urllib2
9
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    28
socket._fileobject.default_bufsize = 0
11
54d7f1486ac4 implement get_oauth_token
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 9
diff changeset
    29
9
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    30
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    31
883
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
    32
# columns_tweet = [u'favorited', u'truncated', u'text', u'created_at', u'source', u'in_reply_to_status_id', u'in_reply_to_screen_name', u'in_reply_to_user_id', u'geo', u'id', u'user']
9
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    33
columns_tweet = [u'user', u'favorited', u'contributors', u'truncated', u'text', u'created_at', u'retweeted', u'in_reply_to_status_id_str', u'coordinates', u'in_reply_to_user_id_str', u'entities', u'in_reply_to_status_id', u'place', u'in_reply_to_user_id', u'id', u'in_reply_to_screen_name', u'retweet_count', u'geo', u'id_str', u'source']
883
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
    34
# columns_user = [u'id', u'verified', u'profile_sidebar_fill_color', u'profile_text_color', u'followers_count', u'protected', u'location', u'profile_background_color', u'utc_offset', u'statuses_count', u'description', u'friends_count', u'profile_link_color', u'profile_image_url', u'notifications', u'geo_enabled', u'profile_background_image_url', u'screen_name', u'profile_background_tile', u'favourites_count', u'name', u'url', u'created_at', u'time_zone', u'profile_sidebar_border_color', u'following']
9
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    35
columns_user = [u'follow_request_sent', u'profile_use_background_image', u'id', u'verified', u'profile_sidebar_fill_color', u'profile_text_color', u'followers_count', u'protected', u'location', u'profile_background_color', u'id_str', u'utc_offset', u'statuses_count', u'description', u'friends_count', u'profile_link_color', u'profile_image_url', u'notifications', u'show_all_inline_media', u'geo_enabled', u'profile_background_image_url', u'name', u'lang', u'following', u'profile_background_tile', u'favourites_count', u'screen_name', u'url', u'created_at', u'contributors_enabled', u'time_zone', u'profile_sidebar_border_color', u'is_translator', u'listed_count']
883
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
    36
# just put it in a sqlite3 tqble
9
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    37
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
    38
DEFAULT_TIMEOUT = 5
9
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    39
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    40
def set_logging(options):
272
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
    41
    loggers = []
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
    42
    
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
    43
    loggers.append(utils.set_logging(options, logging.getLogger('iri.tweet')))
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
    44
    loggers.append(utils.set_logging(options, logging.getLogger('multiprocessing')))
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    45
    if options.debug >= 2:
272
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
    46
        loggers.append(utils.set_logging(options, logging.getLogger('sqlalchemy.engine')))
883
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
    47
    # utils.set_logging(options, logging.getLogger('sqlalchemy.dialects'))
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
    48
    # utils.set_logging(options, logging.getLogger('sqlalchemy.pool'))
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
    49
    # utils.set_logging(options, logging.getLogger('sqlalchemy.orm'))
272
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
    50
    return loggers
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    51
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
    52
def set_logging_process(options, queue):
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
    53
    qlogger = utils.set_logging(options, logging.getLogger('iri.tweet.p'), queue)
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
    54
    qlogger.propagate = 0
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
    55
    return qlogger
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
    56
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    57
def get_auth(options, access_token):
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    58
    if options.username and options.password:
883
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
    59
        auth = requests.auth.BasicAuthHandler(options.username, options.password)        
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    60
    else:
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    61
        consumer_key = models.CONSUMER_KEY
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    62
        consumer_secret = models.CONSUMER_SECRET
883
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
    63
        auth = requests.auth.OAuth1(access_token[0], access_token[1], consumer_key, consumer_secret, signature_type='auth_header')
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    64
    return auth
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    65
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    66
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
    67
def add_process_event(type, args, session_maker):
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
    68
    session = session_maker()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
    69
    try:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
    70
        evt = ProcessEvent(args=None if args is None else anyjson.serialize(args), type=type)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
    71
        session.add(evt)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
    72
        session.commit()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
    73
    finally:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
    74
        session.close()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
    75
9
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    76
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
    77
class BaseProcess(Process):
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
    78
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
    79
    def __init__(self, session_maker, queue, options, access_token, stop_event, logger_queue, parent_pid):
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
    80
        self.parent_pid = parent_pid
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
    81
        self.session_maker = session_maker
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
    82
        self.queue = queue
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
    83
        self.options = options
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
    84
        self.logger_queue = logger_queue
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
    85
        self.stop_event = stop_event
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
    86
        self.access_token = access_token
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
    87
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
    88
        super(BaseProcess, self).__init__()
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
    89
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
    90
    #
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
    91
    # from http://stackoverflow.com/questions/2542610/python-daemon-doesnt-kill-its-kids
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
    92
    #
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
    93
    def parent_is_alive(self):
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
    94
        try:
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
    95
            # try to call Parent
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
    96
            os.kill(self.parent_pid, 0)
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
    97
        except OSError:
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
    98
            # *beeep* oh no! The phone's disconnected!
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
    99
            return False
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   100
        else:
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   101
            # *ring* Hi mom!
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   102
            return True
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   103
    
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   104
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   105
    def __get_process_event_args(self):
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   106
        return {'name':self.name, 'pid':self.pid, 'parent_pid':self.parent_pid, 'options':self.options.__dict__, 'access_token':self.access_token}
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   107
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   108
    def run(self):
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   109
        try:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   110
            add_process_event("start_worker", self.__get_process_event_args(), self.session_maker)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   111
            self.do_run()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   112
        finally:
289
a5eff8f2b81d improve session maker creation + models version + add model version in db
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 272
diff changeset
   113
            add_process_event("stop_worker", self.__get_process_event_args(), self.session_maker)
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   114
        
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   115
    def do_run(self):
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   116
        raise NotImplementedError()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   117
9
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   118
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   119
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   120
class SourceProcess(BaseProcess):
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   121
    
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   122
    def __init__(self, session_maker, queue, options, access_token, stop_event, logger_queue, parent_pid):
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   123
        self.track = options.track
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   124
        self.token_filename = options.token_filename
528
7fb5a7b0d35c remove reconnecting stream and propagate options
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 464
diff changeset
   125
        self.catchup = options.catchup
7fb5a7b0d35c remove reconnecting stream and propagate options
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 464
diff changeset
   126
        self.timeout = options.timeout
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   127
        self.stream = None
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   128
        super(SourceProcess, self).__init__(session_maker, queue, options, access_token, stop_event, logger_queue, parent_pid)
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   129
                    
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   130
    def __source_stream_iter(self):
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   131
        
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   132
        self.logger = set_logging_process(self.options, self.logger_queue)
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   133
        self.logger.debug("SourceProcess : run ")
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   134
        
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   135
        self.auth = get_auth(self.options, self.access_token) 
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   136
        self.logger.debug("SourceProcess : auth set ")
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   137
        
883
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
   138
        track_list = self.track  # or raw_input('Keywords to track (comma seperated): ').strip()
528
7fb5a7b0d35c remove reconnecting stream and propagate options
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 464
diff changeset
   139
        self.logger.debug("SourceProcess : track list " + track_list)
7fb5a7b0d35c remove reconnecting stream and propagate options
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 464
diff changeset
   140
        
7fb5a7b0d35c remove reconnecting stream and propagate options
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 464
diff changeset
   141
        track_list = [k.strip() for k in track_list.split(',')]
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   142
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   143
        self.logger.debug("SourceProcess : before connecting to stream " + repr(track_list))                        
883
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
   144
        self.stream = iri_tweet.stream.FilterStream(self.auth, track=track_list, raw=True, url=self.options.url, catchup=self.catchup, timeout=self.timeout, chunk_size=1, logger=self.logger)
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   145
        self.logger.debug("SourceProcess : after connecting to stream")
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   146
        self.stream.muststop = lambda: self.stop_event.is_set()        
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   147
        
883
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
   148
        stream_wrapper = iri_tweet.stream.SafeStreamWrapper(self.stream, logger=self.logger)
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   149
        
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   150
        session = self.session_maker()
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   151
        
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   152
        try:
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   153
            for tweet in stream_wrapper:
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   154
                if not self.parent_is_alive():
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   155
                    self.stop_event.set()
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   156
                    stop_thread.join(5)
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   157
                    sys.exit()
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   158
                self.logger.debug("SourceProcess : tweet " + repr(tweet))
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   159
                source = TweetSource(original_json=tweet)
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   160
                self.logger.debug("SourceProcess : source created")
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   161
                add_retries = 0
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   162
                while add_retries < 10:
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   163
                    try:
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   164
                        add_retries += 1
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   165
                        session.add(source)
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   166
                        session.flush()
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   167
                        break
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   168
                    except OperationalError as e:
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   169
                        session.rollback()
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   170
                        self.logger.debug("SourceProcess : Operational Error %s nb %d" % (repr(e), add_retries))
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   171
                        if add_retries == 10:
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   172
                            raise
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   173
                     
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   174
                source_id = source.id
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   175
                self.logger.debug("SourceProcess : before queue + source id " + repr(source_id))
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   176
                self.logger.info("SourceProcess : Tweet count: %d - current rate : %.2f - running : %s" % (self.stream.count, self.stream.rate, int(time.time() - self.stream.starttime)))
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   177
                session.commit()
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   178
                self.queue.put((source_id, tweet), False)
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   179
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   180
        except Exception as e:
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   181
            self.logger.error("SourceProcess : Error when processing tweet " + repr(e))
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   182
            raise
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   183
        finally:
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   184
            session.rollback()
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   185
            session.close()
738
2497c7f38e0a correct. remove mutex and clear
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 737
diff changeset
   186
            self.logger_queue.close()
2497c7f38e0a correct. remove mutex and clear
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 737
diff changeset
   187
            self.queue.close()
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   188
            self.stream.close()
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   189
            self.stream = None
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   190
            if not self.stop_event.is_set():
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   191
                self.stop_event.set()
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   192
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   193
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   194
    def do_run(self):
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   195
        
883
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
   196
        # import pydevd
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
   197
        # pydevd.settrace(suspend=False)
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   198
        
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   199
        source_stream_iter_thread = threading.Thread(target=self.__source_stream_iter , name="SourceStreamIterThread")
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   200
        
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   201
        source_stream_iter_thread.start()
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   202
        
738
2497c7f38e0a correct. remove mutex and clear
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 737
diff changeset
   203
        while not self.stop_event.is_set():
739
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   204
            self.logger.debug("SourceProcess : In while after start")
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   205
            self.stop_event.wait(DEFAULT_TIMEOUT)
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   206
            if self.stop_event.is_set() and self.stream:
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   207
                self.stream.close()
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   208
            elif not self.stop_event.is_set() and not source_stream_iter_thread.is_alive:
738
2497c7f38e0a correct. remove mutex and clear
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 737
diff changeset
   209
                self.stop_event.set()
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   210
738
2497c7f38e0a correct. remove mutex and clear
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 737
diff changeset
   211
        self.logger.info("SourceProcess : join")
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   212
        source_stream_iter_thread.join(30)
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   213
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   214
464
b9243ade95e2 code cleaning and reorganisation for scripts
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 425
diff changeset
   215
def process_tweet(tweet, source_id, session, access_token, twitter_query_user, logger):
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   216
    try:
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   217
        if not tweet.strip():
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   218
            return
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   219
        tweet_obj = anyjson.deserialize(tweet)
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   220
        if 'text' not in tweet_obj:
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   221
            tweet_log = TweetLog(tweet_source_id=source_id, status=TweetLog.TWEET_STATUS['NOT_TWEET'])
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   222
            session.add(tweet_log)
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   223
            return
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   224
        screen_name = ""
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   225
        if 'user' in tweet_obj and 'screen_name' in tweet_obj['user']:
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   226
            screen_name = tweet_obj['user']['screen_name']
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   227
        logger.info(u"Process_tweet from %s : %s" % (screen_name, tweet_obj['text']))
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   228
        logger.debug(u"Process_tweet :" + repr(tweet))
464
b9243ade95e2 code cleaning and reorganisation for scripts
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 425
diff changeset
   229
        processor = utils.TwitterProcessor(tweet_obj, tweet, source_id, session, access_token, None, twitter_query_user)
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   230
        processor.process()
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   231
    except ValueError as e:
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   232
        message = u"Value Error %s processing tweet %s" % (repr(e), tweet)
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   233
        output = StringIO.StringIO()
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   234
        try:
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   235
            traceback.print_exc(file=output)
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   236
            error_stack = output.getvalue()
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   237
        finally:
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   238
            output.close()
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   239
        tweet_log = TweetLog(tweet_source_id=source_id, status=TweetLog.TWEET_STATUS['NOT_TWEET'], error=message, error_stack=error_stack)
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   240
        session.add(tweet_log)
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   241
        session.commit()        
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   242
    except Exception as e:
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   243
        message = u"Error %s processing tweet %s" % (repr(e), tweet)
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   244
        logger.exception(message)
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   245
        output = StringIO.StringIO()
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   246
        try:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   247
            traceback.print_exc(file=output)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   248
            error_stack = output.getvalue()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   249
        finally:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   250
            output.close()
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   251
        session.rollback()
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   252
        tweet_log = TweetLog(tweet_source_id=source_id, status=TweetLog.TWEET_STATUS['ERROR'], error=message, error_stack=error_stack)
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   253
        session.add(tweet_log)
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   254
        session.commit()
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   255
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   256
    
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   257
        
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   258
class TweetProcess(BaseProcess):
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   259
    
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   260
    def __init__(self, session_maker, queue, options, access_token, stop_event, logger_queue, parent_pid):
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   261
        super(TweetProcess, self).__init__(session_maker, queue, options, access_token, stop_event, logger_queue, parent_pid)
464
b9243ade95e2 code cleaning and reorganisation for scripts
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 425
diff changeset
   262
        self.twitter_query_user = options.twitter_query_user
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   263
9
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   264
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   265
    def do_run(self):
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   266
        
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   267
        self.logger = set_logging_process(self.options, self.logger_queue)
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   268
        session = self.session_maker()
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   269
        try:
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   270
            while not self.stop_event.is_set() and self.parent_is_alive():
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   271
                try:
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   272
                    source_id, tweet_txt = self.queue.get(True, 3)
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   273
                    self.logger.debug("Processing source id " + repr(source_id))
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   274
                except Exception as e:
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   275
                    self.logger.debug('Process tweet exception in loop : ' + repr(e))
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   276
                    continue
464
b9243ade95e2 code cleaning and reorganisation for scripts
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 425
diff changeset
   277
                process_tweet(tweet_txt, source_id, session, self.access_token, self.twitter_query_user, self.logger)
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   278
                session.commit()
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   279
        finally:
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   280
            session.rollback()
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   281
            session.close()
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   282
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   283
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   284
def get_sessionmaker(conn_str):
289
a5eff8f2b81d improve session maker creation + models version + add model version in db
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 272
diff changeset
   285
    engine, metadata, Session = models.setup_database(conn_str, echo=False, create_all=False, autocommit=False)
a5eff8f2b81d improve session maker creation + models version + add model version in db
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 272
diff changeset
   286
    Session = scoped_session(Session)
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   287
    return Session, engine, metadata
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   288
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   289
            
528
7fb5a7b0d35c remove reconnecting stream and propagate options
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 464
diff changeset
   290
def process_leftovers(session, access_token, twitter_query_user, logger):
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   291
    
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   292
    sources = session.query(TweetSource).outerjoin(TweetLog).filter(TweetLog.id == None)
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   293
    
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   294
    for src in sources:
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   295
        tweet_txt = src.original_json
528
7fb5a7b0d35c remove reconnecting stream and propagate options
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 464
diff changeset
   296
        process_tweet(tweet_txt, src.id, session, access_token, twitter_query_user, logger)
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   297
        session.commit()
9
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   298
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   299
        
15
5d552b6a0e55 add oauth authentication to tweetstream
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 11
diff changeset
   300
    
883
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
   301
    # get tweet source that do not match any message
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
   302
    # select * from tweet_tweet_source ts left join tweet_tweet_log tl on ts.id = tl.tweet_source_id where tl.id isnull;
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   303
def process_log(logger_queues, stop_event):
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   304
    while not stop_event.is_set():
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   305
        for lqueue in logger_queues:
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   306
            try:
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   307
                record = lqueue.get_nowait()
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   308
                logging.getLogger(record.name).handle(record)
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   309
            except Queue.Empty:
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   310
                continue
256
2f335337ff64 Do not stop on IOErrors
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 255
diff changeset
   311
            except IOError:
2f335337ff64 Do not stop on IOErrors
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 255
diff changeset
   312
                continue
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   313
        time.sleep(0.1)
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   314
11
54d7f1486ac4 implement get_oauth_token
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 9
diff changeset
   315
        
54d7f1486ac4 implement get_oauth_token
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 9
diff changeset
   316
def get_options():
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   317
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   318
    usage = "usage: %prog [options]"
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   319
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   320
    parser = OptionParser(usage=usage)
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   321
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   322
    parser.add_option("-f", "--file", dest="conn_str",
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   323
                      help="write tweet to DATABASE. This is a connection string", metavar="CONNECTION_STR", default="enmi2010_twitter.db")
9
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   324
    parser.add_option("-u", "--user", dest="username",
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   325
                      help="Twitter user", metavar="USER", default=None)
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   326
    parser.add_option("-w", "--password", dest="password",
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   327
                      help="Twitter password", metavar="PASSWORD", default=None)
15
5d552b6a0e55 add oauth authentication to tweetstream
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 11
diff changeset
   328
    parser.add_option("-T", "--track", dest="track",
9
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   329
                      help="Twitter track", metavar="TRACK")
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   330
    parser.add_option("-n", "--new", dest="new", action="store_true",
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   331
                      help="new database", default=False)
272
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   332
    parser.add_option("-D", "--daemon", dest="daemon", action="store_true",
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   333
                      help="launch daemon", default=False)
15
5d552b6a0e55 add oauth authentication to tweetstream
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 11
diff changeset
   334
    parser.add_option("-t", dest="token_filename", metavar="TOKEN_FILENAME", default=".oauth_token",
5d552b6a0e55 add oauth authentication to tweetstream
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 11
diff changeset
   335
                      help="Token file name")
199
514e0ee0c68a add a duration. not quitewhat expected but that will do
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 82
diff changeset
   336
    parser.add_option("-d", "--duration", dest="duration",
514e0ee0c68a add a duration. not quitewhat expected but that will do
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 82
diff changeset
   337
                      help="Duration of recording in seconds", metavar="DURATION", default= -1, type='int')
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   338
    parser.add_option("-N", "--nb-process", dest="process_nb",
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   339
                      help="number of process.\nIf 0, only the lefovers of the database are processed.\nIf 1, no postprocessing is done on the tweets.", metavar="PROCESS_NB", default=2, type='int')
290
2ddd11ec2da2 - twitter is https only now
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 289
diff changeset
   340
    parser.add_option("--url", dest="url",
883
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
   341
                      help="The twitter url to connect to.", metavar="URL", default=iri_tweet.stream.FilterStream.url)
464
b9243ade95e2 code cleaning and reorganisation for scripts
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 425
diff changeset
   342
    parser.add_option("--query-user", dest="twitter_query_user", action="store_true",
b9243ade95e2 code cleaning and reorganisation for scripts
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 425
diff changeset
   343
                      help="Query twitter for users", default=False, metavar="QUERY_USER")
528
7fb5a7b0d35c remove reconnecting stream and propagate options
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 464
diff changeset
   344
    parser.add_option("--catchup", dest="catchup",
7fb5a7b0d35c remove reconnecting stream and propagate options
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 464
diff changeset
   345
                      help="catchup count for tweets", default=None, metavar="CATCHUP", type='int')
7fb5a7b0d35c remove reconnecting stream and propagate options
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 464
diff changeset
   346
    parser.add_option("--timeout", dest="timeout",
7fb5a7b0d35c remove reconnecting stream and propagate options
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 464
diff changeset
   347
                      help="timeout for connecting in seconds", default=60, metavar="TIMEOUT", type='int')
7fb5a7b0d35c remove reconnecting stream and propagate options
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 464
diff changeset
   348
    
464
b9243ade95e2 code cleaning and reorganisation for scripts
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 425
diff changeset
   349
290
2ddd11ec2da2 - twitter is https only now
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 289
diff changeset
   350
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   351
11
54d7f1486ac4 implement get_oauth_token
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 9
diff changeset
   352
    utils.set_logging_options(parser)
9
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   353
11
54d7f1486ac4 implement get_oauth_token
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 9
diff changeset
   354
    return parser.parse_args()
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   355
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   356
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   357
def do_run(options, session_maker):
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   358
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   359
    stop_args = {}
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   360
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   361
    access_token = None
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   362
    if not options.username or not options.password:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   363
        access_token = utils.get_oauth_token(options.token_filename)
11
54d7f1486ac4 implement get_oauth_token
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 9
diff changeset
   364
    
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   365
    session = session_maker()
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   366
    try:
528
7fb5a7b0d35c remove reconnecting stream and propagate options
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 464
diff changeset
   367
        process_leftovers(session, access_token, options.twitter_query_user, utils.get_logger())
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   368
        session.commit()
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   369
    finally:
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   370
        session.rollback()
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   371
        session.close()
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   372
    
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   373
    if options.process_nb <= 0:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   374
        utils.get_logger().debug("Leftovers processed. Exiting.")
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   375
        return None
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   376
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   377
    queue = mQueue()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   378
    stop_event = Event()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   379
    
883
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
   380
    # workaround for bug on using urllib2 and multiprocessing
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   381
    req = urllib2.Request('http://localhost')
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   382
    conn = None
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   383
    try:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   384
        conn = urllib2.urlopen(req)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   385
    except:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   386
        utils.get_logger().debug("could not open localhost")
883
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
   387
        # donothing
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   388
    finally:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   389
        if conn is not None:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   390
            conn.close()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   391
    
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   392
    process_engines = []
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   393
    logger_queues = []
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   394
    
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   395
    SessionProcess, engine_process, metadata_process = get_sessionmaker(conn_str)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   396
    process_engines.append(engine_process)
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   397
    lqueue = mQueue(50)
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   398
    logger_queues.append(lqueue)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   399
    pid = os.getpid()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   400
    sprocess = SourceProcess(SessionProcess, queue, options, access_token, stop_event, lqueue, pid)    
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   401
    
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   402
    tweet_processes = []
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   403
    
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   404
    for i in range(options.process_nb - 1):
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   405
        SessionProcess, engine_process, metadata_process = get_sessionmaker(conn_str)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   406
        process_engines.append(engine_process)
693
2ef837069108 Starting 'listener_update' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 528
diff changeset
   407
        lqueue = mQueue(50)
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   408
        logger_queues.append(lqueue)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   409
        cprocess = TweetProcess(SessionProcess, queue, options, access_token, stop_event, lqueue, pid)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   410
        tweet_processes.append(cprocess)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   411
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   412
    log_thread = threading.Thread(target=process_log, name="loggingThread", args=(logger_queues, stop_event,))
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   413
    log_thread.daemon = True
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   414
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   415
    log_thread.start()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   416
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   417
    sprocess.start()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   418
    for cprocess in tweet_processes:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   419
        cprocess.start()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   420
289
a5eff8f2b81d improve session maker creation + models version + add model version in db
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 272
diff changeset
   421
    add_process_event("pid", {'main':os.getpid(), 'source':(sprocess.name, sprocess.pid), 'consumers':dict([(p.name, p.pid) for p in tweet_processes])}, session_maker)
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   422
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   423
    if options.duration >= 0:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   424
        end_ts = datetime.datetime.utcnow() + datetime.timedelta(seconds=options.duration)    
739
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   425
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   426
    def interupt_handler(signum, frame):
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   427
        utils.get_logger().debug("shutdown asked " + repr(signum) + "  " + repr(inspect.getframeinfo(frame, 9)))
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   428
        stop_args.update({'message': 'interupt', 'signum':signum, 'frameinfo':inspect.getframeinfo(frame, 9)})
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   429
        stop_event.set()
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   430
        
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   431
    signal.signal(signal.SIGINT , interupt_handler)
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   432
    signal.signal(signal.SIGHUP , interupt_handler)
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   433
    signal.signal(signal.SIGALRM, interupt_handler)
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   434
    signal.signal(signal.SIGTERM, interupt_handler)
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   435
    
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   436
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   437
    while not stop_event.is_set():
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   438
        if options.duration >= 0 and  datetime.datetime.utcnow() >= end_ts:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   439
            stop_args.update({'message': 'duration', 'duration' : options.duration, 'end_ts' : end_ts})
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   440
            stop_event.set()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   441
            break
739
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   442
        if sprocess.is_alive():
350ffcb7ae4d correct listener.
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 738
diff changeset
   443
            utils.get_logger().debug("Source process alive")
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   444
            time.sleep(1)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   445
        else:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   446
            stop_args.update({'message': 'Source process killed'})
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   447
            stop_event.set()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   448
            break
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   449
    utils.get_logger().debug("Joining Source Process")
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   450
    try:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   451
        sprocess.join(10)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   452
    except:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   453
        utils.get_logger().debug("Pb joining Source Process - terminating")
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   454
        sprocess.terminate()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   455
        
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   456
    for i, cprocess in enumerate(tweet_processes):
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   457
        utils.get_logger().debug("Joining consumer process Nb %d" % (i + 1))
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   458
        try:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   459
            cprocess.join(3)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   460
        except:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   461
            utils.get_logger().debug("Pb joining consumer process Nb %d - terminating" % (i + 1))
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   462
            cprocess.terminate()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   463
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   464
    
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   465
    utils.get_logger().debug("Close queues")
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   466
    try:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   467
        queue.close()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   468
        for lqueue in logger_queues:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   469
            lqueue.close()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   470
    except exception as e:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   471
        utils.get_logger().error("error when closing queues %s", repr(e))
883
8ae3d91ea4ae after update to requests 1.0.2, do some cleaning: remove tweetstream and tweepy
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 739
diff changeset
   472
        # do nothing
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   473
        
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   474
    
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   475
    if options.process_nb > 1:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   476
        utils.get_logger().debug("Processing leftovers")
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   477
        session = session_maker()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   478
        try:
528
7fb5a7b0d35c remove reconnecting stream and propagate options
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 464
diff changeset
   479
            process_leftovers(session, access_token, options.twitter_query_user, utils.get_logger())
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   480
            session.commit()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   481
        finally:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   482
            session.rollback()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   483
            session.close()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   484
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   485
    for pengine in process_engines:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   486
        pengine.dispose()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   487
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   488
    return stop_args
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   489
9
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   490
272
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   491
def main(options, args):
11
54d7f1486ac4 implement get_oauth_token
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 9
diff changeset
   492
    
272
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   493
    global conn_str
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   494
    
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   495
    conn_str = options.conn_str.strip()
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   496
    if not re.match("^\w+://.+", conn_str):
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   497
        conn_str = 'sqlite:///' + options.conn_str
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   498
        
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   499
    if conn_str.startswith("sqlite") and options.new:
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   500
        filepath = conn_str[conn_str.find(":///") + 4:]
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   501
        if os.path.exists(filepath):
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   502
            i = 1
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   503
            basename, extension = os.path.splitext(filepath)
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   504
            new_path = '%s.%d%s' % (basename, i, extension)
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   505
            while i < 1000000 and os.path.exists(new_path):
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   506
                i += 1
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   507
                new_path = '%s.%d%s' % (basename, i, extension)
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   508
            if i >= 1000000:
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   509
                raise Exception("Unable to find new filename for " + filepath)
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   510
            else:
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   511
                shutil.move(filepath, new_path)
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   512
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   513
    Session, engine, metadata = get_sessionmaker(conn_str)
9
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   514
    
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   515
    if options.new:
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   516
        check_metadata = sqlalchemy.schema.MetaData(bind=engine, reflect=True)
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   517
        if len(check_metadata.sorted_tables) > 0:
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   518
            message = "Database %s not empty exiting" % conn_str
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   519
            utils.get_logger().error(message)
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   520
            sys.exit(message)
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   521
    
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   522
    metadata.create_all(engine)
289
a5eff8f2b81d improve session maker creation + models version + add model version in db
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 272
diff changeset
   523
    session = Session()
a5eff8f2b81d improve session maker creation + models version + add model version in db
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 272
diff changeset
   524
    try:
a5eff8f2b81d improve session maker creation + models version + add model version in db
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 272
diff changeset
   525
        models.add_model_version(session)
a5eff8f2b81d improve session maker creation + models version + add model version in db
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 272
diff changeset
   526
    finally:
a5eff8f2b81d improve session maker creation + models version + add model version in db
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 272
diff changeset
   527
        session.close()
a5eff8f2b81d improve session maker creation + models version + add model version in db
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 272
diff changeset
   528
    
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   529
    stop_args = {}
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   530
    try:
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   531
        add_process_event(type="start", args={'options':options.__dict__, 'args': args, 'command_line': sys.argv}, session_maker=Session)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   532
        stop_args = do_run(options, Session)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   533
    except Exception as e:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   534
        utils.get_logger().exception("Error in main thread")        
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   535
        outfile = StringIO.StringIO()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   536
        try:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   537
            traceback.print_exc(file=outfile)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   538
            stop_args = {'error': repr(e), 'message': getattr(e, 'message', ''), 'stacktrace':outfile.getvalue()}
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   539
        finally:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   540
            outfile.close()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   541
        raise
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   542
    finally:    
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   543
        add_process_event(type="shutdown", args=stop_args, session_maker=Session)
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   544
425
b346fd32fc34 prepare for publication, add sync info
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 290
diff changeset
   545
    utils.get_logger().debug("Done. Exiting. " + repr(stop_args))
272
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   546
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   547
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   548
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   549
if __name__ == '__main__':
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   550
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   551
    (options, args) = get_options()
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   552
    
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   553
    loggers = set_logging(options)
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   554
    
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   555
    utils.get_logger().debug("OPTIONS : " + repr(options))
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   556
    
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   557
    if options.daemon:
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   558
        import daemon
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   559
        import lockfile
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   560
        
272
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   561
        hdlr_preserve = []
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   562
        for logger in loggers:
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   563
            hdlr_preserve.extend([h.stream for h in logger.handlers])
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   564
            
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   565
        context = daemon.DaemonContext(working_directory=os.getcwd(), files_preserve=hdlr_preserve) 
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   566
        with context:
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   567
            main(options, args)
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   568
    else:
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   569
        main(options, args)
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   570