script/stream/recorder_tweetstream.py
author Raphael Velt <raph.velt@gmail.com>
Tue, 07 May 2013 18:28:26 +0200
changeset 877 41ce1c341abe
parent 528 7fb5a7b0d35c
child 693 2ef837069108
permissions -rw-r--r--
Added tag V02.26 for changeset 734640c4515a
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
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    14
import logging
11
54d7f1486ac4 implement get_oauth_token
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 9
diff changeset
    15
import os
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    16
import re
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
    17
import shutil
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
    18
import signal
11
54d7f1486ac4 implement get_oauth_token
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 9
diff changeset
    19
import socket
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    20
import sqlalchemy.schema
11
54d7f1486ac4 implement get_oauth_token
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 9
diff changeset
    21
import sys
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
    22
import threading
206
6d642d650470 Improve tweet recorder log info
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 199
diff changeset
    23
import time
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
    24
import traceback
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
    25
import tweepy.auth
9
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    26
import tweetstream
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
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
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']
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']
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
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']
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']
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    36
#just put it in a sqlite3 tqble
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    37
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    38
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    39
def set_logging(options):
272
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
    40
    loggers = []
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
    41
    
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
    42
    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
    43
    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
    44
    if options.debug >= 2:
272
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
    45
        loggers.append(utils.set_logging(options, logging.getLogger('sqlalchemy.engine')))
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    46
    #utils.set_logging(options, logging.getLogger('sqlalchemy.dialects'))
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    47
    #utils.set_logging(options, logging.getLogger('sqlalchemy.pool'))
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    48
    #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
    49
    return loggers
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    50
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
    51
def set_logging_process(options, queue):
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
    52
    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
    53
    qlogger.propagate = 0
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
    54
    return qlogger
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
    55
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    56
def get_auth(options, access_token):
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    57
    if options.username and options.password:
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    58
        auth = tweepy.auth.BasicAuthHandler(options.username, options.password)        
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    59
    else:
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    60
        consumer_key = models.CONSUMER_KEY
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    61
        consumer_secret = models.CONSUMER_SECRET
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    62
        auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret, secure=False)
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
    63
        auth.set_access_token(*access_token)
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
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   127
        super(SourceProcess, self).__init__(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
   128
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   129
    def do_run(self):
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   130
        
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   131
        #import pydevd
528
7fb5a7b0d35c remove reconnecting stream and propagate options
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 464
diff changeset
   132
        #pydevd.settrace(suspend=True)
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   133
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   134
        self.logger = set_logging_process(self.options, self.logger_queue)
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   135
        self.auth = get_auth(self.options, self.access_token) 
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   136
        
528
7fb5a7b0d35c remove reconnecting stream and propagate options
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 464
diff changeset
   137
        self.logger.debug("SourceProcess : run ")
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
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))                        
528
7fb5a7b0d35c remove reconnecting stream and propagate options
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 464
diff changeset
   144
        stream = tweetstream.FilterStream(self.auth, track=track_list, raw=True, url=self.options.url, catchup=self.catchup, timeout=self.timeout)
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")
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   146
        stream.muststop = lambda: self.stop_event.is_set()
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   147
        
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   148
        session = self.session_maker()
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
        try:
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   151
            for tweet in stream:
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   152
                if not self.parent_is_alive():
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   153
                    sys.exit()
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   154
                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
   155
                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
   156
                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
   157
                add_retries = 0
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   158
                while add_retries < 10:
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   159
                    try:
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   160
                        add_retries += 1
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   161
                        session.add(source)
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   162
                        session.flush()
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   163
                        break
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   164
                    except OperationalError as e:
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   165
                        session.rollback()
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   166
                        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
   167
                        if add_retries == 10:
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   168
                            raise e
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   169
                     
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   170
                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
   171
                self.logger.debug("SourceProcess : before queue + source id " + repr(source_id))
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   172
                self.logger.info("SourceProcess : Tweet count: %d - current rate : %.2f - running : %s" % (stream.count, stream.rate, int(time.time() - stream.starttime)))
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   173
                session.commit()
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   174
                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
   175
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   176
        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
   177
            self.logger.error("SourceProcess : Error when processing tweet " + repr(e))
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   178
        finally:
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   179
            session.rollback()
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   180
            stream.close()
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   181
            session.close()
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   182
            self.queue.close()
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   183
            self.stop_event.set()
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   184
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   185
464
b9243ade95e2 code cleaning and reorganisation for scripts
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 425
diff changeset
   186
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
   187
    try:
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   188
        tweet_obj = anyjson.deserialize(tweet)
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   189
        if 'text' not in tweet_obj:
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   190
            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
   191
            session.add(tweet_log)
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   192
            return
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   193
        screen_name = ""
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   194
        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
   195
            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
   196
        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
   197
        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
   198
        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
   199
        processor.process()
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   200
    except Exception as e:
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   201
        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
   202
        logger.exception(message)
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   203
        output = StringIO.StringIO()
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   204
        try:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   205
            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
   206
            error_stack = output.getvalue()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   207
        finally:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   208
            output.close()
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   209
        session.rollback()
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   210
        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
   211
        session.add(tweet_log)
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   212
        session.commit()
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
    
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   215
        
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   216
class TweetProcess(BaseProcess):
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   217
    
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   218
    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
   219
        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
   220
        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
   221
