예제 #1
0
def daemon_start(pid_file):
    def handle_exit(signum, _):
        if signum == signal.SIGTERM:
            sys.exit(0)
        sys.exit(1)

    signal.signal(signal.SIGINT, handle_exit)
    signal.signal(signal.SIGTERM, handle_exit)

    # fork only once because we are sure parent will exit
    pid = os.fork()
    assert pid != -1

    if pid > 0:
        # parent waits for its child
        time.sleep(5)
        sys.exit(0)

    # child signals its parent to exit
    ppid = os.getppid()
    pid = os.getpid()
    if write_pid_file(pid_file, pid) != 0:
        Log.e(MAIN_TAG, "write_pid_file() failed, pid = " + str(pid))
        os.kill(ppid, signal.SIGINT)
        sys.exit(1)

    os.setsid()
    signal.signal(signal.SIG_IGN, signal.SIGHUP)

    Log.d(MAIN_TAG, 'started')
    os.kill(ppid, signal.SIGTERM)

    sys.stdin.close()
예제 #2
0
 def doQuery(self, checkin_date, checkout_dates, time_title):
     """
     the entry for build and execute a home api query
     :param checkin_date: the date when u are going to check in.
     :param checkout_dates: multiple checkout dates for enumerating the home info as completable as possible
     :param time_title:
     :return:
     """
     Log.d(SearchService.TAG, "===> querying on " + getDateStr())
     storageService = StorageService(self._config.localStoragePath)
     try:
         for (cityName, cityList) in self._config.cityList.items():
             retry_time = 0
             while retry_time < 3:
                 homeInfoCollection = dict()
                 for city in cityList:
                     for checkout_date in checkout_dates:
                         for adults in [1, 2]:
                             self.tryGetHomeInfo(checkin_date, checkout_date, homeInfoCollection, adults, city)
                 if len(homeInfoCollection) > 0:
                     roomInfos = [x for (_, x) in homeInfoCollection.items()]
                     storageService.saveOrUpdateRoomBatch(roomInfos)
                     analyze, excelRet = self.performAnalyze(roomInfos, cityName, storageService, time_title)
                     self.analyzeCollection.append(analyze)
                     self.forExcel.append(excelRet)
                     break
                 else:
                     retry_time += 1
                     Log.w(SearchService.TAG, "no data for query={0}, will retry for {1}th time".format(cityName, retry_time))
                     time.sleep(self._queryIntervalForFailInSec)  # take your time to do it
     except BaseException as e:
         Log.w(SearchService.TAG, "doQuery() failed, ", str(e) + "\r\nexception detail:" + traceback.format_exc())
     finally:
         storageService.close()
예제 #3
0
def daemon_stop(pid_file):
    import errno
    try:
        with open(pid_file) as f:
            buf = f.read()
            pid = Utils.to_str(buf)
            if not buf:
                Log.e(MAIN_TAG, 'not running')
    except IOError as e:
        Log.e(MAIN_TAG, "daemon_stop() fail", e)
        if e.errno == errno.ENOENT:
            # always exit 0 if we are sure daemon is not running
            Log.e(MAIN_TAG, 'not running')
            return
        sys.exit(1)
    pid = int(pid)
    if pid > 0:
        try:
            os.kill(pid, signal.SIGTERM)
        except OSError as e:
            if e.errno == errno.ESRCH:
                Log.e(MAIN_TAG, 'not running')
                # always exit 0 if we are sure daemon is not running
                return
            Log.e(MAIN_TAG, "daemon_stop() fail", e)
            sys.exit(1)
    else:
        Log.e(MAIN_TAG, 'pid is not positive: ' + str(pid))

    # sleep for maximum 10s
    for i in range(0, 200):
        try:
            # query for the pid
            os.kill(pid, 0)
        except OSError as e:
            if e.errno == errno.ESRCH:
                break
        time.sleep(0.05)
    else:
        Log.e(MAIN_TAG, 'timed out when stopping pid ' + str(pid))
        sys.exit(1)
    Log.d(MAIN_TAG, 'stopped')
    os.unlink(pid_file)
예제 #4
0
    def retrieveHomeData(self, query, originReturn, homeInfoCollection):
        '''
        pagenationMeta is like
         "paginationMetadata":{
                        "__typename":"DoraExploreV3PaginationMetadata",
                        "hasNextPage":true,
                        "itemsOffset":40,
                        "sectionOffset":3,
                        "hasPreviousPage":true,
                        "previousPageSectionOffset":0,
                        "previousPageItemsOffset":0,
                        "searchSessionId":"d91045aa-0d95-49ba-ad44-0d7bcf8b2813",
                        "pageLimit":20,
                        "totalCount":"124"
                    }
        '''
        Log.d(SearchService.TAG, "analyzing " + query)
        sections = originReturn["data"]["dora"]["exploreV3"]["filters"]["sections"]
        pagination = originReturn["data"]["dora"]["exploreV3"]["metadata"]["paginationMetadata"]
        if sections and len(sections) > 0:
            # if pagination:
            #     print("has_next_page = " + str(pagination["has_next_page"]))
            #     print("items_offset = " + str(pagination.get("items_offset")))
            #     print("section_offset = " + str(pagination.get("section_offset")))
            for item in sections:
                if item and item["__typename"] == "DoraExploreV3ListingsSection":
                    real_homes = item["items"]
                    Log.d(SearchService.TAG, str(len(real_homes)) + " rooms for " + query)
                    for roomItem in real_homes:
                        roomInfo = RoomInfo.parseFromDict(roomItem, query)
                        if roomInfo:
                            homeInfoCollection[roomInfo.roomId] = roomInfo

        else:
            Log.w(SearchService.TAG, "explore_tabs is empty")
        return pagination
예제 #5
0
    def HttpGetRequest(host, url, decode='utf-8'):
        """HTTP/HTTPS GET 方法"""
        try:
            if host.startswith("https://"):
                connect = httplib.HTTPSConnection
                host = host[8:]
            elif host.startswith("http://"):
                connect = httplib.HTTPConnection
                host = host[7:]
            else:
                connect = httplib.HTTPConnection

            with closing(connect(host)) as conn:
                conn.request(method="GET", url=url)
                response = conn.getresponse()
                if response.status != httplib.OK:
                    return None

                res = response.read()
                res = res.decode(decode, 'ignore').strip()

                return res
        except Exception as e:
            Log.d(TAG, e)
예제 #6
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 测试日志辅助类

import sys
sys.path.append("../")
from LogHelper import Log

TAG = "Test"

Log.d(TAG, "Hello")
Log.e(TAG, "World!")
Log.i(TAG, "Hehe")
Log.w(TAG, "XXXX")