Ejemplo n.º 1
0
 def __init__(self, shutDownEvent, lock, console: Console, nbrProcess=0, processStartInterval=3, dryRun=False):
     self.shutdownEvent = shutDownEvent
     self.processStartInterval = processStartInterval
     self.console = console
     self.lock = lock
     procCount = multiprocessing.cpu_count()
     if nbrProcess == 0:
         nbrProcess = procCount
     self.tasks = TasksQueue()
     self.nbrProcess = nbrProcess
     self.taskCount = 0
     self.processes = []
     manager = multiprocessing.Manager()
     self.states = manager.dict()
     self.stats = manager.dict()
     self.dryRun = dryRun
     self.startTime = 0.0
     
     console.log('Worker manager found %d cores' % procCount, True, True)
Ejemplo n.º 2
0
def runner(console: Console, shutdownEvent: Event, headless: bool, user: dict,
           proxy: dict, playlist: str, vnc: bool, sqsEndpoint: str,
           screenshotDir: str, runnerStats: Array, processStates: Array):
    tid = current_process().pid
    console.log('#%d Start' % tid)
    driver = None
    vdisplay = None
    x11vnc = None
    userDataDir = None
    spotify = None
    try:
        if headless == False:
            width = 1280
            height = 1024
            if 'windowSize' in user:
                [width, height] = user['windowSize'].split(',')
            vdisplay = Xvfb(width=width,
                            height=height,
                            colordepth=24,
                            tempdir=None,
                            noreset='+render')
            vdisplay.start()
            if vnc:
                x11vnc = X11vnc(vdisplay)
                x11vnc.start()

        driverManager = DriverManager(console, shutdownEvent)
        driverData = driverManager.getDriver(type='chrome',
                                             uid=tid,
                                             user=user,
                                             proxy=proxy,
                                             headless=headless)
        del driverManager
        collect()
        if not driverData or not driverData['driver']:
            if vdisplay:
                vdisplay.stop()
            if x11vnc:
                x11vnc.stop()
            raise Exception('No driver was returned from adapter')

        driver = driverData['driver']
        userDataDir = driverData['userDataDir']
    except Exception as e:
        runnerStats[STAT_DRIVER_NONE] += 1
        console.exception('Driver unavailable')
    else:
        try:
            spotify = Adapter(driver, console, shutdownEvent)
            console.log('#%d Start create account for %s' %
                        (tid, user['email']))
            spotify.register(user)
            try:
                boto3.client('sqs').send_message(
                    QueueUrl=sqsEndpoint,
                    MessageBody=dumps({
                        'user': user,
                        'playlist': playlist
                    }),
                    DelaySeconds=1,
                )
            except:
                console.exception('#%d Failed to send message to the queue' %
                                  tid)
            else:
                console.log('#%d Account created for %s' %
                            (tid, user['email']))
                runnerStats[STAT_ACCOUNT_CREATED] += 1
        except Exception as e:
            runnerStats[STAT_ERROR] += 1
            try:
                id = randint(10000, 99999)
                with open(screenshotDir + ('%d.log' % id), 'w') as f:
                    f.write(str(e))
                driver.save_screenshot(screenshotDir + ('%d.png' % id))
            except:
                console.exception()
    if driver:
        try:
            driver.quit()
            del driver
        except:
            pass
    if spotify:
        try:
            del spotify
        except:
            pass
    if userDataDir:
        try:
            rmtree(path=userDataDir, ignore_errors=True)
        except:
            pass
    if x11vnc:  #Terminate vnc server if any
        try:
            x11vnc.stop()
            del x11vnc
        except:
            pass
    if vdisplay:
        try:
            vdisplay.stop()
            del vdisplay
        except:
            pass
    console.log('#%d Stop' % tid)
    collect()
Ejemplo n.º 3
0
def run(console: Console, shutdownEvent: Event, headless: bool, user: dict,
        proxy: dict, playlist: str, vnc: bool, screenshotDir, stats: Array,
        states: Array):

    tid = current_process().pid
    console.log('#%d Start' % tid)

    driver = None
    try:
        if shutdownEvent.is_set():
            return
        vdisplay = None
        x11vnc = None
        if headless == False:
            width = 1280
            height = 1024
            if 'windowSize' in user:
                [width, height] = user['windowSize'].split(',')
            vdisplay = Xvfb(width=width,
                            height=height,
                            colordepth=24,
                            tempdir=None,
                            noreset='+render')
            vdisplay.start()
            if vnc:
                x11vnc = X11vnc(vdisplay)
                x11vnc.start()

        driverManager = DriverManager(console, shutdownEvent)
        driverData = driverManager.getDriver(type='chrome',
                                             uid=tid,
                                             user=user,
                                             proxy=proxy,
                                             headless=headless)
        if not driverData:
            raise Exception('No driverData was returned from adapter')
        driver = driverData['driver']
        userDataDir = driverData['userDataDir']
        if not driver:
            raise Exception('No driver was returned from adapter')

    except:
        stats[STAT_DRIVER_NONE] += 1
        console.exception('Driver unavailable')
    else:
        try:
            spotify = Adapter(driver, console, shutdownEvent)
            spotify.login(user['email'], user['password'])
            stats[STAT_LOGGED_IN] += 1
            console.log('#%d Logged In' % tid)
            spotify.playPlaylist(playlist, shutdownEvent, 80, 100)
            console.log('#%d Played' % tid)
            stats[STAT_PLAYED] += 1
        except Exception as e:
            stats[STAT_ERROR] += 1
            try:
                id = randint(10000, 99999)

                with open(screenshotDir + ('%d.log' % id), 'w') as f:
                    f.write(str(e))
                driver.save_screenshot(screenshotDir + ('%d.png' % id))
            except:
                console.exception()

    if driver:
        try:
            driver.quit()
            del driver
        except:
            pass
    if userDataDir:
        try:
            rmtree(path=userDataDir, ignore_errors=True)
        except:
            pass
    if x11vnc:  #Terminate vnc server if any
        try:
            x11vnc.stop()
            del x11vnc
        except:
            pass
    if vdisplay:
        try:
            vdisplay.stop()
            del vdisplay
        except:
            pass
    collect()