9
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   222
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   223
    def do_run(self):
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   224
        
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   225
        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
   226
        session = self.session_maker()
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   227
        try:
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   228
            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
   229
                try:
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   230
                    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
   231
                    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
   232
                except Exception as e:
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   233
                    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
   234
                    continue
464
b9243ade95e2 code cleaning and reorganisation for scripts
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 425
diff changeset
   235
                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
   236
                session.commit()
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   237
        finally:
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   238
            session.rollback()
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   239
            self.stop_event.set()
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   240
            session.close()
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   241
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   242
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   243
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
   244
    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
   245
    Session = scoped_session(Session)
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   246
    return Session, engine, metadata
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   247
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   248
            
528
7fb5a7b0d35c remove reconnecting stream and propagate options
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 464
diff changeset
   249
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
   250
    
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   251
    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
   252
    
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   253
    for src in sources:
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   254
        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
   255
        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
   256
        session.commit()
9
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   257
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   258
        
15
5d552b6a0e55 add oauth authentication to tweetstream
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 11
diff changeset
   259
    
243
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   260
    #get tweet source that do not match any message
9213a63fa34a - debug multithread (still database lock problem)
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 242
diff changeset
   261
    #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
   262
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
   263
    while not stop_event.is_set():
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   264
        for lqueue in logger_queues:
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   265
            try:
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   266
                record = lqueue.get_nowait()
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   267
                logging.getLogger(record.name).handle(record)
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   268
            except Queue.Empty:
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   269
                continue
256
2f335337ff64 Do not stop on IOErrors
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 255
diff changeset
   270
            except IOError:
2f335337ff64 Do not stop on IOErrors
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 255
diff changeset
   271
                continue
255
500cd0405c7a improve multi processing architecture
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 254
diff changeset
   272
        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
   273
11
54d7f1486ac4 implement get_oauth_token
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 9
diff changeset
   274
        
54d7f1486ac4 implement get_oauth_token
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 9
diff changeset
   275
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
   276
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   277
    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
   278
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   279
    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
   280
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   281
    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
   282
                      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
   283
    parser.add_option("-u", "--user", dest="username",
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   284
                      help="Twitter user", metavar="USER", default=None)
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   285
    parser.add_option("-w", "--password", dest="password",
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   286
                      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
   287
    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
   288
                      help="Twitter track", metavar="TRACK")
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   289
    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
   290
                      help="new database", default=False)
272
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   291
    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
   292
                      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
   293
    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
   294
                      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
   295
    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
   296
                      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
   297
    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
   298
                      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
   299
    parser.add_option("--url", dest="url",
2ddd11ec2da2 - twitter is https only now
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 289
diff changeset
   300
                      help="The twitter url to connect to.", metavar="URL", default=tweetstream.FilterStream.url)
464
b9243ade95e2 code cleaning and reorganisation for scripts
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 425
diff changeset
   301
    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
   302
                      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
   303
    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
   304
                      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
   305
    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
   306
                      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
   307
    
464
b9243ade95e2 code cleaning and reorganisation for scripts
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 425
diff changeset
   308
290
2ddd11ec2da2 - twitter is https only now
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 289
diff changeset
   309
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   310
11
54d7f1486ac4 implement get_oauth_token
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 9
diff changeset
   311
    utils.set_logging_options(parser)
9
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   312
11
54d7f1486ac4 implement get_oauth_token
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 9
diff changeset
   313
    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
   314
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   315
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   316
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
   317
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   318
    stop_args = {}
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   319
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   320
    access_token = None
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   321
    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
   322
        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
   323
    
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   324
    session = session_maker()
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   325
    try:
528
7fb5a7b0d35c remove reconnecting stream and propagate options
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 464
diff changeset
   326
        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
   327
        session.commit()
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   328
    finally:
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   329
        session.rollback()
