Пример #1
0
def task_run(idnum, request):
    while 1:
        try:
            task = Task.objects.get(id=idnum)
        except:
            continue
        break
    if task.status != 'executed' and task.status != 'running' and task.status != 'NULL':
        hosttag = task.dbtag
        sql = task.sqltext
        mycreatetime = task.create_time
        incept.log_incep_op(sql, hosttag, request, mycreatetime)
        status = 'running'
        task.status = status
        task.operator = request.user.username
        task.update_time = datetime.datetime.now()
        task.save()
        db_type_record = func.get_dbtype_bydbtag(hosttag)
        dbtype_flag = str(db_type_record[0][0])
        if dbtype_flag == 'mysql':
            process_runtask.delay(hosttag, sql, task)
            #process_runtask(hosttag, sql, task)
        elif dbtype_flag == 'Oracle':
            useraccount = request.user.username
            ipaddr = func.get_client_ip(request)
            process_runtask_oracle.delay(hosttag, sql, task, useraccount,
                                         ipaddr)
            #process_runtask_oracle(hosttag, sql, task, useraccount, ipaddr)
            #delayoracle
            #process_runtask_oracle(hosttag, sql, task, useraccount, ipaddr)
        return ''
    elif task.status == 'NULL':
        return 'PLEASE CHECK THE SQL FIRST'
    else:
        return 'Already executed or in running'
Пример #2
0
def task_control():
    feeds = Feed.objects.all_published().filter(interval__gte=1)
    task_set = set()
    for feed in feeds:
        task_name = u"opps.feedcrawler.tasks_feed_{}".format(feed.slug)
        interval, _ = IntervalSchedule.objects.get_or_create(every=feed.interval, period="minutes")
        args = json.dumps([feed.slug])
        task, created = PeriodicTask.objects.get_or_create(
            name=task_name, defaults={"task": "opps.feedcrawler.tasks.process_feed", "interval": interval, "args": args}
        )
        if created:
            log_it("Adding task {}".format(task.name))

        if not created:
            changed = False
            if task.interval != interval:
                changed = True
                task.interval = interval
            if task.args != args:
                changed = True
                task.args = args
            if changed:
                log_it("Updating task {}".format(task.name))
                task.save()

        task_set.add(task.name)

    # Remove inexistent feeds
    tasks_to_remove = PeriodicTask.objects.filter(name__startswith="opps.feedcrawler.tasks_feed").exclude(
        name__in=task_set
    )

    for task in tasks_to_remove:
        log_it("Updating task {}".format(task.name))
        task.delete()
Пример #3
0
def create_task(name, task, task_args, crontab_time):
    '''
    创建任务
    name       # 任务名字
    task       # 执行的任务 "myapp.tasks.add"
    task_args  # 任务参数  {"x":1, "Y":1}
    crontab_time # 定时任务时间 格式:
        {
          'month_of_year': 9  # 月份
          'day_of_month': 5   # 日期
          'hour': 01         # 小时
          'minute':05  # 分钟
        }

    '''
    # task任务, created是否定时创建
    task, created = celery_models.PeriodicTask.objects.get_or_create(
        name=name,
        task=task)
    # 获取 crontab
    crontab = celery_models.CrontabSchedule.objects.filter(
        **crontab_time).first()
    if crontab is None:
        # 如果没有就创建,有的话就继续复用之前的crontab
        crontab = celery_models.CrontabSchedule.objects.create(
            **crontab_time)
    task.crontab = crontab  # 设置crontab
    task.enabled = True  # 开启task
    task.kwargs = json.dumps(task_args)  # 传入task参数
    expiration = timezone.now() + datetime.timedelta(day=1)
    task.expires = expiration  # 设置任务过期时间为现在时间的一天以后
    task.save()
    return True
Пример #4
0
def create_task(name, task, task_args, crontab_time, out_time):
    try:
        '''
        name # 任务名字
        task # 执行的任务 "myapp.tasks.add"
        task_args # 任务参数 {"x":1, "Y":1}
    
        crontab_time # 定时任务时间 格式:
        {
            'month_of_year': 9 # 月份
            'day_of_month': 5 # 日期
            'hour': 01 # 小时
            'minute':05 # 分钟
        }
        '''
        # task任务, created是否定时创建
        task, created = celery_models.PeriodicTask.objects.get_or_create(
            name=name, task=task)
        crontab = celery_models.CrontabSchedule.objects.filter(
            **crontab_time).first()
        if crontab is None:
            # 如果没有就创建,有的话就继续复用之前的crontab
            crontab = celery_models.CrontabSchedule.objects.create(
                **crontab_time)
        task.crontab = crontab  # 设置crontab
        task.enabled = True  # 开启task
        task.kwargs = json.dumps(task_args)  # 传入task参数
        task.expires = out_time  # 设置任务过期时间为现在时间的11天以后
        task.save()
    except Exception, e:
        logger = logging.getLogger("tasks")
        logger.error(e, exc_info=True)
Пример #5
0
def buildpersonalship(worldno, taskid, shiptype):
    world = World.objects.get(worldid=worldno)

    world.shipyardsinuse = F('shipyardsinuse') - 5
    world.flagshiptype = shiptype
    world.flagshiplocation = world.region

    if shiptype == 1:
        world.flagshippicture = 'pf01'
    elif shiptype == 2:
        world.flagshippicture = 'my01'
    elif shiptype == 3:
        world.flagshippicture = 'cs01'

    world.save(update_fields=['shipyardsinuse', 'flagshiptype', 'flagshiplocation','flagshippicture'])

    name = display.personalshipname(shiptype)

    task = Task.objects.get(pk=taskid)
    task.outcome = "Your %s was completed and delivered to your home fleet on %s." % (name, now())
    task.save()

    try:
        world.flagshipbuild = False
        world.save(update_fields=['flagshipbuild'])
    except:
        pass
Пример #6
0
def buildship(worldno, taskid, shiptype, amount, yards):
    world = World.objects.get(worldid=worldno)

    if world.shipyardsinuse - yards * amount < 0:
        world.shipyardsinuse = 0
    else:
        world.shipyardsinuse = F('shipyardsinuse') - yards * amount
    world.save(update_fields=['shipyardsinuse'])

    if 'prodstaging' in world.shipsortprefs:
        destination = 'staging area'
        utilities.movecomplete(world, shiptype, amount, 'S', 0)
    else:
        destination = 'home fleet'
        utilities.movecomplete(world, shiptype, amount, world.region, 0)

    name = utilities.resname(shiptype + 10, amount, lower=True)

    pluralise = ("%s was" % name if amount == 1 else "%s %s were" %
                 (amount, name))

    task = Task.objects.get(pk=taskid)
    task.outcome = "Your %s completed and delivered to your %s on %s." % (
        pluralise, destination, now())
    task.save()
Пример #7
0
def create_task(name, task, task_args, crontab_time):
    '''
        name # 任务名字
        task # 执行的任务 "myapp.tasks.add"
        task_args # 任务参数 {"x":1, "Y":1}

        crontab_time # 定时任务时间 格式:
        {
            'month_of_year': 9 # 月份
            'day_of_month': 5 # 日期
            'hour': 01 # 小时
            'minute':05 # 分钟
        }
        '''

    # task任务, created是否定时创建
    task, created = celery_models.PeriodicTask.objects.get_or_create(name=name,
                                                                     task=task)

    # 获取 crontab
    crontab = celery_models.CrontabSchedule.objects.filter(
        **crontab_time).first()
    if crontab is None:
        # 如果没有就创建,有的话就继续复用之前的crontab
        crontab = celery_models.CrontabSchedule.objects.create(**crontab_time)

    task.crontab = crontab  # 设置crontab
    task.enabled = True  # 开启task
    task.kwargs = json.dumps(task_args)  # 传入task参数
    expiration = timezone.now() + datetime.timedelta(day=1)
    task.expires = expiration  # 设置任务过期时间为现在时间的一天以后
    task.save()
    return True
Пример #8
0
def tradecomplete(worldno, targetno, taskid, send, sendamount, trainingchange,
                  freighters):
    world = World.objects.get(worldid=worldno)
    target = World.objects.get(worldid=targetno)
    utilities.resourcecompletion(target, send, sendamount, trainingchange)

    world.freightersinuse = F('freightersinuse') - freighters
    world.save(update_fields=['freightersinuse'])
    utilities.freightermove(world, world.region, freighters)

    resname = utilities.resname(send, sendamount)

    linkworld = reverse('stats_ind', args=(world.worldid, ))
    fullworld = '<a href="%(link)s">%(world)s</a>' % {
        'link': linkworld,
        'world': world.world_name
    }

    ResourceLog.objects.create(owner=target,
                               target=world,
                               res=send,
                               amount=sendamount,
                               sent=False,
                               trade=True)

    htmldata = news.tradecompletion(world, resname, sendamount)
    NewsItem.objects.create(target=target, content=htmldata)

    utilities.spyintercept(target, world, resname, sendamount)

    task = Task.objects.get(pk=taskid)
    task.outcome = "We received %(amount)s %(name)s from our trade with %(world)s on %(time)s." \
                % {'amount':sendamount, 'name':resname, 'world':fullworld, 'time':now()}
    task.save()
