Search in sources :

Example 6 with TemporaryResource

use of com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResource in project ma-modules-public by infiniteautomation.

the class MangoTaskTemporaryResourceManager method scheduleTask.

private void scheduleTask(TemporaryResource<T, AbstractRestV2Exception> resource) {
    TaskData tasks = (TaskData) resource.getData();
    // TODO Mango 3.4 keep user inside the resource isntead of user id?
    // maybe change the user inside DataPointRestController bulk operation lambda function to get user from background context
    User user = UserDao.instance.get(resource.getUserId());
    if (user == null) {
        AccessDeniedException error = new AccessDeniedException();
        resource.safeError(error);
        return;
    }
    tasks.mainTask = new HighPriorityTask("Temporary resource " + resource.getResourceType() + " " + resource.getId()) {

        @Override
        public void run(long runtime) {
            try {
                BackgroundContext.set(user);
                resource.getTask().run(resource);
            } catch (Exception e) {
                AbstractRestV2Exception error = MangoTaskTemporaryResourceManager.this.mapException(e);
                resource.safeError(error);
            } finally {
                BackgroundContext.remove();
            }
        }

        @Override
        public void rejected(RejectedTaskReason reason) {
            super.rejected(reason);
            TranslatableMessage msg = null;
            switch(reason.getCode()) {
                case RejectedTaskReason.POOL_FULL:
                    msg = new TranslatableMessage("rest.error.rejectedTaskPoolFull");
                    break;
                case RejectedTaskReason.TASK_QUEUE_FULL:
                    msg = new TranslatableMessage("rest.error.rejectedTaskQueueFull");
                    break;
                case RejectedTaskReason.CURRENTLY_RUNNING:
                    msg = new TranslatableMessage("rest.error.rejectedTaskAlreadyRunning");
                    break;
            }
            ServerErrorException ex = msg == null ? new ServerErrorException() : new ServerErrorException(msg);
            AbstractRestV2Exception error = MangoTaskTemporaryResourceManager.this.mapException(ex);
            resource.safeError(error);
        }
    };
    Common.backgroundProcessing.execute(tasks.mainTask);
    this.scheduleTimeout(resource);
}
Also used : AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) HighPriorityTask(com.serotonin.m2m2.util.timeout.HighPriorityTask) User(com.serotonin.m2m2.vo.User) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ServerErrorException(com.infiniteautomation.mango.rest.v2.exception.ServerErrorException) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) ServerErrorException(com.infiniteautomation.mango.rest.v2.exception.ServerErrorException) AccessDeniedException(com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException) RejectedTaskReason(com.serotonin.timer.RejectedTaskReason)

Aggregations

AbstractRestV2Exception (com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception)5 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)5 BadRequestException (com.infiniteautomation.mango.rest.v2.exception.BadRequestException)4 User (com.serotonin.m2m2.vo.User)4 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)4 HttpHeaders (org.springframework.http.HttpHeaders)4 ResponseEntity (org.springframework.http.ResponseEntity)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 AccessDeniedException (com.infiniteautomation.mango.rest.v2.exception.AccessDeniedException)3 TemporaryResourceStatus (com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResource.TemporaryResourceStatus)3 RQLToObjectListQuery (com.infiniteautomation.mango.db.query.pojo.RQLToObjectListQuery)2 BulkRequest (com.infiniteautomation.mango.rest.v2.bulk.BulkRequest)2 BulkResponse (com.infiniteautomation.mango.rest.v2.bulk.BulkResponse)2 VoAction (com.infiniteautomation.mango.rest.v2.bulk.VoAction)2 NotFoundRestException (com.infiniteautomation.mango.rest.v2.exception.NotFoundRestException)2 DataPointModel (com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel)2 MangoTaskTemporaryResourceManager (com.infiniteautomation.mango.rest.v2.temporaryResource.MangoTaskTemporaryResourceManager)2 TemporaryResource (com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResource)2 TemporaryResourceManager (com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResourceManager)2 TemporaryResourceStatusUpdate (com.infiniteautomation.mango.rest.v2.temporaryResource.TemporaryResourceStatusUpdate)2