#!/usr/bin/env python
# -*- coding: utf-8 -*-

###############################################################################
#  Copyright Kitware Inc.
#
#  Licensed under the Apache License, Version 2.0 ( the "License" );
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
###############################################################################

from girder import events
from girder.constants import AccessType
from girder.plugin import getPlugin, GirderPlugin
from girder_jobs.models.job import Job

from .api.worker import Worker
from . import event_handlers


class WorkerPlugin(GirderPlugin):
    DISPLAY_NAME = 'Worker'
    CLIENT_SOURCE_PATH = 'web_client'

    def load(self, info):
        getPlugin('jobs').load(info)

        info['apiRoot'].worker = Worker()

        events.bind('jobs.schedule', 'worker', event_handlers.schedule)
        events.bind('jobs.status.validate', 'worker', event_handlers.validateJobStatus)
        events.bind('jobs.status.validTransitions', 'worker', event_handlers.validTransitions)
        events.bind('jobs.cancel', 'worker', event_handlers.cancel)
        events.bind('model.job.save.after', 'worker', event_handlers.attachJobInfoSpec)
        events.bind('model.job.save', 'worker', event_handlers.attachParentJob)
        Job().exposeFields(AccessType.SITE_ADMIN, {'celeryTaskId', 'celeryQueue'})

@@ -786,124 +786,1105 @@
 ##%0A%0A
-from 
+# Pre-
 girder 
-import events%0Afrom girder.constants import AccessType%0Afrom girder.plugin import getPlugin, GirderPlugin%0A
+3.0 plugins supported the same entrypoint (girder.plugin) to register%0A# themselves.  If a Girder 2.x instance is installed with this package, it will%0A# attempt to load this entrypoint and fail with an error message.  Due to how%0A# pre-3.0 plugins worked, it still falls back to the built-in version of worker%0A# and functions correctly.  The logic below eliminates that error message.%0A%0A# First ensure girder is installed, otherwise it doesn't make sense to import%0A# this module at all.%0Aimport girder  # noqa%0A%0Afrom girder_worker import logger%0A%0A# Detect if girder%3E=3 is installed by checking an import that was added in 3.0.%0A_isGirder3 = False%0Atry:%0A    from girder.plugin import getPlugin, GirderPlugin%0A    _isGirder3 = True%0Aexcept ImportError:%0A    logger.info('Girder 2.x is detected skipping incompatible entrypoint definition.')%0A%0A%0A# If girder%3E=3 is installed, it is safe to continue defining the plugin class, otherwise%0A# just define a dummy class to prevent error messages from propagating.%0Aif _isGirder3:%0A    from girder import events%0A    from girder.constants import AccessType%0A    
 from
@@ -1919,16 +1919,20 @@
 rt Job%0A%0A
+    
 from .ap
@@ -1954,16 +1954,20 @@
  Worker%0A
+    
 from . i
@@ -1988,17 +1988,20 @@
 ndlers%0A%0A
-%0A
+    
 class Wo
@@ -2026,16 +2026,20 @@
 lugin):%0A
+    
     DISP
@@ -2062,16 +2062,20 @@
 er'%0A    
+ 
+   
 CLIENT_S
@@ -2101,16 +2101,20 @@
 lient'%0A%0A
+    
     def 
@@ -2139,16 +2139,20 @@
         
+ 
+   
 getPlugi
@@ -2173,16 +2173,20 @@
 (info)%0A%0A
+    
         
@@ -2220,32 +2220,36 @@
 rker()%0A%0A        
+  
+  
 events.bind('job
@@ -2296,16 +2296,20 @@
 hedule)%0A
+    
         
@@ -2388,32 +2388,36 @@
 Status)%0A        
+  
+  
 events.bind('job
@@ -2487,32 +2487,36 @@
 itions)%0A        
+    
 events.bind('job
@@ -2559,32 +2559,36 @@
 cancel)%0A        
+   
+ 
 events.bind('mod
@@ -2651,32 +2651,36 @@
 foSpec)%0A        
+    
 events.bind('mod
@@ -2735,16 +2735,20 @@
 entJob)%0A
+    
         
@@ -2822,8 +2822,59 @@
 ueue'%7D)%0A
+else:%0A    class WorkerPlugin(object):%0A        pass%0A

Add error handling logic when running with girder 2.x