Пример #9
0
def tradecomplete(worldno, targetno, taskid, send, sendamount, trainingchange, freighters):
    world = World.objects.get(worldid=worldno)
    target = World.objects.get(worldid=targetno)
    utilities.resourcecompletion(target, send, sendamount, trainingchange)

    world.freightersinuse = F('freightersinuse') - freighters
    world.save(update_fields=['freightersinuse'])
    utilities.freightermove(world, world.region, freighters)

    resname = utilities.resname(send, sendamount)

    linkworld = reverse('stats_ind', args=(world.worldid,))
    fullworld = '<a href="%(link)s">%(world)s</a>' % {'link':linkworld,'world':world.world_name}

    ResourceLog.objects.create(owner=target, target=world, res=send, amount=sendamount, sent=False, trade=True)

    htmldata = news.tradecompletion(world, resname, sendamount)
    NewsItem.objects.create(target=target, content=htmldata)

    utilities.spyintercept(target, world, resname, sendamount)

    task = Task.objects.get(pk=taskid)
    task.outcome = "We received %(amount)s %(name)s from our trade with %(world)s on %(time)s." \
                % {'amount':sendamount, 'name':resname, 'world':fullworld, 'time':now()}
    task.save()
Пример #10
0
def buildpersonalship(worldno, taskid, shiptype):
    world = World.objects.get(worldid=worldno)

    world.shipyardsinuse = F('shipyardsinuse') - 5
    world.flagshiptype = shiptype
    world.flagshiplocation = world.region

    if shiptype == 1:
        world.flagshippicture = 'pf01'
    elif shiptype == 2:
        world.flagshippicture = 'my01'
    elif shiptype == 3:
        world.flagshippicture = 'cs01'

    world.save(update_fields=[
        'shipyardsinuse', 'flagshiptype', 'flagshiplocation', 'flagshippicture'
    ])

    name = display.personalshipname(shiptype)

    task = Task.objects.get(pk=taskid)
    task.outcome = "Your %s was completed and delivered to your home fleet on %s." % (
        name, now())
    task.save()

    try:
        world.flagshipbuild = False
        world.save(update_fields=['flagshipbuild'])
    except:
        pass
Пример #11
0
def movepersonalship(worldno, taskid, regionfrom, regionto):
    world = World.objects.get(worldid=worldno)
    try:
        task = Task.objects.get(pk=taskid)
    except:
        pass
    else:
        fromname = display.region_display(regionfrom)
        toname = display.region_display(regionto)
        check = (True if world.flagshiptype != 0 else False)
        if not check:
            htmldata = "Your personal ship was destroyed before it could warp from %s to %s!" % (fromname, toname)
            NewsItem.objects.create(target=world, content=htmldata)
            task.outcome = "Your personal ship was destroyed before it could warp from %s to %s!" % (fromname, toname)
        elif world.warpfuel < 5:
            htmldata = news.notenoughpersonal(amount, shiptext, fromname, toname, 'fuel')
            NewsItem.objects.create(target=world, content=htmldata)
            task.outcome = "You did not have enough fuel to warp your personal ship from %s to %s!" % (fromname, toname)
        else:
            attloss = ''
            world.warpfuel = F('warpfuel') - 5
            world.flagshiplocation = regionto
            if world.wardefender.count() > 0 and regionfrom == world.region:
                utilities.contentmentchange(world, -10)
                utilities.stabilitychange(world, -5)
                attloss = 'You were in a defensive war, and you lost perception and stability by sending ships away from your home!'
            world.save(update_fields=['warpfuel','flagshiplocation'])

            task.outcome = "Your personal ship warped from %(from)s to %(to)s on %(time)s. %(att)s" \
                        % {'from':fromname, 'to':toname, 'time':now(), 'att':attloss}

        task.save()
Пример #12
0
def enable_task(name):
    try:
        task = celery_models.PeriodicTask.objects.get(name=name)
        task.enabled = True
        task.save()
        return True
    except celery_models.PeriodicTask.DoesNotExist:
        return False
Пример #13
0
 def __change_status(task_id, status):
     task = Task.objects.get(id=task_id)
     task.status = status
     # save() 方法会设定所有列的 值,而不是只设定 name 列的值。
     # 如果你所处的环境可能同时由其他操作修改其他列,最好只更新需要修改的值。
     # 为此,使用 QuerySet 对象的 update() 方法
     # TODO 需要将不必要的save替换为update
     task.save(update_fields=['status'])
     logging.info("status change to {}".format(status))
Пример #14
0
def mothball(worldno, taskid, amount, shiptype, shiptext, fuelcost, trainingchange, direction):
    world = World.objects.get(worldid=worldno)
    try:
        task = Task.objects.get(pk=taskid)
    except:
        pass
    else:
        if direction == 'plus':
            if 'sendstaging' in world.shipsortprefs:
                movecheck = utilities.movecheck(world, shiptype, amount, 'S')
            else:
                movecheck = utilities.movecheck(world, shiptype, amount, world.region)
            if not movecheck:
                htmldata = news.mothball(amount, shiptext, 'ships', 'plus')
                newsitem = NewsItem(target=world, content=htmldata)
                newsitem.save()
                task.outcome = "You did not have enough ships to mothball %s %s!" % (amount, shiptext)
            else:
                attloss = ''
                if world.wardefender.count() > 0:
                    utilities.contentmentchange(world, -10)
                    utilities.stabilitychange(world, -5)
                    attloss = 'You were in a defensive war and your have lost perception and stability by mothballing ships!'

                if 'sendstaging' in world.shipsortprefs:
                    utilities.movecomplete(world,shiptype,-amount,'S',-trainingchange)
                else:
                    utilities.movecomplete(world,shiptype,-amount,world.region,-trainingchange)
                utilities.movecomplete(world,shiptype,amount,'H',trainingchange)

                task.outcome = "Your %(amount)s %(ship)s successfully entered orbital hangars on %(time)s. %(att)s" \
                            % {'amount':amount, 'ship':shiptext, 'time':now(), 'att':attloss}

        if direction == 'minus':
            movecheck = utilities.movecheck(world, shiptype, amount, 'H')
            if not movecheck:
                htmldata = news.mothball(amount, shiptext, 'ships', 'minus')
                NewsItem.objects.create(target=world, content=htmldata)
                task.outcome = "You did not have enough ships to mothball %s %s!" % (amount, shiptext)
            elif world.warpfuel < fuelcost:
                htmldata = news.mothball(amount, shiptext, 'fuel', 'minus')
                NewsItem.objects.create(target=world, content=htmldata)
                task.outcome = "You did not have enough fuel for your %s %s to re-enter active service!" % (amount, shiptext)
            else:
                world.warpfuel = F('warpfuel') - fuelcost
                world.save(update_fields=['warpfuel'])
                utilities.movecomplete(world,shiptype,-amount,'H',-trainingchange)
                if 'receivestaging' in world.shipsortprefs:
                    utilities.movecomplete(world,shiptype,amount,'S',trainingchange)
                else:
                    utilities.movecomplete(world,shiptype,amount,world.region,trainingchange)

                task.outcome = "Your %(amount)s %(ship)s successfully re-entered active service in the home fleet on %(time)s." \
                            % {'amount':amount, 'ship':shiptext, 'time':now()}

        task.save()
Пример #15
0
def some_task(task_id):
    try:
        task = Task.objects.get(pk=task_id)
        task.status = "r"
        task.save()
        time.sleep(random.randint(0, 10))
        task.status = "f"
        task.save()
    except Task.DoesNotExist:
        print('Task dees not exist in database!')
Пример #16
0
def dispatchQueryEvent(queryevent_id):
    queryevent = QueryEvent.objects.get(pk=queryevent_id)

    task_id = spawnSearch(queryevent)
    task = GroupTask(task_id=task_id, subtask_ids=[''])
    task.save()
    queryevent.search_task = task
    queryevent.dispatched = True
    queryevent.save()

    return queryevent.id
Пример #17
0
def restart_task(name):
    try:
        task = celery_models.PeriodicTask.objects.get(name=name)
        task.enabled = False
        task.save()
        time.sleep(2)
        task.enabled = True
        task.save()
        return True
    except celery_models.PeriodicTask.DoesNotExist:
        return False
Пример #18
0
def start_task(task_id):
    '''
    Marks the task as ongoing.

    Arguments:
        task_id {int} -- id of the task to start
    '''

    task = Task.objects.get(pk=task_id)
    task.status = common_constant.TASK_STATUS.ONGOING
    task.save(update_fields=['status'])
