Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RuntimeJob model #1177

Merged
merged 9 commits into from
Jan 25, 2024
Merged

Add RuntimeJob model #1177

merged 9 commits into from
Jan 25, 2024

Conversation

akihikokuroda
Copy link
Collaborator

@akihikokuroda akihikokuroda commented Jan 19, 2024

Summary

Fix #1163

Details and comments

This PR has

  1. add RuntimeJob model
  2. add API: /api/v1/runtime_jobs/utils/create_runtimejob/ with job id and runtime job id as argument. It creates a RuntimeJob table entry
  3. add API: /api/v1/runtime_jobs/utils/list_runtimejob/ with job id as argument. It lists all RuntimeJob entries for the job id.

Signed-off-by: Akihiko Kuroda <akihikokuroda2020@gmail.com>
@psschwei
Copy link
Collaborator

couple of nits on the API:

  • api/v1/runtime_jobs/utils/create_runtimejob/: the runtimejob in the last part is a bit redundant, so can we drop that?
  • using api/v1/runtime_jobs/utils/create/ to list all jobs seems a little counter-intuitive... what about using a different, more explicit path (maybe something like .../list/ or .../jobs/)?

@akihikokuroda
Copy link
Collaborator Author

akihikokuroda commented Jan 23, 2024

@psschwei Thanks for review. I'll update them to

api/v1/runtime_jobs/utils/add/
api/v1/runtime_jobs/utils/issued/

add: add runtime job id to the middleware job.
issued: return runtime job ids issued in the middleware job

@psschwei
Copy link
Collaborator

Thinking about this in the context of #1164 : would these APIs make sense as a subfunction of jobs, i.e. something like api/vi/jobs/<job_id>/runtime_jobs/issued/ ? If that's a long conversation (as we've got a few other API issues/PRs in the works), we could also split the APIs into a separate PR and merge the model creation here.

WDYT? I don't have strong opinions here, just know that changing an API once it's out there is not the most fun thing in the world 😄


queryset = RuntimeJob.objects.all()
serializer_class = v1_serializers.RuntimeJobSerializer
permission_classes = [permissions.IsAuthenticated, IsOwner]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsOwner might not work here because it is checking owner or author field of object. But RuntimeJob does not have that.

We can modify IsOwner permission class a little bit to act differently based on object type. Something like

# Instaed of this
class IsOwner(permissions.BasePermission):
    """
    Custom permission to only allow owners of an object to edit it.
    """

    def has_object_permission(self, request, view, obj):
        return obj.author == request.user
        
# do something like
class IsOwner(permissions.BasePermission):
    """
    Custom permission to only allow owners of an object to edit it.
    """

    def has_object_permission(self, request, view, obj):
        if isinstance(obj, RuntimeJob):
            return obj.job.author == request.user
        return obj.author == request.user

@@ -468,3 +472,41 @@ def upload(self, request): # pylint: disable=invalid-name
destination.write(chunk)
return Response({"message": file_path})
return Response("server error", status=status.HTTP_500_INTERNAL_SERVER_ERROR)


class RuntimeJobViewSet(viewsets.ModelViewSet): # pylint: disable=too-many-ancestors
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can use here standart django model viewset and it will give us CRUD on RuntimeJob object.

Or alternatevely, if we want to keep this view, we should use RuntimeJobSerializer to validate requests.

        serializer = RuntimeJobSerializer(data=request.data)
        serializer.is_valid(raise_exception=True) # or better handle exception and return response with explanation

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IceKhan13 I thought these APIs are convenient but both APIs can be replaces by one CRUD API call. I'll take it out.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@IceKhan13 On the second thought, the API getting the list of runtime jobs for the middleware job may may be useful when the RuntimeJob table gets huge. WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My comment was about using default django api view. For RuntimeJob model, which gives all crud with validation out of the box.

https://www.django-rest-framework.org/api-guide/viewsets/#modelviewset

Basically

class RuntimeJobViewSet(viewsets.ModelViewSet):
    serializer_class = RuntimeJobSerializer
    permission_classes = [...]

    def get_queryset(self):
        return RuntimeJob.object.filter(job__author=self.request.user)
        
    ... # add here insert if needed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But let's merge this PR and add API view in separate one.

@akihikokuroda
Copy link
Collaborator Author

I look out APIs. This PR basically has model change only.

Comment on lines 13 to 14
if isinstance(obj, RuntimeJob):
return obj.job.author == request.user
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this left over from the API part that was removed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I take out. Thanks!

Copy link
Member

@IceKhan13 IceKhan13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's merge this PR and add API view for RuntimeJob in separate PR

Thank you, Aki!

@akihikokuroda akihikokuroda merged commit 9267021 into Qiskit:main Jan 25, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Gateway: add runtime job entry to serverless job object
3 participants