#!/bin/bash
#
# ./startMaintenance30 runs a Seaside 3.0 maintenance vm
#    the class WAGemStoneMaintenanceTask determines which 
#    maintenance task will be performed
#

if [ -s $GEMSTONE/seaside/etc/gemstone.secret ]; then
    . $GEMSTONE/seaside/etc/gemstone.secret
else
    echo 'Missing password file $GEMSTONE/seaside/etc/gemstone.secret'
    exit 1
fi

cat << EOF | nohup $GEMSTONE/bin/topaz -u "Mtce-$GEMSTONE_NAME" -l -e $GEMSTONE/seaside/etc/maintenance30.conf 2>&1 > $GEMSTONE_LOGDIR/maintenance_gem.log &

set user DataCurator pass $GEMSTONE_CURATOR_PASS gems $GEMSTONE_NAME

display oops
iferror where

login

run
"record gems pid in the pid file"
| file |
(GsFile isServerDirectory: '$GEMSTONE_DATADIR') ifFalse: [ ^nil ].
file := GsFile openWriteOnServer: '$GEMSTONE_DATADIR/maintenance_gem.pid'.
file nextPutAll: (System gemVersionReport at: 'processId') printString.
file cr.
file close.
(ObjectLogEntry
  info: 'MTCE: startup'
  object: 'pid: ', (System gemVersionReport at: 'processId') printString) addToLog.
System commitTransaction 
    ifFalse: [ 
      System abortTransaction.
      nil error: 'Could not commit ObjectLog entry' ].
%

run
| x |
"set _cacheName: for Gem to get unique id in statmon output. see http://code.google.com/p/glassdb/issues/detail?id=132"
System _cacheName: ((x := 'Maintenance-' , (GsSession serialOfSession: System session) printString , '-' , System myUserProfile userId) copyFrom: 1 to: (x size min: 31)).
%

run
| count |
GsProcess usingNativeCode not
  ifTrue: [
    "Enable remote Breakpoing handling"
    Breakpoint trappable: true.
    GemToGemAnnouncement installStaticHandler.
    GemToGemAnnouncement cleanRegisteredSessions. "call in maintenance vm only to avoid commit conflicts"
    System commitTransaction ifFalse: [ nil error: 'Could not commit for GemToGemSignaling' ].
  ].

System transactionMode: #manualBegin.
Exception 
  installStaticException: 
    [:ex :cat :num :args |
      "Run the abort in a lowPriority process, since we must acquire the
       transactionMutex."
      [
        GRPlatform current transactionMutex 
          critical: [ 
            GRPlatform current doAbortTransaction ].
        System enableSignaledAbortError.
      ] forkAt: Processor lowestPriority.
    ]
  category: GemStoneError
  number: 6009 "#rtErrSignalAbort"
  subtype: nil.
System enableSignaledAbortError.
"This thread is needed to handle the SigAbort exception, when the primary
 thread is blocked. Assuming default 60 second STN_GEM_ABORT_TIMEOUT, wake 
 up at 30 second intervals."
[ 
  [ true ] whileTrue: [ (Delay forSeconds: 30) wait ].
] forkAt: Processor lowestPriority.

count := 0.
[true] whileTrue: [ [
  "run maintenance tasks"
  WAGemStoneMaintenanceTask performTasks: count.
  "Sleep for a minute"
  (Delay forSeconds: 60) wait.
  count := count + 1] 
    on: Error, Halt, Breakpoint
    do: [:ex | 
	  System inTransaction
		ifTrue: [ 
          DebuggerLogEntry createContinuationLabeled: 'MTCE continuation'.
          System commitTransaction.
          System beginTransaction ]
        ifFalse: [
          System beginTransaction.
          DebuggerLogEntry createContinuationLabeled: 'MTCE continuation'.
          System commitTransaction].
      ex isResumable ifTrue: [ex resume]]].
%
run
GemToGemAnnouncement uninstallStaticHandler.
System beginTransaction.
(ObjectLogEntry
  fatal: 'MTCE: topaz exit'
  object:
    'pid: ', (System gemVersionReport at: 'processId') printString) addToLog.
System commitTransaction.
%
EOF