Пример #19
0
def disable_task(name):
    '''
    关闭任务
    '''
    try:
        task = celery_models.PeriodicTask.objects.get(name=name)
        task.enabled = False  # 设置关闭
        task.save()
        return True
    except celery_models.PeriodicTask.DoesNotExist:
        return True
Пример #20
0
def dispatchQueryEvent(queryevent_id):
    queryevent = QueryEvent.objects.get(pk=queryevent_id)
    
    task_id = spawnSearch(queryevent)
    task = GroupTask(   task_id=task_id,
                        subtask_ids=[''] )
    task.save()
    queryevent.search_task = task
    queryevent.dispatched = True
    queryevent.save()        

    return queryevent.id
Пример #21
0
def handle_task(task_name):
    task = Task.objects.get(name=task_name)

    logger.info('Handle task: %s' % str(task))

    if task.complete:
        # Bad
        logger.error('Duplicate Task: %s' % str(task))

    # Good
    with transaction.commit_on_success():
        task.complete = True
        task.save()
Пример #22
0
def handle_task_creation(master_name, num_tasks):
    logger.info('Create tasks, Master:%s, Tasks:%s' % (master_name, num_tasks))

    for x in range(num_tasks):
        # Create task
        with transaction.commit_on_success():
            task_name = '%s.%s' % (master_name, x)
            task = Task(name=task_name)
            task.save()

        # Queue it for "completion"
        queue_task(task)

    logger.info('Create tasks complete, Master:%s' % (master_name))
Пример #23
0
def job(request, job_id):
    task = PingJob.objects.get(id=job_id)

    if 'NEW' == task.status:
        result = AsyncResult(task.task_id)
        if result.ready():
            task.status = 'Completed'
            task.result = '%s is %s' % (task.host,
                    {True: "up", False: "down"}[result.result])
            task.save()

    return render_to_response("job.html",
                RequestContext(request, {"job": task})
           )
Пример #24
0
def job(request, job_id):
    task = PingJob.objects.get(id=job_id)

    if 'NEW' == task.status:
        result = AsyncResult(task.task_id)
        if result.ready():
            task.status = 'Completed'
            task.result = '%s is %s' % (task.host, {
                True: "up",
                False: "down"
            }[result.result])
            task.save()

    return render_to_response("job.html",
                              RequestContext(request, {"job": task}))
Пример #25
0
    def create_PowerStatus(idName):
        print("start to create_PowerStatus")
        #定义一个时间间隔
        schedule, created = IntervalSchedule.objects.get_or_create(
            every=300,
            period=IntervalSchedule.SECONDS,
        )
        print("start to try...except")
        #定义一个时间周期
        # schedule, _ = CrontabSchedule.objects.get_or_create(
        #     minute='30',
        #     hour='*',
        #     day_of_week='*',
        #     day_of_month='*',
        #     month_of_year='*',
        # )
        #创建一个周期性任务,如果查询到就返回,如果没查询到就向数据库加入新的对象
        try:
            print("------------do try----------")
            task, created = PeriodicTask.objects.get_or_create(
                #crontab=schedule,
                name=idName,
                task='HostManager.Tasks.powerStatus',
                interval=schedule,  # we created this above.
                args=json.dumps([idName]),
                # kwargs=json.dumps({
                #     'be_careful': True,
                # }),
                #expires=datetime.datetime.utcnow() + datetime.timedelta(seconds=30)
            )
            if created:
                task.enabled = True
                task.save()
            else:
                task.enabled = True
                task.save()
            return True
        except:
            print("------------do except------------")
            periodicTask = PeriodicTask.objects.get(name=idName)
            periodicTask.enabled = True  # 设置开启
            periodicTask.save()
            return True

        #开启任务

        #PeriodicTask.enabled = True
        print("----------------------------")
Пример #26
0
def editTask(request):
    if request.method == "POST":
        try:
            task = PeriodicTask.objects.get(id=request.POST.get('id'))
            task.name = request.POST.get('name')
            task.interval_id = request.POST.get('interval', None)
            task.crontab_id = request.POST.get('crontab', None)
            # task.args = request.POST.get('args')
            # task.kwargs = request.POST.get('kwargs')
            # task.queue = request.POST.get('queue',None)
            task.expires = request.POST.get('expires', None)
            task.enabled = int(request.POST.get('enabled'))
            task.save()
            return JsonResponse({"code": 200, "data": None, "msg": "修改成功"})
        except Exception, e:
            return JsonResponse({"code": 500, "data": str(e), "msg": "修改失败"})
Пример #27
0
def task_control():
    feeds = Feed.objects.all_published().filter(interval__gte=1)
    task_set = set()
    for feed in feeds:
        task_name = u'opps.feedcrawler.tasks_feed_{}'.format(feed.slug)
        interval, _ = IntervalSchedule.objects.get_or_create(
            every=feed.interval, period='minutes')
        args = json.dumps([
            feed.slug,
        ])
        task, created = PeriodicTask.objects.get_or_create(
            name=task_name,
            defaults={
                'task': 'opps.feedcrawler.tasks.process_feed',
                'interval': interval,
                'args': args
            })
        if created:
            log_it('Adding task {}'.format(task.name))

        if not created:
            changed = False
            if task.interval != interval:
                changed = True
                task.interval = interval
            if task.args != args:
                changed = True
                task.args = args
            if changed:
                log_it('Updating task {}'.format(task.name))
                task.save()

        task_set.add(task.name)

    # Remove inexistent feeds
    tasks_to_remove = PeriodicTask.objects.filter(
        name__startswith='opps.feedcrawler.tasks_feed').exclude(
            name__in=task_set)

    for task in tasks_to_remove:
        log_it('Updating task {}'.format(task.name))
        task.delete()
Пример #28
0
def triggerReceiver(task, results):
    #    if task.status=='FN':
    #        return
    task.parameters['result'] = results
    task.save()
    log.info("ok here we go")
    # log.debug("receive  " + task.taskactiviti.receive)
    username = settings.ACTIVITI_USERNAME
    password = settings.ACTIVITI_PASSWORD
    #http://localhost:8080/activiti-rest/service/process-instance/149/signal
    url = settings.ACTIVITI_URL + "/runtime/executions/" + task.process.processactiviti.instanceID
    data = {}
    # data['activityId'] = task.taskactiviti.receive + "-receive"
    data['action'] = 'signal'
    variables = []
    variables.append(createObject(task.parameters["data_name"], results))
    data['variables'] = variables
    dumps = json.dumps(data)
    log.debug("dumps data %s", dumps)
    response = requests.put(url, data=dumps, auth=HTTPBasicAuth(username, password))
    log.debug(response.text)
Пример #29
0
def update_notify():
    now_time = datetime.now()
    task_list = ItTask.objects.filter(status=0, notify_time__lt=now_time)
    for task in task_list:
        update_status_notify(task)
        # 下一次通知时间
        task.notify_time = task.notify_time + timedelta(days=task.interval)
        task.save()
        
    
        
    
    
    
    
    
    
    
    
    
        
Пример #30
0
def setFormatImage(idTask):
    """
    Is executed concurrently by Celery
    This image search
    """
    task = Task.objects.get(id=idTask)
    i = inspect()
    if i.active() > 3 : #Control about task, only 3 concurrently
        status = 1
        added = False
    else:
        task = Task.objects.get(id=idTask)
        url = task.image.url
        im = Image.open(open(str(task.image.file),'rb'))
        path_new = str(task.image.file).replace ('.png', '.jpg')
        im.save(path_new)
        task.status = 3
        task.image_converted = str(task.image).replace('.png', '.jpg')
        task.save()
        added = True
    return added
Пример #31
0
def create_period_task(task_params, register_task, task_name):
    '''
    @summary:创建周期性任务
    '''
    execute_time = datetime.datetime.now() + datetime.timedelta(minutes=5)
    task, created = PeriodicTask.objects.get_or_create(name=task_name)
    task.task = register_task
    task.kwargs = json.dumps(task_params)
    task.save()
    if created:
        crontab = CrontabSchedule.objects.create(minute=execute_time.minute,
                                                 hour=execute_time.hour,
                                                 day_of_week="*",
                                                 day_of_month=execute_time.day,
                                                 month_of_year=execute_time.month,
                                                 )
        crontab.save()
        task.crontab = crontab
        task.enabled = True
        task.save()
    return True
Пример #32
0
def buildfreighter(worldno, taskid, amount):
    world = World.objects.get(worldid=worldno)

    if world.shipyardsinuse - 2*amount < 0:
        world.shipyardsinuse = 0
    else:
        world.shipyardsinuse = F('shipyardsinuse') - 2*amount
    world.save(update_fields=['shipyardsinuse'])

    if 'prodstaging' in world.shipsortprefs:
        destination = 'staging area'
        utilities.freightermove(world, 'S', amount)
    else:
        destination = 'home fleet'
        utilities.freightermove(world, world.region, amount)

    pluralise = ("freighter was" if amount == 1 else "%s freighters were" % amount)

    task = Task.objects.get(pk=taskid)
    task.outcome = "Your %s completed and delivered to your %s on %s." % (pluralise, destination, now())
    task.save()
