## replicate.pro : tasks for data replication : -*- sh -*-
## this file contains the tasks for maintaining duplicates as needed in Nebulous
## these tasks use the books replicatePending

# test for required global variables
check.globals

$NEB_DB    = nebulous
$NEB_HOST  = alala
$NEB_USER  = jhipp
$NEB_PASS  = jhipp

$LOGSUBDIR = $LOGDIR/replicate
exec mkdir -p $LOGSUBDIR

book init replicatePending

macro replicate.reset
  book init replicatePending
end

macro replicate.status
  book listbook replicatePending
end

macro replicate.on
  task replicate.load
    active true
  end
  task replicate.run
    active true
  end
end

macro replicate.off
  task replicate.load
    active false
  end
  task replicate.run
    active false
  end
end

macro set.host.for.replicate
  if ($0 != 2)
    echo "USAGE: set.host.for.replicate (hostname)"
    break
  end

  if (not($PARALLEL))
    host local
    return
  end

# parse volume name

  if ("$1" == "NULL")
    host anyhost
  else
    host $1
  end
end

# the replicate process interacts with only the single Nebulous server

# select Nebulous objects which desire additional copies
task	       replicate.load
  host         local

  # modify these after the tasks are tested
  periods      -poll 10
  periods      -exec 1
  periods      -timeout 300
  npending     1

  # silently drop stdout
  stdout NULL
  stderr $LOGSUBDIR/replicate.log

  task.exec
      # command does not need to be dynamic, but having it so allows us to adjust the periods
      # so that we dont have to wait 10 minutes for things to start up
      command neb-admin --host $NEB_HOST --db $NEB_DB --user $NEB_USER --pass $NEB_PASS --pendingreplicate --limit 2000
      periods      -exec 600

  end

  # success
  task.exit $EXIT_SUCCESS
    # convert 'stdout' to book format
    ipptool2book stdout replicatePending -key key -uniq -setword pantaskState INIT

    if ($VERBOSE > 2)
      book listbook replicatePending
    end

    # delete existing entries in the appropriate pantaskStates
    process_cleanup replicatePending
  end

  # locked list
  task.exit    default
    showcommand failure
  end

  # operation times out?
  task.exit    timeout
    showcommand timeout
  end
end

# create the desired replicas
task	       replicate.run
  periods      -poll $RUNPOLL
  periods      -exec 30
  periods      -timeout 30

  task.exec
    book npages replicatePending -var N
    if ($NETWORK == 0) break
    if ($N == 0)
        periods -exec 30
        break
    end
    periods -exec 0.05
    
    # look for new objects in replicatePending
    book getpage replicatePending 0 -var pageName -key pantaskState INIT
    if ("$pageName" == "NULL") break

    book setword replicatePending $pageName pantaskState RUN

    # XXX what values do I need to get back?
    book getword replicatePending $pageName key         -var KEY
    book getword replicatePending $pageName need_copies -var NEED_COPIES
    book getword replicatePending $pageName volume_name -var VOLUME_NAME
    book getword replicatePending $pageName volume_host -var VOLUME_HOST

    set.host.for.replicate $VOLUME_HOST

    stdout NULL
    stderr $LOGSUBDIR/replicate.log

    # these operations do not require a database to be specified
    $run = neb-replicate --copies $NEED_COPIES $KEY

    # save the pageName for future reference below
    options $pageName

    # create the command line
    if ($VERBOSE > 1)
      echo command $run
    end
    command $run
  end

  # default exit status
  task.exit default
    process_exit replicatePending $options:0 $JOB_STATUS
  end

  # operation timed out?
  task.exit    timeout
    showcommand timeout
    book setword replicatePending $options:0 pantaskState TIMEOUT
  end
end