261
d84c4aa2a9eb add process event for start and shutdown
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 260
diff changeset
   330
        session.close()
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   331
    
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   332
    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
   333
        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
   334
        return None
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   335
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   336
    queue = mQueue()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   337
    stop_event = Event()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   338
    
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   339
    #workaround for bug on using urllib2 and multiprocessing
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   340
    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
   341
    conn = None
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   342
    try:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   343
        conn = urllib2.urlopen(req)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   344
    except:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   345
        utils.get_logger().debug("could not open localhost")
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   346
        #donothing
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   347
    finally:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   348
        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
   349
            conn.close()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   350
    
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   351
    process_engines = []
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   352
    logger_queues = []
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   353
    
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   354
    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
   355
    process_engines.append(engine_process)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   356
    lqueue = mQueue(1)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   357
    logger_queues.append(lqueue)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   358
    pid = os.getpid()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   359
    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
   360
    
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   361
    tweet_processes = []
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   362
    
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   363
    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
   364
        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
   365
        process_engines.append(engine_process)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   366
        lqueue = mQueue(1)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   367
        logger_queues.append(lqueue)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   368
        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
   369
        tweet_processes.append(cprocess)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   370
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   371
    def interupt_handler(signum, frame):
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   372
        utils.get_logger().debug("shutdown asked " + repr(signum) + "  " + repr(inspect.getframeinfo(frame, 9)))
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   373
        stop_args.update({'message': 'interupt', 'signum':signum, 'frameinfo':inspect.getframeinfo(frame, 9)})
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   374
        stop_event.set()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   375
        
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   376
    signal.signal(signal.SIGINT , interupt_handler)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   377
    signal.signal(signal.SIGHUP , interupt_handler)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   378
    signal.signal(signal.SIGALRM, interupt_handler)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   379
    signal.signal(signal.SIGTERM, interupt_handler)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   380
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   381
    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
   382
    log_thread.daemon = True
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   383
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   384
    log_thread.start()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   385
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   386
    sprocess.start()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   387
    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
   388
        cprocess.start()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   389
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
   390
    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
   391
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   392
    if options.duration >= 0:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   393
        end_ts = datetime.datetime.utcnow() + datetime.timedelta(seconds=options.duration)    
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
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   396
    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
   397
        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
   398
            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
   399
            stop_event.set()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   400
            break
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   401
        if sprocess.is_alive():            
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   402
            time.sleep(1)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   403
        else:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   404
            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
   405
            stop_event.set()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   406
            break
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   407
    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
   408
    try:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   409
        sprocess.join(10)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   410
    except:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   411
        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
   412
        sprocess.terminate()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   413
        
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   414
    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
   415
        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
   416
        try:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   417
            cprocess.join(3)
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   418
        except:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   419
            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
   420
            cprocess.terminate()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   421
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
    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
   424
    try:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   425
        queue.close()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   426
        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
   427
            lqueue.close()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   428
    except exception as e:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   429
        utils.get_logger().error("error when closing queues %s", repr(e))
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   430
        #do nothing
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   431
        
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   432
    
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   433
    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
   434
        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
   435
        session = session_maker()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   436
        try:
528
7fb5a7b0d35c remove reconnecting stream and propagate options
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 464
diff changeset
   437
            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
   438
            session.commit()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   439
        finally:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   440
            session.rollback()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   441
            session.close()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   442
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   443
    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
   444
        pengine.dispose()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   445
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   446
    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
   447
9
bb44692e09ee script apres traitement enmi
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   448
272
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   449
def main(options, args):
11
54d7f1486ac4 implement get_oauth_token
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 9
diff changeset
   450
    
272
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   451
    global conn_str
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   452
    
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   453
    conn_str = options.conn_str.strip()
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   454
    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
   455
        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
   456
        
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   457
    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
   458
        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
   459
        if os.path.exists(filepath):
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   460
            i = 1
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   461
            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
   462
            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
   463
            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
   464
                i += 1
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   465
                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
   466
            if i >= 1000000:
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   467
                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
   468
            else:
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   469
                shutil.move(filepath, new_path)
242
cdd7d3c0549c Starting 'parallel_twitter' branch
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 207
diff changeset
   470
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   471
    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
   472
    
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   473
    if options.new:
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   474
        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
   475
        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
   476
            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
   477
            utils.get_logger().error(message)
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   478
            sys.exit(message)
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   479
    
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   480
    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
   481
    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
   482
    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
   483
        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
   484
    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
   485
        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
   486
    
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   487
    stop_args = {}
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   488
    try:
263
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   489
        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
   490
        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
   491
    except Exception as e:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   492
        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
   493
        outfile = StringIO.StringIO()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   494
        try:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   495
            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
   496
            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
   497
        finally:
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   498
            outfile.close()
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   499
        raise
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   500
    finally:    
6671e9a4c9c5 correct model ans improve event tracking
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 261
diff changeset
   501
        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
   502
425
b346fd32fc34 prepare for publication, add sync info
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 290
diff changeset
   503
    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
   504
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   505
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   506
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   507
if __name__ == '__main__':
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   508
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   509
    (options, args) = get_options()
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   510
    
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   511
    loggers = set_logging(options)
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   512
    
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   513
    utils.get_logger().debug("OPTIONS : " + repr(options))
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   514
    
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   515
    if options.daemon:
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   516
        import daemon
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   517
        import lockfile
254
2209e66bb50b multiple debugging and corrections
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 243
diff changeset
   518
        
272
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   519
        hdlr_preserve = []
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   520
        for logger in loggers:
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   521
            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
   522
            
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   523
        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
   524
        with context:
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   525
            main(options, args)
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   526
    else:
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   527
        main(options, args)
fe2efe3600ea add daemon option
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 263
diff changeset
   528