Пример #33
0
def handle_task(hash, dictionary, length, task_id):

    password = subprocess.Popen('python C:\\MPI\\brute.py %s %s %s' %
                                (hash, str(dictionary), str(length)),
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE,
                                shell=True)
    while True:
        line = password.stdout.readline()
        print line
        if line.startswith('progress'):
            process_percent = int(line.split()[-1])
            current_task.update_state(
                state='PROGRESS', meta={'process_percent': process_percent})
        else:
            break
    if password:
        task = Task.objects.get(pk=int(task_id))
        task.output_password = line
        task.save()
    return line
Пример #34
0
def moveship(worldno, taskid, amount, shiptype, shiptext, regionfrom, regionto,
             fuelcost, trainingchange):
    world = World.objects.get(worldid=worldno)
    try:
        task = Task.objects.get(pk=taskid)
    except:
        pass  # revoked
    else:
        fromname = display.region_display(regionfrom)
        toname = display.region_display(regionto)
        movecheck = utilities.movecheck(world, shiptype, amount, regionfrom)
        if not movecheck:
            htmldata = "Your attempt to warp %s %s from %s to %s failed as you did not have enough ships." \
                        % (amount, shiptext, regionfrom, regionto)
            NewsItem.objects.create(target=world, content=htmldata)
            task.outcome = "You did not have enough ships to warp %(amount)s %(ship)s from %(from)s to %(to)s!" \
                        % {'amount':amount, 'ship':shiptext, 'from':fromname, 'to':toname}
        elif world.warpfuel < fuelcost:
            htmldata = "Your attempt to warp %s %s from %s to %s failed as you did not have enough fuel." \
                        % (amount, shiptext, regionfrom, regionto)
            NewsItem.objects.create(target=world, content=htmldata)
            task.outcome = "You did not have enough fuel to warp %(amount)s %(ship)s from %(from)s to %(to)s!" \
                        % {'amount':amount, 'ship':shiptext, 'from':fromname, 'to':toname}
        else:
            attloss = ''
            world.warpfuel = F('warpfuel') - fuelcost
            if world.wardefender.count() > 0 and regionfrom == world.region:
                utilities.contentmentchange(world, -10)
                utilities.stabilitychange(world, -5)
                attloss = 'You were in a defensive war, and you lost perception and stability by sending ships away from your home!'
            world.save(update_fields=['warpfuel'])
            utilities.movecomplete(world, shiptype, -amount, regionfrom,
                                   -trainingchange)
            utilities.movecomplete(world, shiptype, amount, regionto,
                                   trainingchange)

            task.outcome = "Your %(amount)s %(ship)s warped from %(from)s to %(to)s on %(time)s. %(att)s" \
                        % {'amount':amount, 'ship':shiptext, 'from':fromname, 'to':toname, 'time':now(), 'att':attloss}

        task.save()
Пример #35
0
def task_run(idnum, request):
    while 1:
        try:
            task = Task.objects.get(id=idnum)
        except:
            continue
        break
    if task.status != 'executed' and task.status != 'running' and task.status != 'NULL':
        hosttag = task.dbtag
        sql = task.sqltext
        mycreatetime = task.create_time
        incept.log_incep_op(sql, hosttag, request, mycreatetime)
        status = 'running'
        task.status = status
        task.update_time = datetime.datetime.now()
        task.save()
        process_runtask.delay(hosttag, sql, task)
        return ''
    elif task.status == 'NULL':
        return 'PLEASE CHECK THE SQL FIRST'
    else:
        return 'Already executed or in running'
Пример #36
0
def task_run(idnum,request):
    while 1:
        try:
            task = Task.objects.get(id=idnum)
        except:
            continue
        break
    if task.status!='executed' and task.status!='running' and task.status!='NULL':
        hosttag = task.dbtag
        sql = task.sqltext
        mycreatetime = task.create_time
        incept.log_incep_op(sql,hosttag,request,mycreatetime)
        status='running'
        task.status = status
        task.update_time = datetime.datetime.now()
        task.save()
        process_runtask.delay(hosttag,sql,task)
        return ''
    elif task.status=='NULL':
        return 'PLEASE CHECK THE SQL FIRST'
    else:
        return 'Already executed or in running'
Пример #37
0
def buildship(worldno, taskid, shiptype, amount, yards):
    world = World.objects.get(worldid=worldno)

    if world.shipyardsinuse - yards*amount < 0:
        world.shipyardsinuse = 0
    else:
        world.shipyardsinuse = F('shipyardsinuse') - yards*amount
    world.save(update_fields=['shipyardsinuse'])

    if 'prodstaging' in world.shipsortprefs:
        destination = 'staging area'
        utilities.movecomplete(world, shiptype, amount, 'S', 0)
    else:
        destination = 'home fleet'
        utilities.movecomplete(world, shiptype, amount, world.region, 0)

    name = utilities.resname(shiptype+10, amount, lower=True)

    pluralise = ("%s was" % name if amount == 1 else "%s %s were" % (amount, name))

    task = Task.objects.get(pk=taskid)
    task.outcome = "Your %s completed and delivered to your %s on %s." % (pluralise, destination, now())
    task.save()
Пример #38
0
def archive_save_hook(sender, instance, created, raw, using,
                      update_fields, **kwargs):
    # check if an svn update or checkout is needed before queuing the task
    updated = False
    if not created:
        # if directory doesn't exist, check it out
        if not os.path.isdir(instance.svn_local_path):
            updated = True
        # if path already exists, check if the svn url has changed
        else:
            client = svn_client()
            svninfo = client.info(instance.svn_local_path, depth=0)
            current_svn_url = svninfo[svninfo.keys()[0]].url
            if current_svn_url != instance.svn:
                updated = True

    if created or updated:
        result = archive_svn_checkout.delay(instance, update=updated)
        task = TaskResult(label='SVN checkout',
            object_id=instance.label,  # will be displayed in task result
            url=reverse('admin:fa_archive_change', args=[instance.pk]), # link in task result
            task_id=result.task_id)
        task.save()
Пример #39
0
def buildfreighter(worldno, taskid, amount):
    world = World.objects.get(worldid=worldno)

    if world.shipyardsinuse - 2 * amount < 0:
        world.shipyardsinuse = 0
    else:
        world.shipyardsinuse = F('shipyardsinuse') - 2 * amount
    world.save(update_fields=['shipyardsinuse'])

    if 'prodstaging' in world.shipsortprefs:
        destination = 'staging area'
        utilities.freightermove(world, 'S', amount)
    else:
        destination = 'home fleet'
        utilities.freightermove(world, world.region, amount)

    pluralise = ("freighter was" if amount == 1 else "%s freighters were" %
                 amount)

    task = Task.objects.get(pk=taskid)
    task.outcome = "Your %s completed and delivered to your %s on %s." % (
        pluralise, destination, now())
    task.save()
Пример #40
0
def movefreighter(worldno, taskid, amount, regionfrom, regionto):
    world = World.objects.get(worldid=worldno)
    try:
        task = Task.objects.get(pk=taskid)
    except:
        pass
    else:
        fromname = display.region_display(regionfrom)
        toname = display.region_display(regionto)
        movecheck = utilities.freightercheck(world, regionfrom, amount)
        if not movecheck:
            htmldata = news.notenough(amount, 'freighters', fromname, toname, 'of them')
            NewsItem.objects.create(target=world, content=htmldata)
            task.outcome = "You did not have enough freighters to warp %(amount)s of them from %(from)s to %(to)s!" \
                        % {'amount':amount, 'from':fromname, 'to':toname}
        else:
            utilities.freightermove(world, regionfrom, -amount)
            utilities.freightermove(world, regionto, amount)

            task.outcome = "Your %(amount)s freighters warped from %(from)s to %(to)s on %(time)s." \
                        % {'amount':amount, 'from':fromname, 'to':toname, 'time':now()}

        task.save()
