Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How could I realize aos [myProcess] --cron 5-minutes in sub-process‘ lua code when using ao.spawn #225

Open
xgocn opened this issue Sep 10, 2024 · 17 comments

Comments

@xgocn
Copy link
Contributor

xgocn commented Sep 10, 2024

    local newProcess = ao.spawn(ao.env.Module.Id, {
      Authority = ao.id
    }).receive()

    ao.send({
      Target = newProcess.Process,
      Action = 'Eval',
      Data = [[
            Handlers.add(
                'CronTick', 
                Handlers.utils.hasMatchingTag('Action', 'Cron'), 
                 function(msg)
                    msg.reply({ Data = 'Pong'})
                end
            )
        ]]
    })

How could I realize aos [myProcess] --cron 5-minutes in sub-process‘ lua code when using ao.spawn

@xgocn xgocn changed the title How could I realize aos [myProcess] --cron 5-minutes in process‘ lua code How could I realize aos [myProcess] --cron 5-minutes in sub-process‘ lua code when using ao.spawn Sep 10, 2024
@twilson63
Copy link
Contributor

Spawn(ao.env.Module.Id, {
  Authority = ao.id,
  ["Cron-Tag-Action"] = "Cron",
  ["Cron-Interval"] = "5-minutes"
 })

You will have to use the MU REST endpoint to engage monitoring...

POST https://mu.ao-testnet.xyz/monitor/:processid

@xgocn
Copy link
Contributor Author

xgocn commented Sep 10, 2024

When we define spawn, newprocess has not defined handler yet. Is cron already running? When we add handlers.add in the later part, it has business logic. Before that, is cron executing an empty loop?

@twilson63
Copy link
Contributor

twilson63 commented Sep 10, 2024 via email

@xgocn
Copy link
Contributor Author

xgocn commented Sep 10, 2024

I just ran curl -X POST https://mu.ao-testnet.xyz/monitor/b3WAvKqYrdsLBtGkNVPD5RpeJERy1WiCA3LBbf6LmPI

got {"error":"this.binary.subarray is not a function"}

@xgocn
Copy link
Contributor Author

xgocn commented Sep 10, 2024

I believe the newprocess is running,because I send a Ping and got a Pong

@xgocn
Copy link
Contributor Author

xgocn commented Sep 10, 2024

this is my code

    -- parent process : l7ph6gka-mmrVE5P23lRa-1BWgQC8MXH8FcTbs6wk_U 
    Handlers.add(
    'ping',
    Handlers.utils.hasMatchingTag('Action', 'Ping'),
    function(msg)
        print("Received Ping from: " .. msg.From)
        msg.reply({ Data = 'Pong from Main Process:' .. tostring(ao.id)})
    end
   )

    local newProcess = ao.spawn(ao.env.Module.Id, {
        Authority = ao.id,
        ["Cron-Tag-Action"] = "Cron",
        ["Cron-Interval"] = "10-seconds"
    }).receive()

    print(newProcess.Process)
   -- got jSj0KUIDJN8gx8PW5-MLZ_gTAaY6_EQd0N3Kl6AT6YY

    DB:exec(string.format("UPDATE npc SET proc_id = '%s' WHERE id = %d", newProcess.Process,npcId))

    ao.send({
      Target = newProcess.Process,
      Action = 'Eval',
      Data = [[
            Handlers.add(
                'ping',
                Handlers.utils.hasMatchingTag('Action', 'Ping'),
                function(msg)
                    ao.send({Target = 'l7ph6gka-mmrVE5P23lRa-1BWgQC8MXH8FcTbs6wk_U',Action = 'Ping'})
                    msg.reply({ Data = 'Pong'})
                end
            )
            
            Handlers.add(
                'CronTick', 
                Handlers.utils.hasMatchingTag('Action', 'Cron'), 
                function(msg)
                    ao.send({Target ='l7ph6gka-mmrVE5P23lRa-1BWgQC8MXH8FcTbs6wk_U',Action = 'Ping'})
                    msg.reply({ Data = "NPC has died." })
                end
            )
        ]]
    })

I make test like:

[email protected][Inbox:90]> Send({Target="jSj0KUIDJN8gx8PW5-MLZ_gTAaY6_EQd0N3Kl6AT6YY",Action="Ping"})
{
   onReply = function: 0x227dc60,
   output = "Message added to outbox",
   receive = function: 0x227dd00
}
Received Ping from: jSj0KUIDJN8gx8PW5-MLZ_gTAaY6_EQd0N3Kl6AT6YY

but the cron part didn't work,so I make a futher test:
I call Cron as a common handler,it responed

[email protected][Inbox:91]> Send({Target="jSj0KUIDJN8gx8PW5-MLZ_gTAaY6_EQd0N3Kl6AT6YY",Action="Cron"})
{
   onReply = function: 0x227d9e0,
   output = "Message added to outbox",
   receive = function: 0x227d840
}
Received Ping from: jSj0KUIDJN8gx8PW5-MLZ_gTAaY6_EQd0N3Kl6AT6YY

by far,I consider the handler's code is correct and the cron works too,but can't ao.send a Ping under Cron mode

what could the problem be?

@xgocn
Copy link
Contributor Author

xgocn commented Sep 11, 2024

who is the singer of a cron handler in sub-process,maybe no one?

@twilson63
Copy link
Contributor

twilson63 commented Sep 11, 2024 via email

@xgocn
Copy link
Contributor Author

xgocn commented Sep 11, 2024

yes, signer,if cron messages are generated implicitly,the

Handlers.add(
                'CronTick', 
                Handlers.utils.hasMatchingTag('Action', 'Cron'), 
                function(msg)
                    -- here
                    ao.send({Target =parentProcess,Action = 'Ping'})
                    -- 
                    msg.reply({ Data = "NPC has died." })
                end
            )

the ao.send didn‘t work under implicit mode in my test, BUT works when called as a standard handler like [email protected][Inbox:91]> Send({Target=:NEWPROCESSID,Action="Cron"})

@twilson63
Copy link
Contributor

twilson63 commented Sep 12, 2024 via email

@xgocn
Copy link
Contributor Author

xgocn commented Sep 12, 2024

test.zip

the ao.send in Cron doesn't work for unknown reason

@xgocn
Copy link
Contributor Author

xgocn commented Sep 13, 2024

And I‘m not the only one who met the problem

@twilson63
Copy link
Contributor

twilson63 commented Sep 13, 2024 via email

@xgocn
Copy link
Contributor Author

xgocn commented Sep 14, 2024

Have a good time!

@antaintan
Copy link

I had the same problem

@windTrace
Copy link

yes, bug exists when call ao.send in Cron of spawned sub-process

@xgocn
Copy link
Contributor Author

xgocn commented Sep 30, 2024

To be continued here #228

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants