<commit_before>Application = require '../models/application'
{AppManager} = require "../lib/paas"

applicationTimeout = []

mark_broken = (app, err) ->
    app.state = "broken"
    app.password = <PASSWORD>
    app.errormsg = err.message
    app.save (saveErr) ->
        return send_error saveErr if saveErr

stop_app = (app) ->
    manager = new AppManager
    manager.stop app, (err, result) =>
        return mark_broken app, err if err
        data =
            state: "stopped"
            port: 0
        app.updateAttributes data, (err) =>
            return send_error err if err
            manager.resetProxy (err) =>
                return mark_broken app, err if err

start_timeout = (name) ->
    applicationTimeout[name] = setTimeout () ->
        if name isnt "home" and name isnt "proxy"
            Application.all (err, apps) ->
                for app in apps
                    if app.name is name and app.isStoppable
                        console.log "stop : " + name
                        stop_app app
    , 180000

module.exports.restartTimeout = (name) ->
    if applicationTimeout[name]?
        clearTimeout applicationTimeout[name]
    start_timeout name

module.exports.init = () ->
    Application.all (err, apps) ->
        for app in apps
            if app.state is 'installed' and app.isStoppable
                start_timeout app.name<commit_msg>improve syntax and add comments<commit_after>Application = require '../models/application'
{AppManager} = require "../lib/paas"

applicationTimeout = []

###
Mark application broken
   * Update application state in database
###
markBroken = (app, err) ->
    app.state = "broken"
    app.password = <PASSWORD>
    app.errormsg = err.message
    app.save (saveErr) ->
        return send_error saveErr if saveErr

###
Stop application <app> :
   * Stop process (via controller)
   * Update application state in database
   * Reset proxy routes
###
stopApp = (app) ->
    manager = new AppManager
    manager.stop app, (err, result) =>
        return markBroken app, err if err
        data =
            state: "stopped"
            port: 0
        app.updateAttributes data, (err) =>
            return send_error err if err
            manager.resetProxy (err) =>
                return markBroken app, err if err

###
Start timeout for application other than proxy and home
    * After 3 minutes of inactivity, application are stopped
    if application is stoppable.
###
startTimeout = (name) ->
    applicationTimeout[name] = setTimeout () ->
        if name isnt "home" and name isnt "proxy"
            Application.all (err, apps) ->
                for app in apps
                    if app.name is name and app.isStoppable
                        console.log "stop : " + name
                        stopApp app
    , 3 * 60 * 1000

###
Restart tiemout for application.
    * Remove old timeout if it exists
    * Start new timeout (3 minutes)
###
module.exports.restartTimeout = (name) ->
    if applicationTimeout[name]?
        clearTimeout applicationTimeout[name]
    startTimeout name

###
Init timeout
    When home is started, it start timeout for all installed application
###
module.exports.init = () ->
    Application.all (err, apps) ->
        for app in apps
            if app.state is 'installed' and app.isStoppable
                startTimeout app.name
{'fraction_non_alphanumeric': 0.09506833036244801, 'fraction_numerical': 0.005050505050505051, 'mean_word_length': 2.097516099356026, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7119430', 'n_tokens_mistral': 935, 'n_tokens_neox': 900, 'n_words': 305}