Пример #41
0
def moveship(worldno, taskid, amount, shiptype, shiptext, regionfrom, regionto, fuelcost, trainingchange):
    world = World.objects.get(worldid=worldno)
    try:
        task = Task.objects.get(pk=taskid)
    except:
        pass # revoked
    else:
        fromname = display.region_display(regionfrom)
        toname = display.region_display(regionto)
        movecheck = utilities.movecheck(world, shiptype, amount, regionfrom)
        if not movecheck:
            htmldata = "Your attempt to warp %s %s from %s to %s failed as you did not have enough ships." \
                        % (amount, shiptext, regionfrom, regionto)
            NewsItem.objects.create(target=world, content=htmldata)
            task.outcome = "You did not have enough ships to warp %(amount)s %(ship)s from %(from)s to %(to)s!" \
                        % {'amount':amount, 'ship':shiptext, 'from':fromname, 'to':toname}
        elif world.warpfuel < fuelcost:
            htmldata = "Your attempt to warp %s %s from %s to %s failed as you did not have enough fuel." \
                        % (amount, shiptext, regionfrom, regionto)
            NewsItem.objects.create(target=world, content=htmldata)
            task.outcome = "You did not have enough fuel to warp %(amount)s %(ship)s from %(from)s to %(to)s!" \
                        % {'amount':amount, 'ship':shiptext, 'from':fromname, 'to':toname}
        else:
            attloss = ''
            world.warpfuel = F('warpfuel') - fuelcost
            if world.wardefender.count() > 0 and regionfrom == world.region:
                utilities.contentmentchange(world, -10)
                utilities.stabilitychange(world, -5)
                attloss = 'You were in a defensive war, and you lost perception and stability by sending ships away from your home!'
            world.save(update_fields=['warpfuel'])
            utilities.movecomplete(world,shiptype,-amount,regionfrom,-trainingchange)
            utilities.movecomplete(world,shiptype,amount,regionto,trainingchange)

            task.outcome = "Your %(amount)s %(ship)s warped from %(from)s to %(to)s on %(time)s. %(att)s" \
                        % {'amount':amount, 'ship':shiptext, 'from':fromname, 'to':toname, 'time':now(), 'att':attloss}

        task.save()
Пример #42
0
def movepersonalship(worldno, taskid, regionfrom, regionto):
    world = World.objects.get(worldid=worldno)
    try:
        task = Task.objects.get(pk=taskid)
    except:
        pass
    else:
        fromname = display.region_display(regionfrom)
        toname = display.region_display(regionto)
        check = (True if world.flagshiptype != 0 else False)
        if not check:
            htmldata = "Your personal ship was destroyed before it could warp from %s to %s!" % (
                fromname, toname)
            NewsItem.objects.create(target=world, content=htmldata)
            task.outcome = "Your personal ship was destroyed before it could warp from %s to %s!" % (
                fromname, toname)
        elif world.warpfuel < 5:
            htmldata = news.notenoughpersonal(amount, shiptext, fromname,
                                              toname, 'fuel')
            NewsItem.objects.create(target=world, content=htmldata)
            task.outcome = "You did not have enough fuel to warp your personal ship from %s to %s!" % (
                fromname, toname)
        else:
            attloss = ''
            world.warpfuel = F('warpfuel') - 5
            world.flagshiplocation = regionto
            if world.wardefender.count() > 0 and regionfrom == world.region:
                utilities.contentmentchange(world, -10)
                utilities.stabilitychange(world, -5)
                attloss = 'You were in a defensive war, and you lost perception and stability by sending ships away from your home!'
            world.save(update_fields=['warpfuel', 'flagshiplocation'])

            task.outcome = "Your personal ship warped from %(from)s to %(to)s on %(time)s. %(att)s" \
                        % {'from':fromname, 'to':toname, 'time':now(), 'att':attloss}

        task.save()
Пример #43
0
def movefreighter(worldno, taskid, amount, regionfrom, regionto):
    world = World.objects.get(worldid=worldno)
    try:
        task = Task.objects.get(pk=taskid)
    except:
        pass
    else:
        fromname = display.region_display(regionfrom)
        toname = display.region_display(regionto)
        movecheck = utilities.freightercheck(world, regionfrom, amount)
        if not movecheck:
            htmldata = news.notenough(amount, 'freighters', fromname, toname,
                                      'of them')
            NewsItem.objects.create(target=world, content=htmldata)
            task.outcome = "You did not have enough freighters to warp %(amount)s of them from %(from)s to %(to)s!" \
                        % {'amount':amount, 'from':fromname, 'to':toname}
        else:
            utilities.freightermove(world, regionfrom, -amount)
            utilities.freightermove(world, regionto, amount)

            task.outcome = "Your %(amount)s freighters warped from %(from)s to %(to)s on %(time)s." \
                        % {'amount':amount, 'from':fromname, 'to':toname, 'time':now()}

        task.save()
Пример #44
0
def send_task_delay():
    projects = models.Project.objects.all()
    tasks = models.Task.objects.all()
    new = datetime.datetime.now()
    for project in projects:
        end_time = project.end_time
        start_time = project.start_time
        if project.status == 1:
            if end_time < new:
                project.status = 2
                project.save()
                project_logs.status_project(project)
                send_project_status2(project)
            project_nodes = models.ProjectNode.objects.filter(project=project)
            for project_node in project_nodes:
                alert_time = project_node.node_alert_time
                if alert_time:
                    if project_node.spare != '1':
                        if alert_time < new:
                            project_node.spare = 1
                            project_node.save()
                            send_alert_project_node(project_node)
        if project.status == 0:
            if project.spare != '1':
                if start_time < new - datetime.timedelta(days=1):
                    project.spare = 1
                    project.save()
                    send_project6(project)
            if start_time < new:
                project.status = 4
                project.save()
                project_logs.status_project(project)
                send_project4(project)
    for task in tasks:
        time = task.end_time
        alert_time = task.alert_time
        if task.status == 1:
            if time < new:
                task.status = 2
                task.save()
                task_logs.g_task(task)
                send_task_status2(task)
            if alert_time:
                if task.spare != '1':
                    if alert_time < new:
                        task.spare = 1
                        task.save()
                        send_alert_task(task)
        if task.status == 0:
            if time < new:
                task.status = 4
                task.save()
                task_logs.g_task(task)
                send_task4(task)
Пример #45
0
def build(branchs, task_id, if_build=True, mode='slam', build_sam=False):
    if not mode == "slam":
        build_sam = True
    if if_build:
        print(branchs)
        task = Task.objects.get(id=task_id)
        task.status = 'build'
        task.save()
        branchs['is_sam'] = build_sam
        compile_code = Compile_code(branchs, task_id)
        print("code path2:", compile_code.compile_info["code_path"])
        try:
            compile_code.run_compile()
            task.status = 'builddone'
            task.save()
        except Exception as e:
            print(e)
            task.status = 'buildfailed'
            task.save()
