Changeset 1581:4841a9e393c7

Show
Ignore:
Timestamp:
10/28/08 14:31:02 (2 months ago)
Author:
Greg Von Kuster <greg@bx.psu.edu>
branch:
default
Message:

Purge metadata files associated with a dataset when the dataset is purged. Also remembered log.exception logs the exception, so corrected a few things in jobs.init.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • lib/galaxy/jobs/__init__.py

    r1578 r1581  
    6363                    self.use_policy = False 
    6464                    log.info("Scheduler policy not defined as expected, defaulting to FIFO") 
    65             except AttributeError, detail : # try may throw AttributeError 
     65            except AttributeError, detail: # try may throw AttributeError 
    6666                self.use_policy = False 
    6767                log.exception("Error while loading scheduler policy class, defaulting to FIFO") 
     
    118118            try: 
    119119                self.monitor_step() 
    120             except Exception, e
    121                 log.exception( "Exception in monitor_step: %s" % str( e )
     120            except
     121                log.exception( "Exception in monitor_step"
    122122            # Sleep 
    123123            self.sleeper.sleep( 1 ) 
     
    185185                    log.error( msg ) 
    186186            except Exception, e: 
    187                 msg = "failure running job %d: %s" % ( job.job_id, str( e ) ) 
    188                 job.info = msg 
    189                 log.exception( msg ) 
     187                job.info = "failure running job %d: %s" % ( job.job_id, str( e ) ) 
     188                log.exception( "failure running job %d" % job.job_id ) 
    190189        # Update the waiting list 
    191190        self.waiting = new_waiting 
     
    202201                    break 
    203202                except Exception, e: # if something else breaks while dispatching 
    204                     msg = "failure running job %d: %s" % ( sjob.job_id, str( e ) ) 
    205                     job.fail( msg ) 
    206                     log.exception( msg ) 
     203                    job.fail( "failure running job %d: %s" % ( sjob.job_id, str( e ) ) ) 
     204                    log.exception( "failure running job %d" % sjob.job_id ) 
    207205             
    208206    def put( self, job_id, tool ): 
     
    474472            if self.working_directory is not None: 
    475473                os.rmdir( self.working_directory )  
    476         except Exception, e
    477             log.exception( "Unable to cleanup job %s, exception: %s" % ( str( self.job_id ), str( e ) )
     474        except
     475            log.exception( "Unable to cleanup job %d" % self.job_id
    478476         
    479477    def get_command_line( self ): 
     
    574572            try: 
    575573                self.monitor_step() 
    576             except Exception, e
    577                 log.exception( "Exception in monitor_step: %s" % str( e )
     574            except
     575                log.exception( "Exception in monitor_step"
    578576            # Sleep 
    579577            self.sleeper.sleep( 1 ) 
  • scripts/cleanup_datasets/cleanup_datasets.py

    r1407 r1581  
    4848    h = app.model.History 
    4949    d = app.model.Dataset 
     50    m = app.model.MetadataFile 
    5051    cutoff_time = datetime.utcnow() - timedelta( days=options.days ) 
    5152    now = strftime( "%Y-%m-%d %H:%M:%S" ) 
     
    6465        else: 
    6566            print "# Datasets will NOT be removed from disk...\n" 
    66         purge_histories( h, d, cutoff_time, options.remove_from_disk ) 
     67        purge_histories( h, d, m, cutoff_time, options.remove_from_disk ) 
    6768    elif options.info_purge_datasets: 
    6869        info_purge_datasets( d, cutoff_time ) 
     
    7273        else: 
    7374            print "# Datasets will NOT be removed from disk...\n" 
    74         purge_datasets( d, cutoff_time, options.remove_from_disk ) 
     75        purge_datasets( d, m, cutoff_time, options.remove_from_disk ) 
    7576    sys.exit(0) 
    7677 
     
    8081    dataset_count = 0 
    8182    where = ( h.table.c.user_id==None ) & ( h.table.c.deleted=='f' ) & ( h.table.c.update_time < cutoff_time ) 
    82     histories = h.query().filter( where ).options( eagerload( 'active_datasets' ) ) 
     83    histories = h.query().filter( where ).options( eagerload( 'active_datasets' ) ).all() 
    8384 
    8485    print '# The following datasets and associated userless histories will be deleted' 
     
    106107    print '# The following datasets and associated userless histories have been deleted' 
    107108    start = time.clock() 
    108     histories = h.query().filter( h_where ).options( eagerload( 'active_datasets' ) ) 
     109    histories = h.query().filter( h_where ).options( eagerload( 'active_datasets' ) ).all() 
    109110    for history in histories: 
    110111        for dataset_assoc in history.active_datasets: 
     
    112113                # Mark all datasets as deleted 
    113114                d_where = ( d.table.c.id==dataset_assoc.dataset_id ) 
    114                 datasets = d.query().filter( d_where ) 
     115                datasets = d.query().filter( d_where ).all() 
    115116                for dataset in datasets: 
    116117                    if not dataset.deleted: 
     
    140141    print '# The following datasets and associated deleted histories will be purged' 
    141142    start = time.clock() 
    142     histories = h.query().filter( h_where ).options( eagerload( 'datasets' ) )    
     143    histories = h.query().filter( h_where ).options( eagerload( 'datasets' ) ).all() 
    143144    for history in histories: 
    144145        for dataset_assoc in history.datasets: 
     
    146147            if dataset_assoc.deleted: 
    147148                d_where = ( d.table.c.id==dataset_assoc.dataset_id ) 
    148                 datasets = d.query().filter( d_where ) 
     149                datasets = d.query().filter( d_where ).all() 
    149150                for dataset in datasets: 
    150151                    if dataset.purgable and not dataset.purged: 
     
    161162    print "Elapsed time: ", stop - start, "\n" 
    162163 
    163 def purge_histories( h, d, cutoff_time, remove_from_disk ): 
     164def purge_histories( h, d, m, cutoff_time, remove_from_disk ): 
    164165    # Purges deleted histories whose update_time is older than the cutoff_time. 
    165166    # The datasets associated with each history are also purged. 
     
    173174    print '# The following datasets and associated deleted histories have been purged' 
    174175    start = time.clock() 
    175     histories = h.query().filter( h_where ).options( eagerload( 'datasets' ) )       
     176    histories = h.query().filter( h_where ).options( eagerload( 'datasets' ) ).all()     
    176177    for history in histories: 
    177178        errors = False 
     
    179180            if dataset_assoc.deleted: 
    180181                d_where = ( d.table.c.id==dataset_assoc.dataset_id ) 
    181                 datasets = d.query().filter( d_where ) 
     182                datasets = d.query().filter( d_where ).all() 
    182183                for dataset in datasets: 
    183184                    if dataset.purgable and not dataset.purged: 
     
    187188                        if remove_from_disk: 
    188189                            dataset.flush() 
    189                             errmsg = purge_dataset( dataset
     190                            errmsg = purge_dataset( dataset, m
    190191                            if errmsg: 
    191192                                errors = True 
     
    197198                            dataset.flush() 
    198199                            print "%s" % dataset.file_name 
     200                            # Mark all associated MetadataFiles as deleted and purged 
     201                            print "The following metadata files associated with dataset '%s' have been marked purged" % dataset.file_name 
     202                            for hda in dataset.history_associations: 
     203                                for metadata_file in m.filter( m.table.c.hda_id==hda.id ).all(): 
     204                                    metadata_file.deleted = True 
     205                                    metadata_file.purged = True 
     206                                    metadata_file.flush() 
     207                                    print "%s" % metadata_file.file_name() 
    199208                        dataset_count += 1 
    200209                        try: 
     
    219228    print '# The following deleted datasets will be purged'     
    220229    start = time.clock() 
    221     datasets = d.query().filter( where ) 
     230    datasets = d.query().filter( where ).all() 
    222231    for dataset in datasets: 
    223232        print "%s" % dataset.file_name 
     
    231240    print "Elapsed time: ", stop - start, "\n" 
    232241 
    233 def purge_datasets( d, cutoff_time, remove_from_disk ): 
     242def purge_datasets( d, m, cutoff_time, remove_from_disk ): 
    234243    # Purges deleted datasets whose update_time is older than cutoff_time.  Files may or may 
    235244    # not be removed from disk. 
     
    241250    print '# The following deleted datasets have been purged' 
    242251    start = time.clock() 
    243     datasets = d.query().filter( where ) 
     252    datasets = d.query().filter( where ).all() 
    244253    for dataset in datasets: 
    245254        file_size = dataset.file_size 
    246255        if remove_from_disk: 
    247             errmsg = purge_dataset( dataset
     256            errmsg = purge_dataset( dataset, m
    248257            if errmsg: 
    249258               print errmsg 
     
    256265            dataset.flush() 
    257266            print "%s" % dataset.file_name 
     267            # Mark all associated MetadataFiles as deleted and purged 
     268            print "The following metadata files associated with dataset '%s' have been marked purged" % dataset.file_name 
     269            for hda in dataset.history_associations: 
     270                for metadata_file in m.filter( m.table.c.hda_id==hda.id ).all(): 
     271                    metadata_file.deleted = True 
     272                    metadata_file.purged = True 
     273                    metadata_file.flush() 
     274                    print "%s" % metadata_file.file_name() 
    258275            dataset_count += 1 
    259276        try: 
     
    267284    print "Elapsed time: ", stop - start, "\n" 
    268285 
    269 def purge_dataset( dataset ): 
     286def purge_dataset( dataset, m ): 
    270287    # Removes the file from disk and updates the database accordingly. 
    271288    if dataset.deleted: 
    272289        # Remove files from disk and update the database 
    273         purgable = False 
    274290        try: 
    275291            dataset.purged = True 
     
    285301                    break #only purge when not shared 
    286302            else: 
     303                # Remove dataset file from disk 
    287304                os.unlink( dataset.file_name ) 
    288                 purgable = True 
     305                # Mark all associated MetadataFiles as deleted and purged and remove them from disk 
     306                print "The following metadata files associated with dataset '%s' have been purged" % dataset.file_name 
     307                for hda in dataset.history_associations: 
     308                    for metadata_file in m.filter( m.table.c.hda_id==hda.id ).all(): 
     309                        os.unlink( metadata_file.file_name() ) 
     310                        metadata_file.deleted = True 
     311                        metadata_file.purged = True 
     312                        metadata_file.flush() 
     313                        print "%s" % metadata_file.file_name() 
     314                try: 
     315                    # Remove associated extra files from disk if they exist 
     316                    os.unlink( dataset.extra_files_path ) 
     317                except: 
     318                    pass 
    289319        except Exception, exc: 
    290320            return "# Error, exception: %s caught attempting to purge %s\n" %( str( exc ), dataset.file_name ) 
    291         try: 
    292             if purgable: 
    293                 os.unlink( dataset.extra_files_path ) 
    294         except: 
    295             pass 
    296321    else: 
    297322        return "# Error: '%s' has not previously been deleted, so it cannot be purged\n" %dataset.file_name