Пример #46
0
def multiuserassignregion(source_user, project_id, regions, users, group_id):
    project = Project.objects.get(pk=project_id)
    group_name = Group.objects.get(pk=group_id).name
    sites_count = len(regions)
    users_count = len(users)

    task_id = multiuserassignregion.request.id
    task = CeleryTaskProgress.objects.get(task_id=task_id)
    task.content_object = project
    task.description = "Assign " + str(users_count) + " people in " + str(
        sites_count) + " regions."
    task.status = 1
    task.save()
    try:
        with transaction.atomic():
            roles_created = 0
            for region_id in regions:
                if region_id == "0":
                    sites = Site.objects.filter(
                        region__isnull=True,
                        project_id=project_id).values('id')
                else:
                    sites = Site.objects.filter(
                        region_id=region_id,
                        project_id=project_id).values('id')
                for site_id in sites:
                    for user in users:
                        site = Site.objects.filter(pk=site_id['id']).first()
                        if site and site.project_id == project.id:
                            role, created = UserRole.objects.get_or_create(
                                user_id=user,
                                site_id=site_id['id'],
                                project__id=project.id,
                                organization__id=project.organization_id,
                                group_id=group_id,
                                ended_at=None)
                            if created:
                                roles_created += 1

                                # description = "{0} was assigned  as {1} in {2}".format(
                                #     role.user.get_full_name(), role.lgroup.name, role.project)
                                # noti_type = 8

                                # if data.get('group') == "Reviewer":
                                #     noti_type =7

                                # noti = role.logs.create(source=role.user, type=noti_type, title=description,
                                #                         description=description, content_type=site, extra_object=self.request.user,
                                #                         site=role.site)
                                # result = {}
                                # result['description'] = description
                                # result['url'] = noti.get_absolute_url()
                                # ChannelGroup("notify-{}".format(role.organization.id)).send({"text": json.dumps(result)})
                                # ChannelGroup("project-{}".format(role.project.id)).send({"text": json.dumps(result)})
                                # ChannelGroup("site-{}".format(role.site.id)).send({"text": json.dumps(result)})
                                # ChannelGroup("notify-0").send({"text": json.dumps(result)})

                                # Device = get_device_model()
                                # if Device.objects.filter(name=role.user.email).exists():
                                #     message = {'notify_type':'Assign Site', 'site':{'name': site.name, 'id': site.id}}
                                #     Device.objects.filter(name=role.user.email).send_message(message)
        task.status = 2
        task.save()
        if roles_created == 0:
            noti = FieldSightLog.objects.create(
                source=source_user,
                type=23,
                title="Task Completed.",
                content_object=project,
                recipient=source_user,
                extra_message="All " + str(users_count) +
                " users were already assigned as " + group_name + " in " +
                str(sites_count) + " selected regions ")
            result = {}
            result['id'] = noti.id,
            result['source_uid'] = source_user.id,
            result['source_name'] = source_user.username,
            result[
                'source_img'] = source_user.user_profile.profile_picture.url,
            result['get_source_url'] = noti.get_source_url(),
            result['get_event_name'] = noti.get_event_name(),
            result['get_event_url'] = noti.get_event_url(),
            result['get_extraobj_name'] = None,
            result['get_extraobj_url'] = None,
            result['get_absolute_url'] = noti.get_absolute_url(),
            result['type'] = 23,
            result['date'] = str(noti.date),
            result['extra_message'] = "All " + str(
                users_count
            ) + " users were already assigned as " + group_name + " in " + str(
                sites_count) + " selected regions ",
            result['seen_by'] = [],
            ChannelGroup("notif-user-{}".format(source_user.id)).send(
                {"text": json.dumps(result)})

        else:

            noti = FieldSightLog.objects.create(
                source=source_user,
                type=22,
                title="Bulk site User Assign",
                content_object=project,
                organization=project.organization,
                project=project,
                extra_message=str(roles_created) + " new " + group_name +
                " Roles in " + str(sites_count) + " regions ")
            result = {}
            result['id'] = noti.id,
            result['source_uid'] = source_user.id,
            result['source_name'] = source_user.username,
            result[
                'source_img'] = source_user.user_profile.profile_picture.url,
            result['get_source_url'] = noti.get_source_url(),
            result['get_event_name'] = noti.get_event_name(),
            result['get_event_url'] = noti.get_event_url(),
            result['get_extraobj_name'] = None,
            result['get_extraobj_url'] = None,
            result['get_absolute_url'] = noti.get_absolute_url(),
            result['type'] = 22,
            result['date'] = str(noti.date),
            result['extra_message'] = str(
                roles_created) + " new " + group_name + " Roles in " + str(
                    sites_count) + " regions ",
            result['seen_by'] = [],
            ChannelGroup("notif-user-{}".format(source_user.id)).send(
                {"text": json.dumps(result)})

    except Exception as e:
        print 'Site Upload Unsuccesfull. ------------------------------------------%s' % e
        task.status = 3
        task.save()
        noti = FieldSightLog.objects.create(
            source=source_user,
            type=422,
            title="Bulk Region User Assign",
            content_object=project,
            recipient=source_user,
            extra_message=group_name + " for " + str(users_count) +
            " people in " + str(sites_count) + " regions ")
        result = {}
        result['id'] = noti.id,
        result['source_uid'] = source_user.id,
        result['source_name'] = source_user.username,
        result['source_img'] = source_user.user_profile.profile_picture.url,
        result['get_source_url'] = noti.get_source_url(),
        result['get_event_name'] = noti.get_event_name(),
        result['get_event_url'] = noti.get_event_url(),
        result['get_extraobj_name'] = None,
        result['get_extraobj_url'] = None,
        result['get_absolute_url'] = noti.get_absolute_url(),
        result['type'] = 422,
        result['date'] = str(noti.date),
        result['extra_message'] = group_name + " role for " + str(
            users_count) + " people in " + str(sites_count) + " regions ",
        result['seen_by'] = [],
        ChannelGroup("notif-user-{}".format(source_user.id)).send(
            {"text": json.dumps(result)})
        return None
Пример #47
0
def bulkuploadsites(source_user, file, pk):
    time.sleep(5)
    project = Project.objects.get(pk=pk)
    task_id = bulkuploadsites.request.id
    task = CeleryTaskProgress.objects.get(task_id=task_id)
    task.content_object = project
    task.status = 1
    task.save()
    try:
        sites = file.get_records()
        count = len(sites)
        task.description = "Bulk Upload of " + str(count) + " Sites."
        task.save()

        with transaction.atomic():
            i = 0
            for site in sites:
                # time.sleep(0.7)
                site = dict((k, v) for k, v in site.iteritems() if v is not '')
                lat = site.get("longitude", 85.3240)
                long = site.get("latitude", 27.7172)
                location = Point(lat, long, srid=4326)
                type_id = int(site.get("type", "1"))

                region_idf = site.get("region_id", None)
                region_id = None

                if region_idf is not None:
                    region, created = Region.objects.get_or_create(
                        identifier=str(region_idf), project=project)
                    region_id = region.id

                _site, created = Site.objects.get_or_create(
                    identifier=str(site.get("id")),
                    name=site.get("name"),
                    project=project,
                    type_id=type_id,
                    region_id=region_id)
                _site.phone = site.get("phone")
                _site.address = site.get("address")
                _site.public_desc = site.get("public_desc"),
                _site.additional_desc = site.get("additional_desc")
                _site.location = location
                _site.logo = "logo/default-org.jpg"

                meta_ques = project.site_meta_attributes

                myanswers = {}
                for question in meta_ques:
                    myanswers[question['question_name']] = site.get(
                        question['question_name'], "")
                    print site.get(question['question_name'])

                _site.site_meta_attributes_ans = myanswers
                _site.save()
                i += 1
                interval = count / 20
                if i > interval:
                    interval = i + interval
                    bulkuploadsites.update_state(state='PROGRESS',
                                                 meta={
                                                     'current': i,
                                                     'total': count
                                                 })
            task.status = 2
            task.save()
            noti = project.logs.create(source=source_user,
                                       type=12,
                                       title="Bulk Sites",
                                       organization=project.organization,
                                       project=project,
                                       content_object=project,
                                       extra_message=str(count) + " Sites")
            result = {}
            result['id'] = noti.id,
            result['source_uid'] = source_user.id,
            result['source_name'] = source_user.username,
            result[
                'source_img'] = source_user.user_profile.profile_picture.url,
            result['get_source_url'] = noti.get_source_url(),
            result['get_event_name'] = project.name,
            result['get_event_url'] = noti.get_event_url(),
            result['get_extraobj_name'] = None,
            result['get_extraobj_url'] = None,
            result['get_absolute_url'] = noti.get_absolute_url(),
            result['type'] = 12,
            result['date'] = str(noti.date),
            result['extra_message'] = str(count) + " Sites",
            result['seen_by'] = [],
            ChannelGroup("notif-user-{}".format(source_user.id)).send(
                {"text": json.dumps(result)})
            # ChannelGroup("project-{}".format(project.id)).send({"text": json.dumps(result)})

    except Exception as e:
        task.status = 3
        task.save()
        print 'Site Upload Unsuccesfull. %s' % e
        print e.__dict__
        noti = project.logs.create(source=source_user,
                                   type=412,
                                   title="Bulk Sites",
                                   content_object=project,
                                   recipient=source_user,
                                   extra_message=str(count) +
                                   " Sites @error " + u'{}'.format(e.message))
        result = {}
        result['id'] = noti.id,
        result['source_uid'] = source_user.id,
        result['source_name'] = source_user.username,
        result['source_img'] = source_user.user_profile.profile_picture.url,
        result['get_source_url'] = noti.get_source_url(),
        result['get_event_name'] = project.name,
        result['get_event_url'] = noti.get_event_url(),
        result['get_extraobj_name'] = None,
        result['get_extraobj_url'] = None,
        result['get_absolute_url'] = noti.get_absolute_url(),
        result['type'] = 412,
        result['date'] = str(noti.date),
        result['extra_message'] = str(count) + " Sites @error " + u'{}'.format(
            e.message),
        result['seen_by'] = [],
        ChannelGroup("notif-user-{}".format(source_user.id)).send(
            {"text": json.dumps(result)})
        return None
Пример #48
0
def multiuserassignproject(source_user, org_id, projects, users, group_id):
    org = Organization.objects.get(pk=org_id)
    projects_count = len(projects)
    users_count = len(users)

    task_id = multiuserassignproject.request.id
    task = CeleryTaskProgress.objects.get(task_id=task_id)
    task.content_object = org
    task.description = "Assign " + str(users_count) + " people in " + str(
        projects_count) + " projects."
    task.status = 1
    task.save()
    try:
        with transaction.atomic():
            roles_created = 0
            for project_id in projects:
                project = Project.objects.get(pk=project_id)
                for user in users:
                    role, created = UserRole.objects.get_or_create(
                        user_id=user,
                        project_id=project_id,
                        organization_id=org.id,
                        group_id=group_id,
                        ended_at=None)
                    if created:
                        roles_created += 1
                        # description = "{0} was assigned  as Project Manager in {1}".format(
                        # role.user.get_full_name(), role.project)
                        # noti = role.logs.create(source=role.user, type=6, title=description, description=description,
                        #  content_object=role.project, extra_object=self.request.user)
                        # result = {}
                        # result['description'] = description
                        # result['url'] = noti.get_absolute_url()
                        # ChannelGroup("notify-{}".format(role.organization.id)).send({"text": json.dumps(result)})
                        # ChannelGroup("project-{}".format(role.project.id)).send({"text": json.dumps(result)})
                        # ChannelGroup("notify-0").send({"text": json.dumps(result)})
        task.status = 2
        task.save()
        if roles_created == 0:
            noti = FieldSightLog.objects.create(
                source=source_user,
                type=23,
                title="Task Completed.",
                content_object=org,
                recipient=source_user,
                extra_message=str(roles_created) +
                " new Project Manager Roles in " + str(projects_count) +
                " projects ")
            result = {}
            result['id'] = noti.id,
            result['source_uid'] = source_user.id,
            result['source_name'] = source_user.username,
            result[
                'source_img'] = source_user.user_profile.profile_picture.url,
            result['get_source_url'] = noti.get_source_url(),
            result['get_event_name'] = noti.get_event_name(),
            result['get_event_url'] = noti.get_event_url(),
            result['get_extraobj_name'] = None,
            result['get_extraobj_url'] = None,
            result['get_absolute_url'] = noti.get_absolute_url(),
            result['type'] = 23,
            result['date'] = str(noti.date),
            result['extra_message'] = "All " + str(
                users_count
            ) + " people were already assigned as Project Managers in " + str(
                projects_count) + " selected projects ",
            result['seen_by'] = [],
            ChannelGroup("notif-user-{}".format(source_user.id)).send(
                {"text": json.dumps(result)})

        else:
            noti = FieldSightLog.objects.create(
                source=source_user,
                type=21,
                title="Bulk Project User Assign",
                content_object=org,
                organization=org,
                extra_message=str(roles_created) +
                " new Project Manager Roles in " + str(projects_count) +
                " projects ")
            result = {}
            result['id'] = noti.id,
            result['source_uid'] = source_user.id,
            result['source_name'] = source_user.username,
            result[
                'source_img'] = source_user.user_profile.profile_picture.url,
            result['get_source_url'] = noti.get_source_url(),
            result['get_event_name'] = noti.get_event_name(),
            result['get_event_url'] = noti.get_event_url(),
            result['get_extraobj_name'] = None,
            result['get_extraobj_url'] = None,
            result['get_absolute_url'] = noti.get_absolute_url(),
            result['type'] = 21,
            result['date'] = str(noti.date),
            result['extra_message'] = str(
                roles_created) + " new Project Manager Roles in " + str(
                    projects_count) + " projects ",
            result['seen_by'] = [],
            ChannelGroup("notif-user-{}".format(source_user.id)).send(
                {"text": json.dumps(result)})

    except Exception as e:
        task.status = 3
        task.save()
        noti = FieldSightLog.objects.create(source=source_user,
                                            type=421,
                                            title="Bulk Project User Assign",
                                            content_object=org,
                                            recipient=source_user,
                                            extra_message=str(users_count) +
                                            " people in " +
                                            str(projects_count) + " projects ")
        result = {}
        result['id'] = noti.id,
        result['source_uid'] = source_user.id,
        result['source_name'] = source_user.username,
        result['source_img'] = source_user.user_profile.profile_picture.url,
        result['get_source_url'] = noti.get_source_url(),
        result['get_event_name'] = noti.get_event_name(),
        result['get_event_url'] = noti.get_event_url(),
        result['get_extraobj_name'] = None,
        result['get_extraobj_url'] = None,
        result['get_absolute_url'] = noti.get_absolute_url(),
        result['type'] = 421,
        result['date'] = str(noti.date),
        result['extra_message'] = str(users_count) + " people in " + str(
            projects_count) + " projects ",
        result['seen_by'] = [],
        ChannelGroup("notif-user-{}".format(source_user.id)).send(
            {"text": json.dumps(result)})
        return None
Пример #49
0
def run_task(id):
    """Run a task"""

    task = Task.objects.get(pk=id)
    task.start_date = timezone.now()
    task.save()

    out2, __ = subprocess.Popen(['fab', '-d', task.command], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=settings.FABRIC_FOLDER).communicate()
    try:
        description = out2.split('\n')[2]
    except:
        task.stderr = 'Cannot get the command description'
        task.end_date = timezone.now()
        task.save()
        return

    if '[$AG:NeedGestion]' in description:
        needGestion = True
    else:
        needGestion = False

    if '[$AG:NeedKM]' in description:
        needKm = True
    else:
        needKm = False

    if '[$AG:NeedUser]' in description:
        needUser = True
    else:
        needUser = False

    if not task.server.ssh_connection_string_from_gestion:
        task.stderr = 'I don\'t know how to connect to the server'
        task.end_date = timezone.now()
        task.save()
        return

    if needKm and not task.server.keymanger_name:
        task.stderr = 'I need a keymanager name to execute this script.'
        task.end_date = timezone.now()
        task.save()
        return

    if needUser and not task.args:
        task.stderr = 'I need a user to execute this script (to put in args).'
        task.end_date = timezone.now()
        task.save()
        return

    thing_to_set = ''

    if needGestion:
        if task.server.vm_host.keymanger_name == settings.GESTION_VM_NAME:
            thing_to_set += ',gestion_ip=' + settings.GESTION_IP
            thing_to_set += ',gestion_name=' + settings.GESTION_NAME

    if needKm:
        thing_to_set += ',keymanagerName=' + task.server.keymanger_name
        thing_to_set += ',keyManagerUsers=' + task.server.get_users_list().replace(',', ' ')

    if needUser:
        thing_to_set += ',fab_user='******'fab', '--abort-on-prompts', '-p', task.server.random_proxmox_password(), '-H', task.server.get_host_for_fabric(), '--set=' + thing_to_set, task.command], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=settings.FABRIC_FOLDER).communicate()

    task.stdout = out
    task.stderr = err

    task.end_date = timezone.now()
    task.save()
Пример #50
0
def run_task(id):
    """Run a task"""

    task = Task.objects.get(pk=id)
    task.start_date = timezone.now()
    task.save()

    out2, __ = subprocess.Popen(['fab', '-d', task.command], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=settings.FABRIC_FOLDER).communicate()
    try:
        description = out2.split('\n')[2]
    except:
        task.stderr = 'Cannot get the command description'
        task.end_date = timezone.now()
        task.save()
        return

    if '[$AG:NeedGestion]' in description:
        needGestion = True
    else:
        needGestion = False

    if '[$AG:NeedKM]' in description:
        needKm = True
    else:
        needKm = False

    if '[$AG:NeedUser]' in description:
        needUser = True
    else:
        needUser = False

    needKomUser = '******' in description
    needSudo = '[$AG:NeedSudo]' in description
    needMysqlPassword = '******' in description
    needSrvIp = '[$AG:NeedSrvIp]' in description

    if not task.server.ssh_connection_string_from_gestion:
        task.stderr = 'I don\'t know how to connect to the server'
        task.end_date = timezone.now()
        task.save()
        return

    if needKm and not task.server.keymanger_name:
        task.stderr = 'I need a keymanager name to execute this script.'
        task.end_date = timezone.now()
        task.save()
        return

    if needUser and not task.args:
        task.stderr = 'I need a user to execute this script (to put in args).'
        task.end_date = timezone.now()
        task.save()
        return

    if needKomUser or needSudo or needMysqlPassword:
        if not task.args:
            task.stderr = 'I needed special arguements bot task.args wasen\'t set !.'
            task.end_date = timezone.now()
            task.save()
            return
        try:
            import json
            arg_data = json.loads(task.args)
        except:
            task.stderr = 'I needed special arguements bot task.args wasen\'t json !.'
            task.end_date = timezone.now()
            task.save()
            return

        for (arg, name) in ((needKomUser, 'user'), (needSudo, 'sudo'), (needMysqlPassword, 'password')):
            if arg and name not in arg_data:
                task.stderr = 'I needed ' + name + ' but it wasent in args.'
                task.end_date = timezone.now()
                task.save()
                return

    thing_to_set = ''

    if needGestion:
        if task.server.vm_host.keymanger_name == settings.GESTION_VM_NAME:
            thing_to_set += ',gestion_ip=' + settings.GESTION_IP
            thing_to_set += ',gestion_name=' + settings.GESTION_NAME

    if needKm:
        thing_to_set += ',keymanagerName=' + task.server.keymanger_name
        thing_to_set += ',keyManagerUsers=' + task.server.get_users_list().replace(',', ' ')

    if needUser:
        thing_to_set += ',fab_user='******',fab_komuser='******'user']

    if needSudo:
        thing_to_set += ',fab_addsudo=' + arg_data['sudo']

    if needMysqlPassword:
        thing_to_set += ',fab_mysqlpassword='******'password']

    if needSrvIp:
        thing_to_set += ',fab_srvip=' + task.server.internal_ip  

    thing_to_set = thing_to_set[1:]

    out, err = subprocess.Popen(['fab', '--abort-on-prompts', '-p', task.server.random_proxmox_password(), '-H', task.server.get_host_for_fabric(), '--set=' + thing_to_set, task.command], stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=settings.FABRIC_FOLDER).communicate()

    task.stdout = out
    task.stderr = err

    task.end_date = timezone.now()
    task.save()
Пример #51
0
    def add_periodic1(password, usernames, commands, IPaddress, Name,
                      timeIntervalID, timeCycleID):
        if timeIntervalID:
            db_dict = IntervalSchedule.objects.filter(
                id=timeIntervalID).values()[0]
            schedule, created = IntervalSchedule.objects.get_or_create(
                every=db_dict['every'],
                period=db_dict['period'],
            )
            try:
                print("------------do try----------")
                task, created = PeriodicTask.objects.get_or_create(
                    #crontab=schedule,
                    name=Name,
                    task='HostManager.Tasks.CustomTask',
                    interval=schedule,  # we created this above.
                    args=json.dumps([password, usernames, commands, IPaddress])
                    # kwargs=json.dumps({
                    #     'be_careful': True,
                    # }),
                    #expires=datetime.datetime.utcnow() + datetime.timedelta(seconds=30)
                )
                print("------------do try11111----------")
                if created:
                    task.enabled = True
                    task.save()
                    print("111111111111111111")
                else:
                    task.enabled = True
                    task.save()
                    print("2222222222222222")
                return True
            except:
                # print("------------do except------------")
                # periodicTask = PeriodicTask.objects.get(name=Name)
                # periodicTask.enabled = True  # 设置开启
                # periodicTask.save()
                return True
        elif timeCycleID:
            db_dict = CrontabSchedule.objects.filter(
                id=timeCycleID).values()[0]
            schedule, created = CrontabSchedule.objects.get_or_create(
                minute=db_dict['minute'],
                hour=db_dict['hour'],
                day_of_week=db_dict['day_of_week'],
                day_of_month=db_dict['day_of_month'],
                month_of_year=db_dict['month_of_year'],
            )
            try:
                print("------------do try----------")
                task, created = PeriodicTask.objects.get_or_create(
                    crontab=schedule,
                    name=Name,
                    task='HostManager.Tasks.CustomTask',
                    #interval=schedule,  # we created this above.
                    args=json.dumps([password, usernames, commands, IPaddress])
                    # kwargs=json.dumps({
                    #     'be_careful': True,
                    # }),
                    #expires=datetime.datetime.utcnow() + datetime.timedelta(seconds=30)
                )
                print("------------do try11111----------")
                if created:
                    task.enabled = True
                    task.save()
                    print("111111111111111111")
                else:
                    task.enabled = True
                    task.save()
                    print("2222222222222222")
                return True
            except:
                # print("------------do except------------")
                # periodicTask = PeriodicTask.objects.get(name=Name)
                # periodicTask.enabled = True  # 设置开启
                # periodicTask.save()
                return True
        #print("start to try...except")
        #创建一个周期性任务,如果查询到就返回,如果没查询到就向数据库加入新的对象
        # try:
        #     print("------------do try----------")
        #     task, created = PeriodicTask.objects.get_or_create(
        #     #crontab=schedule,
        #     name=Name,
        #     task='HostManager.Tasks.CustomTask',
        #     interval=schedule,  # we created this above.
        #     args=json.dumps(['password','usernames','commands','IPaddress'])
        #     # kwargs=json.dumps({
        #     #     'be_careful': True,
        #     # }),
        #     #expires=datetime.datetime.utcnow() + datetime.timedelta(seconds=30)
        #     )
        #     print("------------do try11111----------")
        #     if created:
        #         task.enabled = True
        #         task.save()
        #         print("111111111111111111")
        #     else:
        #         task.enabled = True
        #         task.save()
        #         print("2222222222222222")
        #     return True
        # except:
        #     # print("------------do except------------")
        #     # periodicTask = PeriodicTask.objects.get(name=Name)
        #     # periodicTask.enabled = True  # 设置开启
        #     # periodicTask.save()
        #     return True

        #开启任务

        #PeriodicTask.enabled = True
        print("----------------------------")
Пример #52
0
def task_model(request):
    if request.method == "GET":
        #获取注册的任务
        regTaskList = []
        for task in list(keys(cTasks)):
            if task.startswith('OpsManage.tasks.ansible') or task.startswith('OpsManage.tasks.sched'):
                regTaskList.append(task)
        try:
            crontabList = CrontabSchedule.objects.all().order_by("-id")
            intervalList = IntervalSchedule.objects.all().order_by("-id")
            taskList = PeriodicTask.objects.all().order_by("-id")
        except:
            crontabList = []
            intervalList = []
            taskList = []
        return render(request,'task/task_model.html',{"user":request.user,"crontabList":crontabList,
                                              "intervalList":intervalList,"taskList":taskList,
                                              "regTaskList":regTaskList})  
    elif request.method == "POST":
        op = request.POST.get('op') 
        if op in ['addInterval','delInterval','addTask','editTask','delTask'] and request.user.has_perm('djcelery.change_periodictask'): 
            if op == 'addInterval':
                try:
                    IntervalSchedule.objects.create(every=request.POST.get('every'),period=request.POST.get('period'))
                    return  JsonResponse({"code":200,"data":None,"msg":"添加成功"})
                except:
                    return  JsonResponse({"code":500,"data":None,"msg":"添加失败"})    
            elif op == 'delInterval':
                try:
                    IntervalSchedule.objects.get(id=request.POST.get('id')).delete()
                    return  JsonResponse({"code":200,"data":None,"msg":"删除成功"})
                except:
                    return  JsonResponse({"code":500,"data":None,"msg":"删除失败"})
            elif op == 'addTask':
                try:
                    PeriodicTask.objects.create(name=request.POST.get('name'),
                                                interval_id=request.POST.get('interval',None),
                                                task=request.POST.get('task',None),
                                                crontab_id=request.POST.get('crontab',None),
                                                args = request.POST.get('args','[]'),
                                                kwargs = request.POST.get('kwargs','{}'),
                                                queue = request.POST.get('queue',None),
                                                enabled = int(request.POST.get('enabled',1)),
                                                expires = request.POST.get('expires',None)
                                                      )
                    return  JsonResponse({"code":200,"data":None,"msg":"添加成功"})
                except Exception,e:
                    return  JsonResponse({"code":500,"data":str(e),"msg":"添加失败"})    
            elif op == 'delTask':
                try:
                    PeriodicTask.objects.get(id=request.POST.get('id')).delete()
                    return  JsonResponse({"code":200,"data":None,"msg":"删除成功"})
                except:
                    return  JsonResponse({"code":500,"data":None,"msg":"删除失败"})
            elif op == 'editTask':
                try:
                    task = PeriodicTask.objects.get(id=request.POST.get('id'))
                    task.name = request.POST.get('name')
                    task.interval_id = request.POST.get('interval',None)
                    task.crontab_id = request.POST.get('crontab',None)
                    task.args = request.POST.get('args')
                    task.kwargs = request.POST.get('kwargs')
                    task.queue = request.POST.get('queue',None)
                    task.expires = request.POST.get('expires',None)
                    task.enabled = int(request.POST.get('enabled'))
                    task.save()
                    return  JsonResponse({"code":200,"data":None,"msg":"修改成功"})
                except Exception,e:
                    return  JsonResponse({"code":500,"data":str(e),"msg":"修改失败"})                             
Пример #53
0
     #    print '\t', k, ' : ', v
     print 'score: ', model.score(X, y)
 except Exception, e:
     print 'except: ', e
     traceback.print_exc()
     return
 start_time = time.time()
 print "predict: data size: %d" % len(test_X)
 predicted = model.predict_proba(test_X)
 #predicted = map(lambda x: x[1], predicted)
 predict_spent = int(time.time() - start_time)
 print "predict done, spent %s seconds." % predict_spent
 completed = timezone.now()
 task = ClassificationTask.create(char, u'', train_count, predict_count,
                                  started, completed, fetch_spent, training_spent, predict_spent, auto_apply)
 task.save()
 compare_results = []
 results = []
 for i in range(predict_count):
     new_accuracy = int(predicted[i][1] * 1000)
     origin_accuracy = test_accuracy_lst[i]
     difference = abs(new_accuracy - origin_accuracy)
     results.append( (test_char_id_lst[i], origin_accuracy, new_accuracy, difference) )
 results.sort(key=itemgetter(3), reverse=True)
 # selected_count = max(predict_count/10, 1000)
 selected_count = max(predict_count/2, 1000)
 for char_id, origin_accuracy, new_accuracy, difference in results[:selected_count]:
     if difference != 0:
         result = ClassificationCompareResult.create(task, char_id, origin_accuracy, new_accuracy)
         compare_results.append(result)
 if len(compare_results) > 0: